




已阅读5页,还剩48页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
精品文档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.mdbview plaincopy to clipboardprint?/创建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; /创建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?/新建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 = mdbHeadi.ToString();/列的名称 col2.PropertiesJet 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 = mdbHeadi.ToString();/列的名称 col2.PropertiesJet 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?/ 读取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+) drodrReader.GetName(i) = odrReaderodrReader.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+) drodrReader.GetName(i) = odrReaderodrReader.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?/ 读取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 += columnsi.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+) drodrReader.GetName(i) = odrReaderodrReader.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 += columnsi.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+) drodrReader.GetName(i) = odrReaderodrReader.GetName(i).ToString(); dt.Rows.Add(dr); /关闭连接 odrReader.Close(); odcConnection.Close(); success = true; return dt; catch success = false; return dt; c#操作Access(下)介绍之前先介绍一个结构体。因为以下函数都要用到这个结构体。view plaincopy to clipboardprint?/普通的节点 public struct Node private string nodeType; public string NodeType/表的字段名 set nodeType = value; get return nodeType; private string nodeValue; public string NodeValue/具体的值 set nodeValue = value; get return nodeValue; /照片节点 public struct PictureNode private string nodeType; public string NodeType/照片的列名 set nodeType = value; get return nodeType; private byte nodeValue; public byte NodeValue/照片的值,注意类型 set nodeValue = value; get return nodeValue; /普通的节点 public struct Node private string nodeType; public string NodeType/表的字段名 set nodeType = value; get return nodeType; private string nodeValue; public string NodeValue/具体的值 set nodeValue = value; get return nodeValue; /照片节点 public struct PictureNode private string nodeType; public string NodeType/照片的列名 set nodeType = value; get return nodeType; private byte nodeValue; public byte NodeValue/照片的值,注意类型 set nodeValue = value; get return nodeValue; 具体就用不着多加描述了吧!继续看问题点。1.向table中插入数据(按行插入,如果需要插入多条请自己组织这个函数就ok了),其中的 insertArray存储的是一系列Node,pictureNode是PictureNode。view plaincopy to clipboardprint?/插入数据 public static bool InsertRow( string mdbPath, string tableName, ArrayList insertArray, PictureNode pictureNode, ref string errinfo) try /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(); string str_col = ; int size_col = insertArray.Count; for (int i = 0; i size_col; i+) Node vipNode = new Node(); vipNode = (Node)insertArrayi; str_col += vipNode.NodeType + ,; str_col = str_col.TrimEnd(,); int size_row = insertArray.Count; string str_row = ; for (int i = 0; i size_row; i+) Node vipNode = new Node(); vipNode = (Node)insertArrayi; string v = vipNode.NodeValue.ToString(); v = DealString(v); if (v = ) str_row += null + ,; else str_row += + v + + ,; str_row = str_row.TrimEnd(,); if (pictureNode!= null & pictureNode.NodeValue != null) str_col += , + pictureNode.NodeType; str_row += ,Image; string sql = insert into + tableName + ( + str_col + ) values + ( + str_row + ); OleDbCommand odCommand = new OleDbCommand(sql, odcConnection); if (pictureNode != null& pictureNode.NodeValue != null) odCommand.Parameters.Add(Image, OleDbType.VarBinary, pictureNode.NodeValue.Length).Value = pictureNode.NodeValue; odCommand.ExecuteNonQuery(); odcConnection.Close(); return true; catch (Exception err) errinfo = err.Message; return false; /插入数据 public static bool InsertRow( string mdbPath, string tableName, ArrayList insertArray, PictureNode pictureNode, ref string errinfo) try /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(); string str_col = ; int size_col = insertArray.Count; for (int i = 0; i size_col; i+) Node vipNode = new Node(); vipNode = (Node)insertArrayi; str_col += vipNode.NodeType + ,; str_col = str_col.TrimEnd(,); int size_row = insertArray.Count; string str_row = ; for (int i = 0; i size_row; i+) Node vipNode = new Node(); vipNode = (Node)insertArrayi; string v = vipNode.NodeValue.ToString(); v = DealString(v); if (v = ) str_row += null + ,; else str_row += + v + + ,; str_row = str_row.TrimEnd(,); if (pictureNode!= null & pictureNode.NodeValue != null) str_col += , + pictureNode.NodeType; str_row += ,Image; string sql = insert into + tableName + ( + str_col + ) values + ( + str_row + ); OleDbCommand odCommand = new OleDbCommand(sql, odcConnection); if (pictureNode
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年滨州邹平怀远学校教师模拟试卷及一套答案详解
- 2025广东肇庆市鼎湖区就业困难人员(脱贫劳动力)公益性岗位招聘1人模拟试卷含答案详解
- 2025贵州遵义医科大学附属口腔医院第十三届贵州人才博览会引进急需紧缺专业人才6人考前自测高频考点模拟试题有答案详解
- 2025北京市昌平区人民法院招聘辅助书记员2人模拟试卷带答案详解
- 2025年上半年资中县面向社会公开选聘社区工作者的(71人)考前自测高频考点模拟试题及答案详解参考
- 浮力的利用课件
- 安全培训考计划表课件
- 2025金华市教育局所属金华教育学院公开招聘教师6人考前自测高频考点模拟试题附答案详解(模拟题)
- 2025河南开封国禹建设投资有限公司开招聘3人考前自测高频考点模拟试题及完整答案详解
- 2025贵州黔西南州交通建设发展中心招聘公益性岗位工作人员模拟试卷附答案详解(模拟题)
- 工程经济学-邵颖红-第五版-课后作业
- 变压器主保护基本知识测试题
- 临汾市社区工作者考试题库2023
- 焊接应力计算讲义
- 转型中的地方政府:官员激励与治理(第二版)
- TCEEIA 572-2022 配电电缆局部放电定位及故障预警系统技术规范
- 数字心率计设计资料
- GB/T 3995-2006高铝质隔热耐火砖
- 人教版初中数学《与三角形有关的角》优秀版课件
- 渗滤液处理站运行方案
- 4制度安排及公共伦理课件
评论
0/150
提交评论