2024年1月13日发(作者:)

看到CSDN上不断兄弟姐妹提问关于c#操作Access的问题,于是本人利用闲暇将c#操作Access的方法加以总结,主要解决的问题有:

创建mdb

创建table

读取table内容

查询table中的内容

向table中插入数据

删除table中的记录

向table中插入照片

读取table中的照片等。

另:本人水平有限,不当之处还请斧正。废话少说,开始正题。

文介绍C#访问操作Access数据库的基础知识,并提供一个相关的例程。C#的还不能通过编程方式创建全新的ACCESS(MDB)数据库,所以还只能使用ADOX这个来自COM的链接库来操作。

主要知识点如下:

using ;

using ;

连接字符串:String connectionString = "Provider=.4.0;Data Source=";

建立连接:OleDbConnection connection = new OleDbConnection(connectionString);

使用OleDbCommand类来执行Sql语句:

OleDbCommand cmd = new OleDbCommand(sql, connection);

();

eNonQuery();

1.创建mdb库,例程如下:

需要注意的是:参数mdbPath是mdb的完整路径(不包含表的名称)。例如:D://

[c-sharp] view plaincopy

1. //创建mdb 2. public static bool CreateMDBDataBase(string mdbPath) 3. { 4. try 5. { 6. gClass cat = new gClass(); 7. ("Provider=.4.0;Data Source=" + mdbPath + ";"); 8. cat = null; 9. return true; 10. } 11. catch { return false; } 12. }

2.创建具体的表,例程如下:

通常一个mdb的可以包含n个表。下面的程序主要是创建一个table。

[c-sharp] view plaincopy

1. //新建mdb的表 2. //mdbHead是一个ArrayList,存储的是table表中的具体列名。 3. public static bool CreateMDBTable(string mdbPath,string tableName, ArrayList mdbHead) 4. { 5. try 6. { 7. gClass cat = new gClass(); 8. string sAccessConnection 9. = @"Provider=.4.0;Data Source=" + mdbPath; 10. tion cn = new tion(); 11. (sAccessConnection, null, null, ‐1); 12. Connection = cn; 13. 14. //新建一个表 15. lass tbl = new lass(); 16. Catalog = cat; 17. = tableName; 18. 19. int size = ; 20. for (int i = 0; i < size; i++) 21. { 22. //增加一个文本字段 23. Class col2 = new Class(); 24. Catalog = cat; 25. = mdbHead[i].ToString();//列的名称 26. ties["Jet OLEDB:Allow Zero Length"].Value = false; 27. (col2, Char, 500); 28. } 29. (tbl); //这句把表加入数据库(非常重要) 30. tbl = null; 31. cat = null; 32. (); 33. return true; 34. } 35. catch { return false; } 36. } 3.读取mdb内容(完全读取),例程如下:

本例程返回的是一个DataTable,如需其他格式可以自行转换。

[c-sharp] view plaincopy

1. // 读取mdb数据 2. public static DataTable ReadAllData(string tableName, string mdbPath,ref bool success) 3. { 4. DataTable dt = new DataTable(); 5. try 6. { 7. DataRow dr; 8. //1、建立连接 9. string strConn 10. = @"Provider=.4.0;Data Source=" + mdbPath + ";Jet OLEDB:Database Password=haoren"; 11. OleDbConnection odcConnection = new OleDbConnection(strConn); 12. //2、打开连接 13. (); 14. //建立SQL查询 15. OleDbCommand odCommand = Command(); 16. //3、输入查询语句 17. dText = "select * from " + tableName; 18. //建立读取 19. OleDbDataReader odrReader = eReader(); 20. //查询并显示数据 21. int size = ount; 22. for (int i = 0; i < size; i++) 23. { 24. DataColumn dc; 25. dc = new DataColumn(e(i)); 26. (dc); 27. } 28. while (()) 29. { 30. dr = (); 31. for (int i = 0; i < size; i++) 32. { 33. dr[e(i)] = odrReader[e(i)].ToString(); 34. } 35. (dr); 36. } 37. //关闭连接 38. (); 39. (); 40. success = true; 41. return dt; 42. } 43. catch 44. { 45. success = false;

46. return dt; 47. } 48. } 4.读取mdb内容(按列读取),例程如下:

columns数组存储的是你要查询的列名称(必须确保mdb表中存在你要的列)

[c-sharp] view plaincopy

