检查c#中查询结果是否为null

时间:2016-04-06 09:53:25

标签: c# mysql

您好我正在尝试创建一个验证,检查它是否是一个不成功的SQL查询 - 如果没有返回结果,它应该捕获此错误并在屏幕上显示一条消息'找不到客户端ID的记录:[数字]'< / p>

我目前提取信息的查询如下。

protected void ClientSearchBtn_Click(object sender, EventArgs e)
        {
            //string ClientID = ClientIDTxt.Text;

            //Database connection. Calls from web.config.
            string MyConnectionString = ConfigurationManager.ConnectionStrings
                    ["RCADSCONNECTION"].ConnectionString;

            //SQL Connection
            SqlConnection myConnection = new SqlConnection();
            myConnection.ConnectionString = MyConnectionString;
            //myConnection.Open();

            //SQL string
            try
            {
                SqlCommand cmd = new SqlCommand("SELECT CN.ClientID, CI.NNN, CN.GivenName1, CN.Surname, CI.DateOfBirth, CI.Gender FROM [RioOds].dbo.ClientIndex CI LEFT JOIN [RioOds].[dbo].[ClientName] CN ON CN.ClientID = CI.ClientID AND CN.AliasType = '1' AND CN.EndDate IS NULL WHERE CN.ClientID = @clientid", myConnection);
                cmd.Parameters.Add("@clientid", SqlDbType.Int).Value = ClientIDTxt.Text;
                myConnection.Open();

                var reader = cmd.ExecuteReader();
                StringBuilder sb = new StringBuilder();

                while (reader.Read())
                {
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        if (i != 0)
                        {
                            sb.Append(" | ");
                        }
                        sb.Append(reader[i].ToString());
                    }
                    sb.AppendLine();

                    ClientIDCell.Text = reader[0].ToString();
                    NNNCell.Text = reader[1].ToString();
                    FirstNameCell.Text = reader[2].ToString();
                    SurnameCell.Text = reader[3].ToString();
                    DobCell.Text = reader[4].ToString();
                    GenderCell.Text = reader[5].ToString();
                }


                //Show the results table
                queryResultsTable.Visible = true;

                ResultsLabel.Text = sb.ToString();

                submitButton.Enabled = true;
                resultsButton.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                myConnection.Close();
            }

            myConnection.Close();
        }

我不确定如何做到这一点。我明白这将是一个if语句但不确定如何将sql查询返回与null进行比较。

1 个答案:

答案 0 :(得分:0)

如果要读取更多行,

reader.Read()将返回true。第一次只有在它是假的时候才意味着读者没有数据。

         if(!reader.Read())
         //Your message 
         else
         {
            do
            {
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    if (i != 0)
                    {
                        sb.Append(" | ");
                    }
                    sb.Append(reader[i].ToString());
                }
                sb.AppendLine();

                ClientIDCell.Text = reader[0].ToString();
                NNNCell.Text = reader[1].ToString();
                FirstNameCell.Text = reader[2].ToString();
                SurnameCell.Text = reader[3].ToString();
                DobCell.Text = reader[4].ToString();
                GenderCell.Text = reader[5].ToString();
            } while (reader.Read());
        }

另一种选择是检查阅读器上的属性HasRows。当有数据时,它就是真的。

if(!reader.HasRows)
  //Your error message goes from here
else
 //Do your stuff