c# winform 实现数据库管理
时间: 2023-07-29 16:07:52 浏览: 130
C# Winform可以通过ADO.NET访问数据库,实现数据库管理。下面提供一个简单的示例,演示如何连接Access数据库、添加数据、查询数据、更新数据、删除数据。
首先,需要在Winform中添加一个DataGridView控件用于显示数据,以及若干个TextBox控件用于输入数据。假设我们要管理的数据库中有一个名为"students"的表,包含4个字段:id、name、gender、age。
连接数据库:
```csharp
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\students.mdb";
OleDbConnection conn = new OleDbConnection(connStr);
conn.Open();
```
添加数据:
```csharp
string sql = "insert into students(name, gender, age) values(@name, @gender, @age)";
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.Parameters.AddWithValue("@name", textBox1.Text);
cmd.Parameters.AddWithValue("@gender", textBox2.Text);
cmd.Parameters.AddWithValue("@age", textBox3.Text);
cmd.ExecuteNonQuery();
```
查询数据:
```csharp
string sql = "select * from students";
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);
DataTable dt = new DataTable();
adapter.Fill(dt);
dataGridView1.DataSource = dt;
```
更新数据:
```csharp
string sql = "update students set name=@name, gender=@gender, age=@age where id=@id";
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.Parameters.AddWithValue("@name", textBox1.Text);
cmd.Parameters.AddWithValue("@gender", textBox2.Text);
cmd.Parameters.AddWithValue("@age", textBox3.Text);
cmd.Parameters.AddWithValue("@id", dataGridView1.CurrentRow.Cells[0].Value.ToString());
cmd.ExecuteNonQuery();
```
删除数据:
```csharp
string sql = "delete from students where id=@id";
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.Parameters.AddWithValue("@id", dataGridView1.CurrentRow.Cells[0].Value.ToString());
cmd.ExecuteNonQuery();
```
完整代码如下:
```csharp
using System.Data.OleDb;
// 连接数据库
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\students.mdb";
OleDbConnection conn = new OleDbConnection(connStr);
conn.Open();
// 添加数据
string sql = "insert into students(name, gender, age) values(@name, @gender, @age)";
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.Parameters.AddWithValue("@name", textBox1.Text);
cmd.Parameters.AddWithValue("@gender", textBox2.Text);
cmd.Parameters.AddWithValue("@age", textBox3.Text);
cmd.ExecuteNonQuery();
// 查询数据
string sql = "select * from students";
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);
DataTable dt = new DataTable();
adapter.Fill(dt);
dataGridView1.DataSource = dt;
// 更新数据
string sql = "update students set name=@name, gender=@gender, age=@age where id=@id";
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.Parameters.AddWithValue("@name", textBox1.Text);
cmd.Parameters.AddWithValue("@gender", textBox2.Text);
cmd.Parameters.AddWithValue("@age", textBox3.Text);
cmd.Parameters.AddWithValue("@id", dataGridView1.CurrentRow.Cells[0].Value.ToString());
cmd.ExecuteNonQuery();
// 删除数据
string sql = "delete from students where id=@id";
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.Parameters.AddWithValue("@id", dataGridView1.CurrentRow.Cells[0].Value.ToString());
cmd.ExecuteNonQuery();
// 关闭连接
conn.Close();
```
阅读全文