private void 商店一_商品_Load(object sender, EventArgs e)
{
sqlConn = new SqlConnection("Data Source=ASUS-PC\\SQLEXPRESSInitial Catalog=SUPERMARKETIntegrated Security=True")
/SqlConnection连接到本地服务器ASUS-PC,数据库SUPERMARKET,使用windows身份验证
sqlDa = new SqlDataAdapter("SELECT * FROM dbo.商店一_商品", sqlConn)
//Sql适配器以从表dbo.商店一_商品选择所有列作为命令,绑定到SqlConnection
sqlDs = new DataSet()//为数据集分配内存
sqlDa.Fill(sqlDs, "dbo.商店一_商品")//适配器填充到数据集中的表"dbo.商店一_商品"
dataGridView1.DataSource = sqlDs.Tables["dbo.商店一_商品"]
//dataGridView1以数据集中的表"dbo.商店一_商品"作为其数据源
SqlCommandBuilder sqlCmdBuilder = new SqlCommandBuilder(sqlDa)
//然后用SqlCommandBuilder自动为SqlDataAdapter生成Insert、Update、Delete命令
}
点button1批量更新到数据库:
private void button1_Click_1(object sender, EventArgs e)
{
if (sqlDs.HasChanges())//如果数据集因我们对datagridview的 *** 作发生改变
{
try//捕获异常
{
sqlDa.Update(sqlDs.Tables["dbo.商店一_商品"])//以数据集的"dbo.商店一_商品"表更新数据库
sqlDs.Tables["dbo.商店一_商品"].AcceptChanges()//接受对数据的修改
MessageBox.Show("更新成功!", " *** 作结果", MessageBoxButtons.OK, MessageBoxIcon.Information)//d出提示更新成功
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "更新失败!", MessageBoxButtons.OK, MessageBoxIcon.Error)
//出现异常提示更新失败
}
}
}
button2删除当前行:
private void button2_Click_1(object sender, EventArgs e)
{ //删除首先要定位到当前选中的记录
int delRowIndex = dataGridView1.CurrentRow.Index
this.dataGridView1.Rows.RemoveAt(delRowIndex)
//然后调用button1更新数据库的方法
button1.PerformClick()
}
下面是删除指定的行并更新到库(实际上原理很简单,就是从数据库中删除选定的行,然后清除dataGridView中的所有数据再执行一次查询就行了):1
2
3
4
5
6
7
8
9
10
11
12
13
14
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
string temp = "delete from qty where PartNum='" + dataGridView1["PartNum",indexrow].Value.ToString() + "'"
if (MSSQL.ExecuteQuery(temp))
{
AddControls a = new AddControls()
a.DeleteDataGridView(dataGridView1)
a.SetDataGridViewData(dataGridView1, str, sqldrr, ddrr)
}
}
catch { }
}
里面MSSQL.ExecuteQuery是这样定义的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/// <summary>
/// 执行sql语句
/// </summary>
/// <param name="cmdString">查询,更新,删除语句</param>
/// <returns>成功返回true,失败返回false</returns>
public static bool ExecuteQuery(string cmdString)
{
MsSql()
SqlConnection conn = new SqlConnection(connectString)
SqlCommand cmd = new SqlCommand(cmdString, conn)
cmd.CommandText = cmdString
try
{
conn.Open()
if (cmd.ExecuteNonQuery() >0)
{
conn.Close()
return true
}
else
{
return false
}
}
catch (SqlException Sqlex)
{
Console.WriteLine(Sqlex.Message)
return false
}
}
#endregion连接数据库显示数据#region 连接数据库显示数据
private void form1_load(object sender, eventargs e)
{
sqlconnection conn = new sqlconnection("server=127.0.0.1database=pubsuid=sa")
sqlcommand scd = new sqlcommand("select * from tables", conn)
sda.selectcommand = scd
sda.fill(dt)
datagridview1.datasource = dt
}
#endregion
使用update更新数据库#region 使用update更新数据库
private void toolstripbutton1_click(object sender, eventargs e)
{
try
{
sqlcommandbuilder scb = new sqlcommandbuilder(sda)
sda.update(dt)
}
catch (system.exception ex)
{
messagebox.show(ex.tostring())
return
}
messagebox.show("更新成功!")
}
#endregion
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)