选择查询以从显示空值的表中选择单个值。 [英] select query for selecting single value from table showing null value.

查看:70
本文介绍了选择查询以从显示空值的表中选择单个值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mycnOptionPanel.Open();
  mycmdOptionPanel = mycnOptionPanel.CreateCommand();             

  mycmdOptionPanel.CommandText = "select Device_ID from device_info where      Device_name=@Device_name";
                    mycmdOptionPanel.Parameters.Add("@Device_name",LabelDeviceName.Text);
                  
 string strDeviceID = mycmdOptionPanel.ExecuteScalar().ToString();





我想从mysql数据库中的表中选择单值到strDeviceID变量,最后一行strDeviceID变量包含null,为什么会发生这种情况?是我的代码错了吗?

plz help。



I want to select single value from table in database of mysql into strDeviceID variable, on last line strDeviceID variable contain null,why this is happend? is my code is wrong?
plz help.

推荐答案

一个问题是SqlParameterCollection.Add Method(String,Object) [ ^ ]已过时。因此,请使用 AddWithValue [ ^ ]。换句话说

One problem is that the SqlParameterCollection.Add Method (String, Object)[^] is obsolete. Because of this, use AddWithValue[^] instead. In other words
...
mycmdOptionPanel.Parameters.AddWithValue("@Device_name",LabelDeviceName.Text);
...



另请注意,参数值必须与数据库中的值完全匹配。如果您有区分大小写的数据库,那么即使是小写和大写字母也会影响结果。如果ExecuteScalar没有返回任何行,则返回值为null,从而导致异常。


Also note that the parameter value must be an exact match with the value in the database. If you have case-sensitive database then even small and capital letters affect the result. If the ExecuteScalar returns no rows, the return value is null thus resulting into an exception.


mycmdOptionPanel.CommandText =从device_info中选择Device_ID,其中device_info.Device_name ='+ LabelDeviceName.Text + ';

// mycmdOptionPanel.Parameters.AddWithValue(@ device,LabelDeviceName.Text);



sting strDeviceID = mycmdOptionPanel.ExecuteScalar()。ToString();



i只是注释了粗线,并直接用addwithparameter及其工作指定了值。
mycmdOptionPanel.CommandText = "select Device_ID from device_info where device_info.Device_name='"+ LabelDeviceName.Text + "'";
// mycmdOptionPanel.Parameters.AddWithValue("@device",LabelDeviceName.Text);

sting strDeviceID=mycmdOptionPanel.ExecuteScalar().ToString() ;

i just commented bold lines and specifies value directly witout addwithparameter and its work.


这篇关于选择查询以从显示空值的表中选择单个值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