请写出完整的C#WinForm数据库操作代码

用事件写
用类写不好理解

//按键事件
private void button1_Click_1(object sender, EventArgs e)
{
try
{
SqlConnection scon = new SqlConnection("server=.;uid=sa;pwd=;database=ZSW");
scon.Open();
SqlCommand scom = new SqlCommand();
scom.Connection = scon;
scom.CommandText = "insert into UserInfo Values('" + txt1.Text + "','" + txt2.Text + "','" + txt3.Text + "','" + txt4.Text + "','" + txt5.Text + "')";
int count = scom.ExecuteNonQuery();

if (count > 0)
{
MessageBox.Show("添加成功!");
this.Close();
}
else
{
MessageBox.Show("添加失败!");
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString());
}
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答
大家正在搜