C#动态创建及连接ACCESS数据库的详细步骤

连接ACCESS数据库的详细步骤

一、建立FORM窗体,加一个按钮控件,加一个DATAGRIDVIEW控件。

二、双击FORM,加入命名空间using System.Data.OleDb;

       双击按钮,进入按钮代码,写如下代码

            OleDbConnection strConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "员工信息.mdb" + ";Persist Security Info=False");

            //建立数据库引擎连接,注意数据表(后缀为.db)应放在DEBUG文件下
            OleDbDataAdapter myda = new OleDbDataAdapter("select * from 雇员 ,strConnection);

           //建立适配器,通过SQL语句去搜索数据库
            DataSet myds = new DataSet();

           //建立数据集
            myda.Fill(myds, "雇员");

           //用FILL的方式将适配器已经连接好的数据表填充到数据集MYDS这张表

           dataGridView1.DataSource = myds.Tables["联系人ID"];

          //用显示控件来显示表

三、按F5运行后,点击BUTTON按钮,便会显示相应的SQL语句下的数据库里的表。

     

下面利用Command和reader对象在控制台应用程序下输出数据。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace ConsoleApplication19
{
    class Program
    {
        static void Main(string[] args)
        {

            OleDbConnection mycon =null;
            OleDbDataReader myReader=null;
            try
            {
                string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db4.mdb;";
                mycon = new OleDbConnection(strcon);
                mycon.Open();
                string sql = "select * from 雇员 ";
                OleDbCommand mycom = new OleDbCommand(sql, mycon);
                myReader = mycom.ExecuteReader();
                while (myReader.Read())
                {
                    Console.WriteLine(myReader.GetString(0)+" "+myReader.GetDouble(1)+" "+myReader.GetString(2)+" "+myReader.GetString(3)+" "+myReader.GetString(4));

                }

            }
            finally 
            {
                myReader.Close();
                mycon.Close();
                
            }
        }
    }
}
动态创建代码:
//添加两个com组件引用
//Microsoft ADO Ext. 2.8 for DDL and Security
//Microsoft ActiveX Data Objects 2.8 Library
  

using System; using System.Collections.Generic; using System.Linq; using System.Text; using ADOX; using System.IO;

namespace WebRequestTest.Common {     public static class AccessDbHelper     {         ///

        /// 创建access数据库         ///         /// 数据库文件的全路径,如 D:\\NewDb.mdb         public static bool CreateAccessDb(string filePath)         {             ADOX.Catalog catalog = new Catalog();             if (!File.Exists(filePath))             {                 try                 {                     catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;DData Source=" + filePath + ";Jet OLEDB:Engine Type=5");                 }                 catch (System.Exception ex)                 {                     return false;                 }             }             return true;         }

        ///

        /// 在access数据库中创建表         ///         /// 数据库表文件全路径如D:\\NewDb.mdb 没有则创建         /// 表名         /// ADOX.Column对象数组         public static void CreateAccessTable(string filePath, string tableName, params ADOX.Column[] colums)         {             ADOX.Catalog catalog = new Catalog();             //数据库文件不存在则创建             if (!File.Exists(filePath))             {                 try                 {                     catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Jet OLEDB:Engine Type=5");                 }                 catch (System.Exception ex)                 {

                }             }             ADODB.Connection cn = new ADODB.Connection();             cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath, null, null, -1);             catalog.ActiveConnection = cn;             ADOX.Table table = new ADOX.Table();             table.Name = tableName;             foreach (var column in colums)             {                 table.Columns.Append(column);             }            // column.ParentCatalog = catalog;             //column.Properties["AutoIncrement"].Value = true; //设置自动增长             //table.Keys.Append("FirstTablePrimaryKey", KeyTypeEnum.adKeyPrimary, column, null, null); //定义主键             catalog.Tables.Append(table);             cn.Close();         }             //========================================================================================调用            //ADOX.Column[] columns = {            //                     new ADOX.Column(){Name="id",Type=DataTypeEnum.adInteger,DefinedSize=9},            //                     new ADOX.Column(){Name="col1",Type=DataTypeEnum.adWChar,DefinedSize=50},            //                     new ADOX.Column(){Name="col2",Type=DataTypeEnum.adLongVarChar,DefinedSize=50}            //                 };            // AccessDbHelper.CreateAccessTable("d:\\111.mdb", "testTable", columns);     } }

 

你可能感兴趣的:(ASP.NET,SQL)