创建数据库 -- 表 -- 绑定到dataGridView1中,
private void btnReg_Click(object sender, EventArgs e)
{
this.sqlDataAdapter1.Update(this.msdbDataSet);
}
private void btnDel_Click(object sender, EventArgs e)
{
string str = (string)dataGridView1.SelectedRows[0].Cells[0].Value;
if (MessageBox.Show("您确定要删除本条信息吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
if (str != "")
{
using (SqlConnection con1 = new SqlConnection("Data Source=(local);database=msdb;Integrated Security=SSPI"))
{
con1.Open();
SqlCommand cmd = new SqlCommand("delete from Class1 where Naem='" + str + "'", con1);
cmd.Connection = con1;
cmd.ExecuteNonQuery();
con1.Close();
foreach (DataGridViewRow r in dataGridView1.SelectedRows)
{
dataGridView1.Rows.Remove(r);
}
}
}
}
}// msdbDataSet 数据集// database = msdb 数据库名为: msdb
// Class1 为表名
// Data Source = (local) 格式为调用本地(本机)数据库
// Integrated Security = SSPI 本机登录模式
本文介绍了如何在C#中创建数据库、表,并将数据绑定到DataGridView1中进行展示及更新、删除操作。

被折叠的 条评论
为什么被折叠?



