c#项目中怎么使用access数据库 一简单的access数据库,引入到了用vs2010开发的C#项目中
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace UserEnrol
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//定义两个变量,获取文本框的值
string name;
string pwd;
//创建连Access接数据库方法 返回一个命令对象
public OleDbCommand LianJie(string strSql)
{
OleDbCommand cmd;
//access数据库连接语句
string oleDBString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/Exercise/user.mdb";
//创建连接对象
OleDbConnection conn = new OleDbConnection(oleDBString);
conn.Open();
//创建命令对象
cmd = new OleDbCommand(strSql, conn);
return cmd;
}
//修改用户密码
private void btnLogin_Click(object sender, EventArgs e)
{
name = this.userName.Text;
pwd = this.userPwd.Text;
string strSql = string.Format("update userInfo set userPwd={0} where userName='{1}'", pwd, name);
OleDbCommand cmd = LianJie(strSql);
if (cmd.ExecuteNonQuery() != 0)
{
MessageBox.Show("修改成功");
}
else
{
MessageBox.Show("修改失败");
}
}
//注册用户
private void btnEnrol_Click(object sender, EventArgs e)
{
name = this.userName.Text;
pwd = this.userPwd.Text;
//插入数据库语句
string strSql = string.Format("insert into userInfo values('{0}',{1})", name, pwd);
//调用连接Access数据库方法
OleDbCommand cmd = LianJie(strSql);
if (cmd.ExecuteNonQuery() != 0)
{
MessageBox.Show("注册成功");
}
else
{
MessageBox.Show("注册失败");
}
}
}
}
结果如图:我的QQ1037710689,不懂的话,QQ留言