推荐 卓然博客:http://csidm.com/ C#操作Access之创建表是如何实现的呢?首先我们来看看基本的知识:本文C#操作Access数据库的基础知识,并提供一个相关的实例。C#的ADO.NET还不能通过编程方式创建全新的ACCESS(MDB)数据库,所以还只能使用ADOX这个来自COM
推荐 卓然博客:http://csidm.com/
C#操作Access之创建表是如何实现的呢?首先我们来看看基本的知识:本文C#操作Access数据库的基础知识,并提供一个相关的实例。C#的ADO.NET还不能通过编程方式创建全新的ACCESS(MDB)数据库,所以还只能使用ADOX这个来自COM的链接库来操作。
C#操作Access主要知识点如下:
<ol> <li><span><span>using</span><span> System.Data.OleDb; </span></span></li> <li><span> </span></li> <li> <span>using</span><span> System.Data; </span> </li> </ol>
C#操作Access连接字符串:
<ol> <li><span>String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; </span></li> <li><span> </span></li> <li><span>Data Source=product.mdb"; </span></li> </ol>
C#操作Access建立连接:
<ol><li><span><span>OleDbConnection connection = </span><span>new</span><span> OleDbConnection(connectionString); </span></span></li></ol>
C#操作Access使用OleDbCommand类来执行Sql语句:
<ol> <li><span><span>OleDbCommand cmd = </span><span>new</span><span> OleDbCommand(sql, connection); </span></span></li> <li><span> </span></li> <li><span>connection.Open(); </span></li> <li><span> </span></li> <li><span>cmd.ExecuteNonQuery(); </span></li> </ol>
C#操作Access之创建表,例程如下:
通常一个mdb的可以包含n个表。下面的程序主要是创建一个table。
<ol> <li><span><span>//新建mdb的表,C#操作Access之创建表</span><span> </span></span></li> <li> </li> <li> <span>//mdbHead是一个ArrayList,存储的是table表中的具体列名。 </span><span> </span> </li> <li><span> </span></li> <li> <span>public</span><span> </span><span>static</span><span> </span><span>bool</span><span> CreateMDBTable( </span> </li> <li> <span>string</span><span> mdbPath,</span><span>string</span><span> tableName, ArrayList mdbHead) </span> </li> <li><span> </span></li> <li><span>{ </span></li> <li> <span> </span><span>try</span><span> </span> </li> <li><span> { </span></li> <li> <span>ADOX.CatalogClass cat = </span><span>new</span><span> ADOX.CatalogClass(); </span> </li> <li><span> </span></li> <li> <span>string</span><span> sAccessConnection = @"Provider=Microsoft.Jet.OLEDB.4.0; </span> </li> <li><span>Data Source=" + mdbPath; </span></li> <li><span>cat.Create(strAccessConnection); </span></li> <li> <span>ADODB.Connection cn = </span><span>new</span><span> ADODB.Connection(); </span> </li> <li><span> </span></li> <li> <span>cn.Open(sAccessConnection, </span><span>null</span><span>, </span><span>null</span><span>, -1); </span> </li> <li><span> </span></li> <li><span>cat.ActiveConnection = cn; </span></li> <li><span> </span></li> <li><span>//新建一个表,C#操作Access之创建表</span></li> <li> <span>ADOX.TableClass tbl = </span><span>new</span><span> ADOX.TableClass(); </span> </li> <li><span>tbl.ParentCatalog = cat; </span></li> <li><span>tbl.Name = tableName; </span></li> <li><span> </span></li> <li> <span>int</span><span> size = mdbHead.Count; </span> </li> <li> <span>for</span><span> (</span><span>int</span><span> i = 0; i </span> </li> <li><span>{ </span></li> <li> <span> </span><span>//增加一个文本字段 </span><span> </span> </li> <li> <span> ADOX.ColumnClass col2 = </span><span>new</span><span> ADOX.ColumnClass(); </span> </li> <li><span> </span></li> <li><span> col2.ParentCatalog = cat; </span></li> <li> <span> col2.Name = mdbHead[i].ToString();</span><span>//列的名称 </span><span> </span> </li> <li><span> </span></li> <li> <span> col2.Properties[</span><span>"Jet OLEDB:Allow Zero Length"</span><span>].Value = </span><span>false</span><span>; </span> </li> <li><span> </span></li> <li><span> tbl.Columns.Append(col2, ADOX.DataTypeEnum.adVarWChar, 500); </span></li> <li><span>} </span></li> <li><span>cat.Tables.Append(tbl); </span></li> <li><span> </span></li> <li> <span> </span><span>//这句把表加入数据库(非常重要) ,C#操作Access之创建表</span><span> </span> </li> <li> <span>tbl = </span><span>null</span><span>; </span> </li> <li> <span>cat = </span><span>null</span><span>; </span> </li> <li><span>cn.Close(); </span></li> <li> <span>return</span><span> </span><span>true</span><span>; </span> </li> <li><span> } </span></li> <li> <span> </span><span>catch</span><span> { </span><span>return</span><span> </span><span>false</span><span>; } </span> </li> <li><span>} </span></li> </ol>
C#操作Access之创建表的基本内容就向你介绍到这里,希望对你了解和学习C#操作Access之创建表有所帮助。
推荐 卓然博客:http://csidm.com/