1. // 读取mdb数据 2. public static DataTable ReadDataByColumns(string mdbPaht,string tableName, string[] columns, ref bool success) 3. { 4. DataTable dt = new DataTable(); 5. try 6. { 7. DataRow dr; 8. //1、建立连接 9. string strConn 10. = @"Provider=.4.0;Data Source=" + mdbPath + ";Jet OLEDB:Database Password=haoren"; 11. OleDbConnection odcConnection = new OleDbConnection(strConn); 12. //2、打开连接 13. (); 14. //建立SQL查询 15. OleDbCommand odCommand = Command(); 16. //3、输入查询语句 17. string strColumn = ""; 18. for (int i = 0; i < ; i++) 19. { 20. strColumn += columns[i].ToString() + ","; 21. } 22. strColumn = d(','); 23. dText = "select "+strColumn+" from " + tableName; 24. //建立读取 25. OleDbDataReader odrReader = eReader(); 26. //查询并显示数据 27. int size = ount; 28. for (int i = 0; i < size; i++) 29. { 30. DataColumn dc; 31. dc = new DataColumn(e(i)); 32. (dc); 33. } 34. 35. while (())

36. { 37. dr = (); 38. for (int i = 0; i < size; i++) 39. { 40. dr[e(i)] = odrReader[e(i)].ToString(); 41. } 42. (dr); 43. } 44. //关闭连接 45. (); 46. (); 47. success = true; 48. return dt; 49. } 50. catch 51. { 52. success = false; 53. return dt; 54. } 55. } 话接上回。

介绍之前先介绍一个结构体。因为以下函数都要用到这个结构体。

[c-sharp] view plaincopy

1. //普通的节点 2. public struct Node 3. { 4. private string nodeType; 5. public string NodeType//表的字段名 6. { 7. set { nodeType = value; } 8. get { return nodeType; } 9. } 10. 11. private string nodeValue; 12. public string NodeValue//具体的值 13. { 14. set { nodeValue = value; } 15. get { return nodeValue; } 16. } 17. }

18. 19. //照片节点 20. public struct PictureNode 21. { 22. private string nodeType; 23. public string NodeType//照片的列名 24. { 25. set { nodeType = value; } 26. get { return nodeType; } 27. } 28. 29. private byte[] nodeValue; 30. public byte[] NodeValue//照片的值,注意类型 31. { 32. set { nodeValue = value; } 33. get { return nodeValue; } 34. } 35. } 具体就用不着多加描述了吧!继续看问题点。

1.向table中插入数据(按行插入,如果需要插入多条请自己组织这个函数就ok了),其中的

insertArray存储的是一系列Node,pictureNode是PictureNode。

[c-sharp] view plaincopy

1. //插入数据 2. public static bool InsertRow( string mdbPath, string tableName, ArrayList insertArray, 3. PictureNode pictureNode, ref string errinfo) 4. { 5. try 6. { 7. //1、建立连接 8. string strConn 9. = @"Provider=.4.0;Data Source=" + mdbPath + ";Jet OLEDB:Database Password=haoren"; 10. OleDbConnection odcConnection = new OleDbConnection(strConn); 11. //2、打开连接 12. (); 13. 14. string str_col = ""; 15. int size_col = ; 16. for (int i = 0; i < size_col; i++) 17. { 18. Node vipNode = new Node();

19. vipNode = (Node)insertArray[i]; 20. str_col += pe + ","; 21. } 22. str_col = str_d(','); 23. 24. 25. int size_row = ; 26. string str_row = ""; 27. for (int i = 0; i < size_row; i++) 28. { 29. Node vipNode = new Node(); 30. vipNode = (Node)insertArray[i]; 31. string v = ng(); 32. v = DealString(v); 33. if (v == "") 34. { 35. str_row += "null" + ','; 36. } 37. else 38. { 39. str_row += "'" + v + "'" + ','; 40. } 41. } 42. str_row = str_d(','); 43. if (pictureNode != null && lue != null) 44. { 45. str_col += ',' + pe; 46. str_row += ",@Image"; 47. } 48. string sql = "insert into " + tableName + @" (" + str_col + ") values" + @"(" + str_row + ")"; 49. OleDbCommand odCommand = new OleDbCommand(sql, odcConnection); 50. if (pictureNode != null && lue != null) 51. { 52. ("@Image", ary, ).Value = lue; 53. } 54. eNonQuery(); 55. (); 56. return true; 57. } 58. catch (Exception err) 59. { 60. errinfo = e; 61. return false; 62. }

