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");
}
}
}