新手:c#界面 删除按钮有错 不能删除????
private void button5_Click(object sender, EventArgs e)
{
string b1 = textBox1.Text.ToString();
string b3 = textBox2.Text.ToString();
string b2 = textBox3.Text.ToString();
string b4 = textBox4.Text.ToString();
string b5 = textBox5.Text.ToString();
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
conn = new SqlConnection("Data Source=.;Initial Catalog=基于责任中心的企业成本控制系统;Integrated Security=True");
conn.Open();
string sql = "delete from 用户信息表 where 用户编码='" + b1 + "',部门编号='" + b2 + "',用户名='" + b3 + "',密码='" + b4 + "',职务='" + b5 + "'";
int result = cmd.ExecuteNonQuery();
conn.Close();
if (result > 0)
{
MessageBox.Show("删除成功");
}
else
{
MessageBox.Show("删除失败,请稍候再试");
}
提示“ExecuteNonQuery: Connection 属性尚未初始化。“ 怎么解决????
------解决方案--------------------你怎么改的。。。
------解决方案--------------------你的顺序错了,连接串到前边啊
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=基于责任中心的企业成本控制系统;Integrated Security=True");
conn.Open();
string sql = "delete from 用户信息表 where 用户编码='" + b1 + "',部门编号='" + b2 + "',用户名='" + b3 + "',密码='" + b4 + "',职务='" + b5 + "'";
SqlCommand cmd = new SqlCommand(con,sql);
int result = cmd.ExecuteNonQuery();
conn.Close();
if (result > 0)
{
MessageBox.Show("删除成功");
}
else
{
MessageBox.Show("删除失败,请稍候再试");
}
还有textbox 本来就是string 类型的,没必要在 Tostring() 了。
------解决方案--------------------C# code
SqlConnection conn = new SqlConnection("");//连接字符串
conn.Open();
string sql = ""; // sql语句
SqlCommand cmd = new SqlCommand(sql, conn);
int i = Convert.ToInt32(cmd.ExecuteNonQuery());
if (i > 0)
{
}
else
{
}