一、使用相对路径
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程序代码:
-
using System.Data;
-
using System.Data.OleDb;
-
..
-
-
string strConnection="Provider=Microsoft.Jet.OleDb.4.0;";
-
strConnection+=@"Data Source=C:BegASPNETNorthwind.mdb";
-
-
OleDbConnection objConnection=new OleDbConnection(strConnection);
-
..
-
-
objConnection.Open();
-
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数据库完成.