C#连接ACCESS

OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
cmd.ExecuteNonQuery();
}
}
private void button2_Click(object sender, EventArgs e)
{
//删除记录按钮代码
using (OleDbConnection conn = new OleDbConnection(txtConn))
{
string sql = string.Format(delete from table1 where xm={0}, textBox1.Text);
OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
cmd.ExecuteNonQuery();
}
}
private void button3_Click(object sender, EventArgs e)
{
//添加记录按钮代码
using (OleDbConnection conn = new OleDbConnection(txtConn))
{
string sql = string.Format(insert into table1(xm,fl,lxfs,dzyj) values({0},{1},{2},{3}), textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
cmd.ExecuteNonQuery();
}
}
}
C#连接ACCESS

ADO.NET 最基本的数据库增删改查。
几个注意点:
1.新建一个Access数据库,数据库名:access.mdb表名:table1 字段:xm ,fl ,lxfs ,dzyj 再加一个自动编号主键。
2.数据access.mdb 放在D盘根目录下。
3.以上2点是必须的,否则连接不上数据库。
4.我QQ136492244 。一会我发你邮箱一份完整的。
namespace access
{
public partial class Form1 : Form
{
//定义数据库的连接路径
string txtConn =Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\access.mdb; 
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//修改记录按钮代码
using(OleDbConnection conn = new OleDbConnection(txtConn))
{
string sql = string.Format(update table1 set fl={1},lxfs={2},dzyj={3} where xm={0}, textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
}

猜你喜欢

转载自blog.csdn.net/dyxcome/article/details/82120549