C#通过Datagridview来删除肯更新SQL数据库
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Update_SQL
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SqlConnection con = null;
private DataSet ds = new DataSet();
private SqlDataAdapter myda = null;
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=localhost;database=AlumManager;user=sa;pwd=sql";
string selectSQL = "select * from test ";
SqlDataAdapter myda = new SqlDataAdapter(selectSQL, con);
myda.Fill(ds,"test");
this.dataGridView1.DataSource = ds.Tables["test"];
}
private void Update_btn_Click(object sender, EventArgs e)
{
SqlCommandBuilder SCB = new SqlCommandBuilder(myda);
myda.Update(ds, "test");
}
private void Del_btn_Click(object sender, EventArgs e)
{
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentCell.RowIndex);
SqlCommandBuilder SCB = new SqlCommandBuilder(myda);
myda.Update(ds, "test");
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Update_SQL
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SqlConnection con = null;
private DataSet ds = new DataSet();
private SqlDataAdapter myda = null;
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=localhost;database=AlumManager;user=sa;pwd=sql";
string selectSQL = "select * from test ";
SqlDataAdapter myda = new SqlDataAdapter(selectSQL, con);
myda.Fill(ds,"test");
this.dataGridView1.DataSource = ds.Tables["test"];
}
private void Update_btn_Click(object sender, EventArgs e)
{
SqlCommandBuilder SCB = new SqlCommandBuilder(myda);
myda.Update(ds, "test");
}
private void Del_btn_Click(object sender, EventArgs e)
{
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentCell.RowIndex);
SqlCommandBuilder SCB = new SqlCommandBuilder(myda);
myda.Update(ds, "test");
}
}
}
...全文
相关推荐
C#访问SQL显示到DataGridView,同时根据DataGridView实时更新数据库
本教程将详细讲解如何使用C#来访问SQL数据库,将数据展示在DataGridView控件中,并实现双向同步,即当用户在DataGridView中做出更改时,这些更改能够实时反映到数据库中。
c# 如何删除datagridview中数据并删除数据库中的数据
此过程涉及到了解DataGridView控件的基本操作、SQL命令执行以及与数据库交互等关键技术点。### 标题解析:C# 如何删除DataGridView中数据并删除数据库中的数据#### 1.
C#对DataGridView进行添加、修改、删除数据操作
### C# 对 DataGridView 进行添加、修改、删除数据操作在C#应用程序开发中,尤其是在涉及用户界面的项目中,DataGridView 控件被广泛应用于显示和管理数据表格。
c# 中datagridView中删除行的经典操作
#### 二、与数据库交互时删除行的方法当 `DataGridView` 控件绑定到一个数据源(例如 SQL 数据库)时,我们需要更复杂的逻辑来确保数据库中的数据也被正确更新。
C# 数据库增删查改及DataGridView刷新数据
CRUD是创建(Create)、读取(Read)、更新(Update)和删除(Delete)的首字母缩写,是数据库管理的基础。


