C 连接ACCESS数据库代码实例

本文介绍了如何在C#中连接并操作ACCESS数据库,包括创建数据库、共享文件、建立C#窗体应用,并提供了完整的C#代码示例,展示了查询数据库记录的过程。
摘要由CSDN通过智能技术生成

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

 今天一个网友问到如何在C#中连接access数据库,经查找资料以及请教网友sammyLan,终于测试成功,大致过程如下

1、建立一个access数据库名字为CSharptest.mdb,里面有一个表person,它有两个字段personname(备注:由于name是access的关键字之一,所以尽量不要将字段名或者表明起为name,否则可能出错)和age,分别是文本和数字类型。

并插入两条记录,如下所示

personname     age

bushi                   30

John                    20

2、将access数据库文件CSharptest.mdb所在的目录(假设名为access)设为共享,假设我的机器地址为192.168.1.10,那么设为共享后,在地址栏内输入//192.168.1.10/access/这个目录应该之后可以看到CSharptest.mdb文件。

3、打开VS2008,菜单中点"新建"->"项目"->"Visual C#"->"Windows"->"windows窗体应用程序",建立一个新的C#窗体程序。

4、修改代码文件program.cs的内容,其完整C#代码如下。已经加了注释,就不再另外解释了

[c-sharp] view plain copy print ?
  1. using System.Windows.Forms;  
  2. using System.Data;  
  3. using System.Data.OleDb;  
  4.   
  5. namespace WindowsFormsApplication1  
  6. {  
  7.     static class Program  
  8.     {  
  9.         /// <summary>  
  10.         /// 应用程序的主入口点。  
  11.         /// </summary>  
  12.         [STAThread]  
  13.         static void Main()  
  14.         {  
  15.             //构造连接字符串  
  16.            string strConnection="Provider=Microsoft.Jet.OleDb.4.0;";  
  17.          strConnection +=@"Data Source=//192.168.1.10//access//CSharptest.mdb";  
  18.   
  19.   
  20.             OleDbConnection objConnection = new OleDbConnection(strConnection);  //建立连接  
  21.             objConnection.Open();  //打开连接  
  22.             OleDbCommand sqlcmd = new OleDbCommand(@"select * from person where personname='John'",objConnection);  //sql语句  
  23.             OleDbDataReader reader = sqlcmd.ExecuteReader();              //执行查询  
  24.             int age = new int();  
  25.            if(reader.Read()){ //这个read调用很重要!不写的话运行时将提示找不到数据  
  26.                 age = (int)reader["age"];   //取得字段的值  
  27.                 objConnection.Close();  
  28.                 reader.Close();  
  29.             }  
  30.   
  31.             Application.EnableVisualStyles();  
  32.             Application.SetCompatibleTextRenderingDefault(false);  
  33.             Form1 form = new Form1();  
  34.             form.Text = age.ToString();  
  35.             Application.Run(form);  
  36.         }  
  37.     }  
  38. }  
using System.Windows.Forms;using System.Data;using System.Data.OleDb;namespace WindowsFormsApplication1{    static class Program    {        /// <summary>        /// 应用程序的主入口点。        /// </summary>        [STAThread]        static void Main()        {            //构造连接字符串           string strConnection="Provider=Microsoft.Jet.OleDb.4.0;";         strConnection +=@"Data Source=//192.168.1.10//access//CSharptest.mdb";            OleDbConnection objConnection = new OleDbConnection(strConnection);  //建立连接            objConnection.Open();  //打开连接            OleDbCommand sqlcmd = new OleDbCommand(@"select * from person where personname='John'",objConnection);  //sql语句            OleDbDataReader reader = sqlcmd.ExecuteReader();              //执行查询            int age = new int();           if(reader.Read()){ //这个read调用很重要!不写的话运行时将提示找不到数据                age = (int)reader["age"];   //取得字段的值                objConnection.Close();                reader.Close();            }            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);            Form1 form = new Form1();            form.Text = age.ToString();            Application.Run(form);        }    }}

           

给我老师的人工智能教程打call!
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值