一、使用相对路径
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();
}
}
}