ASP.NET - How to store Byte[] in a BLOB column of MYSQL database using ODBC ?

Asked By Super Man on 04-Mar-11 03:54 AM
How to store Byte[] in a BLOB column of MYSQL database using ODBC ?

MYCode:

byte[] monkey = System.Text.Encoding.ASCII.GetBytes(ftb1.Text);

System.Data.Odbc.OdbcParameter param = new System.Data.Odbc.OdbcParameter("@ftb", System.Data.Odbc.OdbcType.Binary);

param.DbType = DbType.Binary;
param.Value = monkey;
param.Size = monkey.Length;
cmd.Parameters.Add(param);



I have used this. it gives me that error  that description field value can not be null.

Here description is the blob column and ftb parameter is used for that?


Any ideas?
Jatin Prajapati replied to Super Man on 04-Mar-11 03:57 AM
Hi,
Please refer the below code example.

01.public class DataAccess
02.{
03.  private string _strConn =
04.    @"Driver=   {MySQLODBC 3.51 Driver};SERVER=localhost;DATABASE=test;";
05. 
06.  private OdbcConnection _objConn;
07. 
08.  public DataAccess()
09.  {
10.    this._objConn = new OdbcConnection(this._strConn); 
11.  }
12.  // This function adds the Images to database
13. 
14. 
15.  public string addImage(byte [] buffer,string extension)
16.  {
17.    string strSql = "SELECT * FROM File";
18.    DataSet ds = new DataSet("Image");
19.    OdbcDataAdapter tempAP = new OdbcDataAdapter(strSql,this._objConn);
20.    OdbcCommandBuilder objCommand = new OdbcCommandBuilder(tempAP);
21.    tempAP.Fill(ds,"Table");
22. 
23.    try
24.    {
25.      this._objConn.Open();
26.      DataRow objNewRow = ds.Tables["Table"].NewRow();
27.      objNewRow["Extension"] = extension;
28.      objNewRow["Data"] = buffer;
29.      ds.Tables["Table"].Rows.Add(objNewRow);
30.      // trying to update the table to add the image
31. 
32.      tempAP.Update(ds,"Table");
33.    }
34.    catch(Exception e){return e.Message;}
35.    finally{this._objConn.Close();}
36.    return null;
37.  }
38.  // This function to get the image data from the database
39. 
40. 
41.  public byte [] getImage(int imageNumber)
42.  {
43.    string strSql = "SELECT * FROM File";
44.    DataSet ds = new DataSet("Image");
45.    OdbcDataAdapter tempAP = new OdbcDataAdapter(strSql,this._objConn);
46.    OdbcCommandBuilder objCommand = new OdbcCommandBuilder(tempAP);
47.    tempAP.Fill(ds,"Table");
48. 
49.    try
50.    {
51.      this._objConn.Open();
52.      byte [] buffer = (byte [])ds.Tables["Table"].Rows[imageNumber]["Data"];
53.      return buffer;
54.    }
55.    catch{this._objConn.Close();return null;}
56.    finally{this._objConn.Close();}       
57.  }
58.  // Get the image count
59. 
60. 
61.  public int getCount()
62.  {
63.    string strSql = "SELECT COUNT(Data) FROM File";
64.    DataSet ds = new DataSet("Image");
65.    OdbcDataAdapter tempAP = new OdbcDataAdapter(strSql,this._objConn);
66.    OdbcCommandBuilder objCommand = new OdbcCommandBuilder(tempAP);
67.    tempAP.Fill(ds,"Table");
68. 
69.    try>
70.    {
71.      this._objConn.Open();
72.      int count = (int)ds.Tables["Table"].Rows[0][0];
73.      return count;
74.    }
75.    catch{this._objConn.Close();return 0;}
76.    finally{this._objConn.Close();}
77.  }
78. 
79.}

Super Man replied to Jatin Prajapati on 04-Mar-11 04:03 AM
There is not even a single Insert Statement in the answer that you gave.

Doesn't works 4 me.
Jatin Prajapati replied to Super Man on 04-Mar-11 04:18 AM
Hi,
Try the blow one. Create stored procedure in MySql.
01.DELIMITER $$
02. 
03.DROP PROCEDURE IF EXISTS `sakila`.`Image_Insert`$$
04. 
05.CREATE DEFINER=`stefan`@`%` PROCEDURE `Image_Insert`(in p_in_name varchar(50), in p_in_data longblob)
06.BEGIN
07.  insert into images (name,imageData) values( p_in_name, p_in_data);
08.  select LAST_INSERT_ID();
09.END$$
10. 
11.DELIMITER ;


code.
---------------
01.public long InsertMySQL_GetLast_SP(string name, byte[] data)
02.    {
03.      MySql.Data.MySqlClient.MySqlConnection oConn =
04.        new MySql.Data.MySqlClient.MySqlConnection("Database=sakila;Data Source=192.168.10.4;User id=stefan;Password=pekka");
05.      oConn.Open();
06.  
07.  
08.      MySql.Data.MySqlClient.MySqlCommand command =
09.        new MySql.Data.MySqlClient.MySqlCommand("Image_Insert", oConn);
10.      command.CommandType = CommandType.StoredProcedure;
11.      command.Parameters.Add(new MySql.Data.MySqlClient.MySqlParameter("?p_in_name", MySql.Data.MySqlClient.MySqlDbType.VarChar, 50));
12.      command.Parameters.Add(new MySql.Data.MySqlClient.MySqlParameter("?p_in_data", MySql.Data.MySqlClient.MySqlDbType.LongBlog));
13.      command.Parameters[0].Value = name;
14.      command.Parameters[1].Value = data;
15.      //command.ExecuteNonQuery();
16.  
17.      long lNewId = (long)command.ExecuteScalar();
18.  
19.      oConn.Close();
20.      return lNewId;
21.  
22.    }

Super Man replied to Jatin Prajapati on 04-Mar-11 04:24 AM
I haven't tried your code because it using MYSql connector not using ODBC.

 I want using ODBC and w/o Stored procedure.

That's why i have attached my sample code.

can you / others help me more on this ??

solved How to Insert/store Byte[] in BLOB column of MYSQL using ODBC
Super Man replied to Jatin Prajapati on 04-Mar-11 05:09 AM
Odbc doesnt support named parameter in command text.

so we have to specify simply a "?" instead of any parameter name.

Thanks i got it. Its for your knowledge and others who find this by search engine.

thanks 4 ur support.