c# 添加数据库的读取
access 数据库用 英文做关键键名
例如
数据库文件例如:
private void Form1_Load(object sender, EventArgs e)
{
string ConStr = string.Format(//设置数据库连接字符串
@"Provider=Microsoft.Jet.OLEDB.4.0;Data source='{0}\test.mdb'",
Application.StartupPath);
OleDbConnection oleCon = new OleDbConnection(ConStr);//创建数据库连接对象
OleDbDataAdapter oleDap = new OleDbDataAdapter(//创建数据适配器对象
"select * from ZZZ", oleCon);
DataSet ds = new DataSet();//创建数据集
oleDap.Fill(ds, "ZZZ");//填充数据集
this.dataGridView1.DataSource =//设置数据源
ds.Tables[0].DefaultView;
oleCon.Close();//关闭数据库连接
oleCon.Dispose();//释放连接资源
}
读取2 输入数据库密码读取
private void button1_Click(object sender, EventArgs e)
{
string ConStr = string.Format(//设置数据库连接字符串
@"Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:DataBase Password='{0}';
User Id=admin;Data source={1}\test.mdb",
textBox1.Text, Application.StartupPath);
OleDbConnection oleCon = new OleDbConnection(ConStr);//创建数据库连接对象
OleDbDataAdapter oleDap = new OleDbDataAdapter(//创建数据适配器对象
"select * from ZZZ", oleCon);
DataSet ds = new DataSet();//创建数据集
oleDap.Fill(ds, "ZZZ");//填充数据集
this.dataGridView1.DataSource =//设置数据源
ds.Tables[0].DefaultView;
oleCon.Close();//关闭数据库连接
oleCon.Dispose();//释放连接资源
}