C#如何根据输入的内容在数据库中删除该数据?

SqlConnection conn;
private void FormDepartmentFilesDelete_Load(object sender, EventArgs e)//窗体
{
conn = new SqlConnection("server=172.16.43.55;database=CateringDB;uid=sa;pwd=123456");//连接数据库
}

private void button1_Click(object sender, EventArgs e)
{
if (Textname.Text == "")
{
MessageBox.Show("部门名称不能为空,请输入部门名称");
}
else
{
conn.Open();//打开数据库连接
SqlCommand cmd = new SqlCommand("delete from tbl_Department_Info where deptName='" + Textname.Text.Trim() + "'", conn);

conn.Close();
}
}

请问空白地方该怎么写?初学,求指点,,,顺便再问问,如果想在删除的时候加一个确认删除框该怎么加?

以下是我给你修改好的代码 你值接替换就可以了,加你你要的删除提示判断,也加了你要的空白部分,还有帮你加了异常捕获,因为数据库操作很容易出现异常!!!如有疑问,欢迎继续追问!!

     private void button1_Click(object sender, EventArgs e)
        {
          
            if (Textname.Text == "")
            {
                MessageBox.Show("部门名称不能为空,请输入部门名称");
            }
            else
            {
                if(MessageBox.Show("您确定要删除该部门吗?","提示",MessageBoxIcon.Question,MessageBoxButtons.OKCancel,) ==DialogResult.Cancel)
                {
                    return;
                }
                    try 
                        {         
                        conn.Open();//打开数据库连接
                                SqlCommand cmd = new SqlCommand("delete from tbl_Department_Info where deptName='" + Textname.Text.Trim() + "'", conn);
                                SqlCommand Cmd = new SqlCommand(StrSql, Conn);
                                int i = Cmd.ExecuteNonQuery();
                                if (i > 0)
                                {
                                    MessageBox.Show("删除成功!");
                                }
                                else 
                                {
                                    MessageBox.Show("删除失败!");
                                }
                                conn.Close();
                        }
                        catch (Exception ex)
                        {
                        MessageBox.Show("删除失败!"+ex.Message);
                        }
                }
        }

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-12-20
public int GetExcute(string StrSql)
{
int i = -1;
using (SqlConnection Conn = new SqlConnection(ConnnectString))
{
Conn.Open();
SqlCommand Cmd = new SqlCommand(StrSql, Conn);
i = Cmd.ExecuteNonQuery();
}
return i;

}
:MessageBox.Show(Text,Title,MessageBoxButtons,MessageBoxIcon ,MessageBoxDefaultButtons) 参数说明: (1)Text:必选项,消息框的正文。 (2)Title:可选项,消息框的标题。 (3)MessageBoxButtons:可选项,消息框的按钮设置,默认只显示【确定】按钮。 OK――确定 OKCancel――确定和取消 AbortRetryIgnore――终止、重试和忽略 YesNoCancel――是、否和取消 YesNo――是和否 RetryCancel――重试和取消 (4)MessageBoxIcon:对话框中显示的图标样式,默认不显示任何图标。 Question――问号 Information、Asterisk――i号 Error、Stop、Hand――错误号 Warning、Exclamation――!号 None――不显示任何图标 (5)MessageBoxDefaultButtons:可选项,对话框中默认选中的按钮设置。 DefaultButton1――第1个button是默认按钮 DefaultButton2――第2个button是默认按钮 DefaultButton3――第3个button是默认按钮 备注:函数原型中蓝色字体部分的参数,可以通过点来获取其后面跟随的参数值。
DialogResult resault=MessageBox.Show("确定关闭么","",MessageBoxButtons.OkCancel,MessageBoxIcons.infomation);
if(result == DialogResult.OK)
{
//确定后的操作
}