C# 如果在此代码中找不到记录,如何添加异常错误

C# 如果在此代码中找不到记录,如何添加异常错误,c#,sql,windows-forms-designer,C#,Sql,Windows Forms Designer,大家好,正如标题所说,如果在下面的代码中找不到这样的记录,我将如何应用错误 private void button1_Click(object sender, EventArgs e) { SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "select * from CustTicket where Cu

大家好,正如标题所说,如果在下面的代码中找不到这样的记录,我将如何应用错误

    private void button1_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select * from CustTicket where CustmerContactNumber ='" + tbCcontact.Text.ToString() + "'";
        db.ExeNonQuery(cmd);

        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);

        foreach (DataRow dr in dt.Rows)
        {
            tbTest.Text = dr["CustmerName"].ToString();
        }
     }
你是说

if (dt.Rows.Count == 0 )
{
    // Here you can throw new Exception(); or something like that
    MessageBox.Show("Some text", "Some title",  MessageBoxButtons.OK, MessageBoxIcon.Error);
}
你是说

if (dt.Rows.Count == 0 )
{
    // Here you can throw new Exception(); or something like that
    MessageBox.Show("Some text", "Some title",  MessageBoxButtons.OK, MessageBoxIcon.Error);
}

你想要实现什么?是否向用户显示消息框?基本上,您必须检查类似于
dt.Rows.Count==0的条件
您所说的“应用错误”是什么意思?抛出异常?啊,是的,因为查询是由原始用户输入组成的,所以旧的“脚本安全”免责声明“抱歉”意味着显示一个错误*@DharyAlmousa您解决问题了吗?如果是,请接受答案。提前谢谢你你想实现什么?是否向用户显示消息框?基本上,您必须检查类似于
dt.Rows.Count==0的条件
您所说的“应用错误”是什么意思?抛出异常?啊,是的,因为查询是由原始用户输入组成的,所以旧的“脚本安全”免责声明“抱歉”意味着显示一个错误*@DharyAlmousa您解决问题了吗?如果是,请接受答案。先谢谢你