生活随笔
收集整理的這篇文章主要介紹了
C# 操作ACCESS数据库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
看到CSDN上不斷兄弟姐妹提問關于c#操作Access的問題,于是本人利用閑暇將c#操作Access的方法加以總結,主要解決的問題有:
創建mdb
創建table
讀取table內容
查詢table中的內容
向table中插入數據
刪除table中的記錄
向table中插入照片
讀取table中的照片等。
另:本人水平有限,不當之處還請斧正。廢話少說,開始正題。
文介紹C#訪問操作Access數據庫的基礎知識,并提供一個相關的例程。C#的ADO.NET還不能通過編程方式創建全新的ACCESS(MDB)數據庫,所以還只能使用ADOX這個來自COM的鏈接庫來操作。
主要知識點如下:
using System.Data.OleDb;
using System.Data;
連接字符串:String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=product.mdb";
建立連接:OleDbConnection connection = new OleDbConnection(connectionString);
使用OleDbCommand類來執行Sql語句:
OleDbCommand cmd = new OleDbCommand(sql, connection);
connection.Open();
cmd.ExecuteNonQuery();
1.創建mdb庫,例程如下:
需要注意的是:參數mdbPath是mdb的完整路徑(不包含表的名稱)。例如:D:\\test.mdb
?
view plaincopy to clipboardprint? ?? ?public?static?bool?CreateMDBDataBase(string?mdbPath) ?? ?{ ?? ?????try?? ?????{ ?? ?????????ADOX.CatalogClass?cat?=?new?ADOX.CatalogClass(); ?? ?????????cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data?Source="?+?mdbPath?+?";"); ?? ?????????cat?=?null; ?? ?????????return?true; ?? ?????} ?? ?????catch?{?return?false;?} ?? ?}?? //創建mdb public static bool CreateMDBDataBase(string mdbPath) { try { ADOX.CatalogClass cat = new ADOX.CatalogClass(); cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath + ";"); cat = null; return true; } catch { return false; } }
?
2.創建具體的表,例程如下:
通常一個mdb的可以包含n個表。下面的程序主要是創建一個table。
view plaincopy to clipboardprint? ?? ?? public?static?bool?CreateMDBTable(string?mdbPath,string?tableName,?ArrayList?mdbHead) ?? { ?? ????try?? ????{ ?? ????????ADOX.CatalogClass?cat?=?new?ADOX.CatalogClass(); ?? ????????string?sAccessConnection ?? ????????????=?@"Provider=Microsoft.Jet.OLEDB.4.0;Data?Source="?+?mdbPath; ?? ????????ADODB.Connection?cn?=?new?ADODB.Connection(); ?? ????????cn.Open(sAccessConnection,?null,?null,?-1); ?? ????????cat.ActiveConnection?=?cn; ?? ?? ?????????? ????????ADOX.TableClass?tbl?=?new?ADOX.TableClass(); ?? ????????tbl.ParentCatalog?=?cat; ?? ????????tbl.Name?=?tableName; ?? ?? ????????int?size?=?mdbHead.Count; ?? ????????for?(int?i?=?0;?i?<?size;?i++) ?? ????????{ ?? ?????????????? ????????????ADOX.ColumnClass?col2?=?new?ADOX.ColumnClass(); ?? ????????????col2.ParentCatalog?=?cat; ?? ????????????col2.Name?=?mdbHead[i].ToString();?? ????????????col2.Properties["Jet?OLEDB:Allow?Zero?Length"].Value?=?false; ?? ????????????tbl.Columns.Append(col2,?ADOX.DataTypeEnum.adVarWChar,?500); ?? ????????} ?? ????????cat.Tables.Append(tbl);????? ????????tbl?=?null; ?? ????????cat?=?null; ?? ????????cn.Close(); ?? ????????return?true; ?? ????} ?? ????catch?{?return?false;?} ?? }?? //新建mdb的表 //mdbHead是一個ArrayList,存儲的是table表中的具體列名。 public static bool CreateMDBTable(string mdbPath,string tableName, ArrayList mdbHead) { try { ADOX.CatalogClass cat = new ADOX.CatalogClass(); string sAccessConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath; ADODB.Connection cn = new ADODB.Connection(); cn.Open(sAccessConnection, null, null, -1); cat.ActiveConnection = cn; //新建一個表 ADOX.TableClass tbl = new ADOX.TableClass(); tbl.ParentCatalog = cat; tbl.Name = tableName; int size = mdbHead.Count; for (int i = 0; i < size; i++) { //增加一個文本字段 ADOX.ColumnClass col2 = new ADOX.ColumnClass(); col2.ParentCatalog = cat; col2.Name = mdbHead[i].ToString();//列的名稱 col2.Properties["Jet OLEDB:Allow Zero Length"].Value = false; tbl.Columns.Append(col2, ADOX.DataTypeEnum.adVarWChar, 500); } cat.Tables.Append(tbl); //這句把表加入數據庫(非常重要) tbl = null; cat = null; cn.Close(); return true; } catch { return false; } }
?
3.讀取mdb內容(完全讀取),例程如下:
本例程返回的是一個DataTable,如需其他格式可以自行轉換。
view plaincopy to clipboardprint? ?? public?static?DataTable?ReadAllData(string?tableName,?string?mdbPath,ref?bool?success) ?? { ?? ????DataTable?dt?=?new?DataTable(); ?? ????try?? ????{ ?? ????????DataRow?dr; ?? ?????????? ????????string?strConn ?? ????????????=?@"Provider=Microsoft.Jet.OLEDB.4.0;Data?Source="?+?mdbPath?+?";Jet?OLEDB:Database?Password=haoren"; ?? ????????OleDbConnection?odcConnection?=?new?OleDbConnection(strConn); ?? ?????????? ????????odcConnection.Open(); ?? ?????????? ????????OleDbCommand?odCommand?=?odcConnection.CreateCommand(); ?? ?????????? ????????odCommand.CommandText?=?"select?*?from?"?+?tableName; ?? ?????????? ????????OleDbDataReader?odrReader?=?odCommand.ExecuteReader(); ?? ?????????? ????????int?size?=?odrReader.FieldCount; ?? ????????for?(int?i?=?0;?i?<?size;?i++) ?? ????????{ ?? ????????????DataColumn?dc; ?? ????????????dc?=?new?DataColumn(odrReader.GetName(i)); ?? ????????????dt.Columns.Add(dc); ?? ????????} ?? ????????while?(odrReader.Read()) ?? ????????{ ?? ????????????dr?=?dt.NewRow(); ?? ????????????for?(int?i?=?0;?i?<?size;?i++) ?? ????????????{ ?? ????????????????dr[odrReader.GetName(i)]?=?odrReader[odrReader.GetName(i)].ToString(); ?? ????????????} ?? ????????????dt.Rows.Add(dr); ?? ????????} ?? ?????????? ????????odrReader.Close(); ?? ????????odcConnection.Close(); ?? ????????success?=?true; ?? ????????return?dt; ?? ????} ?? ????catch?? ????{ ?? ????????success?=?false; ?? ????????return?dt; ?? ????} ?? }?? // 讀取mdb數據 public static DataTable ReadAllData(string tableName, string mdbPath,ref bool success) { DataTable dt = new DataTable(); try { DataRow dr; //1、建立連接 string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath + ";Jet OLEDB:Database Password=haoren"; OleDbConnection odcConnection = new OleDbConnection(strConn); //2、打開連接 odcConnection.Open(); //建立SQL查詢 OleDbCommand odCommand = odcConnection.CreateCommand(); //3、輸入查詢語句 odCommand.CommandText = "select * from " + tableName; //建立讀取 OleDbDataReader odrReader = odCommand.ExecuteReader(); //查詢并顯示數據 int size = odrReader.FieldCount; for (int i = 0; i < size; i++) { DataColumn dc; dc = new DataColumn(odrReader.GetName(i)); dt.Columns.Add(dc); } while (odrReader.Read()) { dr = dt.NewRow(); for (int i = 0; i < size; i++) { dr[odrReader.GetName(i)] = odrReader[odrReader.GetName(i)].ToString(); } dt.Rows.Add(dr); } //關閉連接 odrReader.Close(); odcConnection.Close(); success = true; return dt; } catch { success = false; return dt; } }
?
4.讀取mdb內容(按列讀取),例程如下:
columns數組存儲的是你要查詢的列名稱(必須確保mdb表中存在你要的列)
?
view plaincopy to clipboardprint? ?? public?static?DataTable?ReadDataByColumns(string?mdbPaht,string?tableName,?string[]?columns,?ref?bool?success) ?? { ?? ????DataTable?dt?=?new?DataTable(); ?? ????try?? ????{ ?? ????????DataRow?dr; ?? ?????????? ????????string?strConn ?? ????????????=?@"Provider=Microsoft.Jet.OLEDB.4.0;Data?Source="?+?mdbPath?+?";Jet?OLEDB:Database?Password=haoren"; ?? ????????OleDbConnection?odcConnection?=?new?OleDbConnection(strConn); ?? ?????????? ????????odcConnection.Open(); ?? ?????????? ????????OleDbCommand?odCommand?=?odcConnection.CreateCommand(); ?? ?????????? ????????string?strColumn?=?""; ?? ????????for?(int?i?=?0;?i?<?columns.Length;?i++) ?? ????????{ ?? ????????????strColumn?+=?columns[i].ToString()?+?","; ?? ????????} ?? ????????strColumn?=?strColumn.TrimEnd(','); ?? ????????odCommand.CommandText?=?"select?"+strColumn+"?from?"?+?tableName; ?? ?????????? ????????OleDbDataReader?odrReader?=?odCommand.ExecuteReader(); ?? ?????????? ????????int?size?=?odrReader.FieldCount; ?? ????????for?(int?i?=?0;?i?<?size;?i++) ?? ????????{ ?? ????????????DataColumn?dc; ?? ????????????dc?=?new?DataColumn(odrReader.GetName(i)); ?? ????????????dt.Columns.Add(dc); ?? ????????} ?? ?? ????????while?(odrReader.Read()) ?? ????????{ ?? ????????????dr?=?dt.NewRow(); ?? ????????????for?(int?i?=?0;?i?<?size;?i++) ?? ????????????{ ?? ????????????????dr[odrReader.GetName(i)]?=?odrReader[odrReader.GetName(i)].ToString(); ?? ????????????} ?? ????????????dt.Rows.Add(dr); ?? ????????} ?? ?????????? ????????odrReader.Close(); ?? ????????odcConnection.Close(); ?? ????????success?=?true; ?? ????????return?dt; ?? ????} ?? ????catch?? ????{ ?? ????????success?=?false; ?? ????????return?dt; ?? ????} ?? }?? // 讀取mdb數據 public static DataTable ReadDataByColumns(string mdbPaht,string tableName, string[] columns, ref bool success) { DataTable dt = new DataTable(); try { DataRow dr; //1、建立連接 string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath + ";Jet OLEDB:Database Password=haoren"; OleDbConnection odcConnection = new OleDbConnection(strConn); //2、打開連接 odcConnection.Open(); //建立SQL查詢 OleDbCommand odCommand = odcConnection.CreateCommand(); //3、輸入查詢語句 string strColumn = ""; for (int i = 0; i < columns.Length; i++) { strColumn += columns[i].ToString() + ","; } strColumn = strColumn.TrimEnd(','); odCommand.CommandText = "select "+strColumn+" from " + tableName; //建立讀取 OleDbDataReader odrReader = odCommand.ExecuteReader(); //查詢并顯示數據 int size = odrReader.FieldCount; for (int i = 0; i < size; i++) { DataColumn dc; dc = new DataColumn(odrReader.GetName(i)); dt.Columns.Add(dc); } while (odrReader.Read()) { dr = dt.NewRow(); for (int i = 0; i < size; i++) { dr[odrReader.GetName(i)] = odrReader[odrReader.GetName(i)].ToString(); } dt.Rows.Add(dr); } //關閉連接 odrReader.Close(); odcConnection.Close(); success = true; return dt; } catch { success = false; return dt; } }
?
今天就簡單寫到這里,以后我會將內容補充完整。
補充一句:轉載的朋友請一定注明出處謝謝!半支煙阿杰 http://blog.csdn.net/gisfarmer/
轉載于:https://www.cnblogs.com/291099657/archive/2009/10/15/1583966.html
總結
以上是生活随笔為你收集整理的C# 操作ACCESS数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。