C#NULL值允许查询

C#NULL值允许查询

本文介绍了SQL C#NULL值允许查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅以下代码。我的问题是:一旦执行查询,结果将是一个空值或非空值,一旦从查询中识别出一个空值,C#程序就会发生一个错误,因为对象引用未设置为对象的实例。。 />


因此,我不需要上面提到的错误。必需的是,textbox应该能够显示null并且一旦得到记录就应该显示非null值。



Please refer below code. My problem is: once execute the query, result will be a null value or not null value, once identify a null value from query, C# program occurred an error as "Object reference not set to an instance of an object.".

Therefore, I don’t need above mentioned error. Required is, textbox should be able to display the null and once got a record it should be displayed the not null value.

SqlCommand comando56 = new SqlCommand();
string myConnectionString56 = (@"Persist Security Info=True;Password=123;User ID=user;Initial Catalog=TEST;Data Source=192.168.99.99");
SqlConnection conn56 = new SqlConnection(myConnectionString56);
comando56.Connection = conn56;
comando56.CommandText = (@"SELECT ITEMS from TEST where Date='" + textBoxDate.Text + "' and ID='" + textBoxID.Text + "'");
conn56.Open();
textBoxResult.Text = comando56.ExecuteScalar().ToString();
conn56.Close();

推荐答案

SELECT COUNT(items) FROM test WHERE...

应该这样做。





由于意外删除造成的错误:rst为First - OriginalGriff [/ edit]

should do it.


[edit]Typo caused by accidental delete: "rst" for "First" - OriginalGriff[/edit]


var varTemp =comando56.ExecuteScalar();
if (varTemp != null)
{
    textBoxResult.Text=Convert.ToString(varTemp );
}






comando56.CommandText = (@"SELECT ISNULL(ITEMS,'') from TEST where Date='" + textBoxDate.Text + "' and ID='" + textBoxID.Text + "'");


这篇关于SQL C#NULL值允许查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 21:21