Friday, January 12, 2007

How to saving /reterving the Image into MSsql with Enterprise Library!

Pre
Before you read and test this program ,I want to explain some external feature that
I use .I use enterprise Library .So If you not interesting this feature ,please change Ado and use it .I will show with command.If you want to use enterprise Library ,But
you have not knowledge for that.Please attention .If not you can skip this section.
Firstly you need to add two .dll to reference of your project.
These are -
1.)Microsoft.Practices.EnterpriseLibrary.Common.dll
2.)Microsoft.Practices.EnterpriseLibrary.Data.dll

If you finish to install enterprise library .you can get the dll from the -
\Microsoft Enterprise Library January 2006\bin
If not , download and install it.You can get enterprise library and some documentation from there .

second you need to import these namespace in your code.
like this -
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;


Save Image into MSsql
Read the Image as a File .And then change to byte array.Now you can easily to save
Image into database.Like this -
protected void Savebtn_Click(object sender, EventArgs e)
{

string ss = File1.Value; //File1 is html input ( File ) control .
byte[] ff = File.ReadAllBytes(ss);
CallInsert(ss,ff);
}

private void CallInsert(string s,byte[] sd)
{
string str_conn = "Data Source=KAVAN;
Initial Catalog=myTestDB;Integrated Security=True";
string sqlcommand = "addImage";
try
{
Database db = new SqlDatabase(str_conn);

//this feature are enterprise lib feature .you can change Ado code.
DbCommand dbCommand = db.GetStoredProcCommand(sqlcommand);
db.AddInParameter(dbCommand, "@PicName", DbType.String, s);
db.AddInParameter(dbCommand, "@Pic", DbType.Binary,sd);

db.ExecuteNonQuery(dbCommand);
}
catch(Exception ex)
{
Console.WriteLine("Test Hight Light");
}
}

Table (
ImagePh )
table name =
ImagePh

Name Type
---------------------------
first column = PicNo int ( identity )
second column = PicName varchar (50)
thrid column = Pic image


addImage ( store procedure )
CREATE PROCEDURE [dbo].[addImage]
(
@PicName varchar(50),
@Pic image
)
AS
insert into ImagePh values(@PicName,@Pic)
GO


Reterive Image from MSsql
Read the data from database as DataSet.And then Generate Image .Like this -

protected void Showbtn_Click(object sender, EventArgs e)
{
DataSet ds = CallGet();
//this is hard code ,you can use looping like this
ds.Tables[0].Rows[x][2];
byte[] ff = (byte[])ds.Tables[0].Rows[0][2];
string f_name = (string)ds.Tables[0].Rows[0][1];
//there is also
string name =
ds.Tables[0].Rows[0][1];
System.IO.MemoryStream mstream = new System.IO.MemoryStream();
mstream.Write(ff, 0, ff.Length);
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(mstream);
bmp.Save(Server.MapPath(
f_name),System.Drawing.Imaging.ImageFormat.Jpeg);
Image1.ImageUrl = "./"+
f_name; //Image1 is Image control (Asp control );
}

Thank for your interest.


No comments:

Google Ad