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.}