63. } 2.更新一行的数据(与插入类似)

[c-sharp] view plaincopy

1. //更新一行数据 2. public static bool UpdateRow(string mdbPath, string tableName, 3. Node keyNode,ArrayList insertArray,PictureNode pictureNode, ref string errinfo) 4. { 5. try 6. { 7. //1、建立连接 8. string strConn 9. = @"Provider=.4.0;Data Source=" + mdbPath + ";Jet OLEDB:Database Password=haoren"; 10. OleDbConnection odcConnection = new OleDbConnection(strConn); 11. //2、打开连接 12. (); 13. 14. int size = ; 15. string str = ""; 16. for (int i = 0; i < size; i++) 17. { 18. Node node = new Node(); 19. node = (Node)insertArray[i]; 20. string v = ng(); 21. v = DealString(v); 22. str += pe + " = "; 23. if (v == "") 24. { 25. str += "null" + ','; 26. } 27. else 28. { 29. str += "'" + v + "'" + ','; 30. } 31. 32. } 33. str = d(','); 34. if (lue != null) 35. { 36. str += ',' + pe; 37. str += " = @Image"; 38. } 39. string sql = "update " + tableName + " set " + str +

40. " where " + pe + " = " + "'" + lue + "'"; 41. OleDbCommand odCommand = new OleDbCommand(sql, odcConnection); 42. if (lue != null) 43. { 44. ("@Image", ary, ).Value = lue; 45. } 46. eNonQuery(); 47. (); 48. return true; 49. } 50. catch (Exception err) 51. { 52. errinfo = e; 53. return false; 54. } 55. } 3. 插入图片数据.

[c-sharp] view plaincopy

1. //插入图片数据 2. public static bool InsertPictureToMDB(string mdbPath, string tableName,Node keyNode, 3. PictureNode pictureNode,ref string errinfo) 4. { 5. try 6. { 7. //1、建立连接 8. string strConn 9. = @"Provider=.4.0;Data Source=" + mdbPath + ";Jet OLEDB:Database Password=haoren"; 10. OleDbConnection odcConnection = new OleDbConnection(strConn); 11. //2、打开连接 12. (); 13. string sql = @"update " + tableName + " set " + pe + "=" 14. + "@Image where " + pe + "=" + "'"+lue+"'"; 15. OleDbCommand comm = new OleDbCommand(sql, odcConnection); 16. byte[] pic = lue; 17. ("@Image", ary, ).Value = pic; 18. eNonQuery(); 19. (); 20. return true; 21. } 22. catch (Exception err) 23. {

24. errinfo = e; 25. return false; 26. } 27. } 4.修改mdb的一条数据.

[c-sharp] view plaincopy

1. //修改mdb的一条数据 2. public static bool UpdateMDBNode( string tableName, Node keyNode, 3. Node saveNode, ref string errinfo) 4. { 5. try 6. { 7. //1、建立连接 8. string strConn 9. = @"Provider=.4.0;Data Source=" + mdbPath + ";Jet OLEDB:Database Password=haoren"; 10. OleDbConnection odcConnection = new OleDbConnection(strConn); 11. //2、打开连接 12. (); 13. 14. string sql = @"update "+ tableName+" set " + pe + " = '" + lue+ 15. "' where " + pe + " = " + "'" + lue + "'"; 16. OleDbCommand comm = new OleDbCommand(sql, odcConnection); 17. eNonQuery(); 18. (); 19. return true; 20. } 21. catch (Exception err) 22. { 23. errinfo = e; 24. return false; 25. } 26. } 5.从mdb中获得照片

[c-sharp] view plaincopy

1. //从mdb中获得照片 2. public static byte[] GetImageFromMDB( string tableName, Node keyNode) 3. { 4. try

5. { 6. string sql = "Select 照片 From " + tableName + 7. " member Where " + pe + " = " 8. + "'" + lue + "'"; 9. string strConn 10. = @"Provider=.4.0;Data Source=" + mdbPath + ";Jet OLEDB:Database Password=haoren"; 11. OleDbConnection odcConnection = new OleDbConnection(strConn); 12. //2、打开连接 13. (); 14. OleDbCommand comm = new OleDbCommand(sql, odcConnection); 15. OleDbDataReader sdr = eReader(); 16. (); 17. 18. byte[] pic = (byte[])sdr[0]; 19. (); 20. (); 21. return pic; 22. } 23. catch 24. { 25. return null; 26. } 27. }