C#连接Access数据库

一、使用相对路径

public static string Path = Application.StartupPath.Substring(0, Application .StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\" )).LastIndexOf("\\"));

public static string M_str_sqlcon = "Provider = Microsoft.jet.OLEDB.4.0;Data Source=" +  Path + @"\db\db_MS.mdb" ;
地址为项目主目录的地址

My_con = new OleDbConnection(M_str_sqlcon);   //用SqlConnection对象与指定的数据库相连接

二、使用绝对路径

public static string M_str_sqlcon = "Provider = Microsoft.jet.OLEDB.4.0;Data Source=E:\\db_MS.mdb";

My_con = new OleDbConnection(M_str_sqlcon);   //用SqlConnection对象与指定的数据库相连接

三、链接access完整代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Data.OleDb;
using System.Text;
using System.Windows.Forms;
 
namespace Location
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Location System";
            button1.Text = "连接数据库";
            button2.Text = "查询";
            button3.Text = "退出";
            button4.Text = "添加";
            button5.Text = "删除";
            button6.Text = "修改";
            label1.Text = "ID:";
            textBox1.Text = "0";
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;data source=Location.mdb";//创建OleDbConnection对象
            OleDbConnection con = new OleDbConnection(ConStr);
            con.Open();
            if (con.State == ConnectionState.Open)
            {
                MessageBox.Show("Access数据库的连接成功!", "Access数据库的连接");
            }
            else
            {
                MessageBox.Show("Access数据库的连接失败!", "Access数据库的连接");
            }
            con.Close();
           
        }
 
        private void button3_Click(object sender, EventArgs e)              //退出
        {
            this.Close();
        }
 
        private void button2_Click(object sender, EventArgs e)              //查询模块
        {
            string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;data source=Location.mdb";//创建OleDbConnection对象
            OleDbConnection con = new OleDbConnection(ConStr);
            con.Open();
            int i = Convert.ToInt16(textBox1 .Text);
            OleDbCommand cmd = new OleDbCommand("Select * From data where ID>=@id", con);
            cmd.Parameters.Add("@id",i);
            OleDbDataReader reader = cmd.ExecuteReader();
            reader.Read();
            
            //textBox1.Text = reader[0].ToString();
            textBox2.Text = reader[1].ToString();
            textBox3.Text = reader[2].ToString();
            textBox4.Text = reader[3].ToString();
            textBox5.Text = reader[4].ToString();
            textBox6.Text = reader[5].ToString();
            textBox7.Text = reader[6].ToString();
 
            reader.Close();
            con.Close();
        }
 
        private void button4_Click(object sender, EventArgs e)              //添加
        {
            string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;data source=Location.mdb";//创建OleDbConnection对象
            OleDbConnection con = new OleDbConnection(ConStr);
            con.Open();
 
            for (int i = 0; i < 1000; i++)
            {
                string sql = "insert into data(ID)values(" + i + ")";
                OleDbCommand cmd = new OleDbCommand(sql, con);
                cmd.ExecuteNonQuery();
            }
                       
            con.Close();
        }
 
        private void button5_Click(object sender, EventArgs e)               //删除
        {
            string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;data source=Location.mdb";//创建OleDbConnection对象
            OleDbConnection con = new OleDbConnection(ConStr);
            con.Open();
            OleDbCommand cmd = new OleDbCommand("delete from data", con);
            cmd.ExecuteNonQuery();
        }
 
        private void button6_Click(object sender, EventArgs e)              //修改
        {
            string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;data source=Location.mdb";//创建OleDbConnection对象
            OleDbConnection con = new OleDbConnection(ConStr);
            con.Open();
            string sql = "update data set longitude=12 where ID=1";
            OleDbCommand cmd = new OleDbCommand(sql, con);
            cmd.ExecuteNonQuery();
        }
    }
}
 

C#连接Access程序代码:

  1. using System.Data;  

  2. using System.Data.OleDb;  

  3. ..  

  4.  

  5. string strConnection="Provider=Microsoft.Jet.OleDb.4.0;";  

  6. strConnection+=@"Data Source=C:BegASPNETNorthwind.mdb";  

  7.  

  8. OleDbConnection objConnection=new OleDbConnection(strConnection);  

  9. ..  

  10.  

  11. objConnection.Open();  

  12. objConnection.Close(); 

解释:

C#连接Access数据库需要导入额外的命名空间,所以有了最前面的两条using命令,这是必不可少的。strConnection这个变量里存放的是连接数据库所需要的连接字符串,他指定了要使用的数据提供者和要使用的数据源."Provider=Microsoft.Jet.OleDb.4.0;"是指数据提供者,这里使用的是Microsoft Jet引擎,也就是Access中的数据引擎,asp.net就是靠这个和Access的数据库连接的."Data Source=C:\BegASPNET\Northwind.mdb"是指明数据源的位置,他的标准形式是"Data Source=MyDrive:MyPath\MyFile.MDB".

PS:

1."+="后面的"@"符号是防止将后面字符串中的"\"解析为转义字符.

2.如果要连接的数据库文件和当前文件在同一个目录下,还可以使用如下的方法连接:

◆strConnection+="Data Source=";

◆strConnection+=MapPath("Northwind.mdb");

这样就可以省得你写一大堆东西了!

3.要注意连接字符串中的参数之间要用分号来分隔.

◆"OleDbConnection objConnection=new OleDbConnection(strConnection);"

这一句是利用定义好的连接字符串来建立了一个链接对象,以后对数据库的操作我们都要和这个对象打交道.

◆"objConnection.Open();"这用来打开连接.至此,C#连接Access数据库完成.

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下步骤来连接 Access 数据库: 1. 引用 COM 组件:在 Visual Studio 中,右键单击“引用”文件夹,选择“添加引用”,在“COM”选项卡中选择“Microsoft Office 16.0 Access Database Engine Object Library”(如果您的 Access 版本不同,则可能会有所不同)。 2. 添加命名空间:在代码文件的顶部添加以下命名空间: ``` using System.Data.OleDb; ``` 3. 设置连接字符串:在代码中设置连接字符串,示例代码如下: ``` string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb"; ``` 其中,“myFolder”是你的 Access 文件所在的文件夹,而“myAccessFile.accdb”是你的 Access 文件的名称。 4. 创建连接对象:使用连接字符串创建 OleDbConnection 对象,示例代码如下: ``` OleDbConnection connection = new OleDbConnection(connectionString); ``` 5. 打开连接:使用 Open() 方法打开连接,示例代码如下: ``` connection.Open(); ``` 6. 执行查询:使用 OleDbCommand 对象来执行查询,示例代码如下: ``` OleDbCommand command = new OleDbCommand("SELECT * FROM myTable", connection); OleDbDataReader reader = command.ExecuteReader(); ``` 其中,“myTable”是你要查询的表的名称。 7. 关闭连接:在使用完连接和数据阅读器之后,使用 Close() 方法关闭连接,示例代码如下: ``` reader.Close(); connection.Close(); ``` 这样,你就可以使用 C# 连接 Access 数据库了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值