Powered By Blogger

Thursday, December 24, 2009

upload image to database (image format) in MS-Sql server and MY-SQL

MS-SQL server
create table:


create table imgtab(idd int identity(1,1), imgsrc image)


aspx.vb


Dim intImageSize As Int64
Dim strImageType As String
Dim ImageStream As Stream
' intImageSize = PersonImage.PostedFile.ContentLength
intImageSize = fup1.PostedFile.ContentLength
strImageType = fup1.PostedFile.ContentType
ImageStream = fup1.PostedFile.InputStream
Dim ImageContent(intImageSize) As Byte
Dim intStatus As Integer
intStatus = ImageStream.Read(ImageContent, 0, intImageSize)
Dim myCommand As New SqlCommand("insert into
imgtab values ( @imgsamgetimage )'", Conn)
myCommand.Parameters.Add("@imgsamgetimage", SqlDbType.Image, ImageContent.Length).Value = ImageContent
myCommand.ExecuteNonQuery()
Conn.Close()


MY-SQL Server:

Create Table:

 CREATE TABLE `cust_file` (              
`id` int(11) NOT NULL auto_increment,
`customer_id` int(11) default NULL,
`filename` varchar(255) default NULL,
`filedata` blob,
`contenttype` varchar(255) default NULL,
`length` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1


C#:

 public void Mysql_File_Save(string sConnString, int nCustId, byte[] bData, string sName, string sContentType, int nContentLength)
{
using (MySql.Data.MySqlClient.MySqlConnection oConn =
new MySql.Data.MySqlClient.MySqlConnection(sConnString))
{
oConn.Open();

MySql.Data.MySqlClient.MySqlCommand oCommand = oConn.CreateCommand();
oCommand.Connection = oConn;


//Add new
oCommand.CommandText = "insert into cust_file(customer_id, filename, filedata, contenttype, length) " +
"values( ?in_customer_id, ?in_filename, ?in_filedata, ?in_contenttype, ?in_length)";
//oCommand.CommandType = CommandType.StoredProcedure;

MySql.Data.MySqlClient.MySqlParameter oParam =
oCommand.Parameters.Add("?in_customer_id",
MySql.Data.MySqlClient.MySqlDbType.Int64);
oParam.Value = nCustId;
oParam = oCommand.Parameters.Add("?in_filename",
MySql.Data.MySqlClient.MySqlDbType.VarChar, 255);
oParam.Value = sName;
oParam = oCommand.Parameters.Add("?in_filedata",
MySql.Data.MySqlClient.MySqlDbType.Blob);
oParam.Value = bData;
oParam = oCommand.Parameters.Add("?in_contenttype",
MySql.Data.MySqlClient.MySqlDbType.VarChar, 255);
oParam.Value = sContentType;
oParam = oCommand.Parameters.Add("?in_length",
MySql.Data.MySqlClient.MySqlDbType.Int64);
oParam.Value = nContentLength;

oCommand.ExecuteNonQuery();
oConn.Close();

}
}

Anyway it allows us to write code such as:
 byte[] bData = FileToArray("f:\\n1jpg.jpg");
Mysql_File_Save("Database=sakila;Data Source=192.168.10.4;User id=stefan;Password=pekka",
1, bData, "N1jpg", MimeType("f:\\n1jpg.jpg"), bData.Length);

Source:

http://www.programmingado.net/c-27/a-141/Insert-blob-into-MySQL.aspx

http://bytes.com/topic/asp-net/answers/342898-how-store-retrieve-array-bytes-mysql

http://www.dotnetspider.com/forum/194010-save-retrive-image-from-to-mysql-database-using-asp-net-c-language.aspx



No comments:

Post a Comment