添加引用
using System.Data.SqlClient;
[CODE] private void toolBar1_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
{
string sqlstr = "";
switch (e.Button.ImageIndex)
{
case 0: //添加
sqlstr = "insert into table1 values(.....)";
break;
case 1: //修改
sqlstr = "update table1 set 字段=新值 where ....";
break;
case 2: //删除
sqlstr = "delete table1 where .....";
break;
}
dataSet(sqlstr); //执行操作
}[/CODE]
自定义方法 [CODE]void dataSet(string sqlstr)
{
SqlConnection myconn = new SqlConnection(数据库连接语句); //建立连接
SqlCommand cmd = new SqlCommand(sqlstr, myconn); //定义命令
try
{
myconn.Open(); //打开连接
cmd.ExecuteNonQuery(); //执行数据操作
MessageBox.Show("操作成功~!", "系统提示:");
}
catch (Exception expSql)
{
MessageBox.Show(expSql.ToString(), "出错啦!"); //显示错误
}
finally
{
myconn.Close(); //关闭连接
myconn.Dispose(); //释放资源
cmd.Dispose(); //释放资源
}
}[/CODE]