使用 SqlDataReader 时可能会进行意外的引用比较以获得值比较
possible unintended reference comparison to get a value comparison when using SqlDataReader
对Select某药的company_name
,date_of_mfg
,date_of_exp
,先medicine_name
选出其SelectedIndexchanged
事件,我使用以下代码:
private void combomedicine_SelectedIndexChanged(object sender, EventArgs e)
{
combocompany.Items.Clear();
string str = "select med_name,mnf_name from tbl_mdcnentry";
command.Connection = connection;
command.CommandText = str;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
if (combomedicine.SelectedItem.ToString() == reader.GetValue(0).ToString())
{
combocompany.Items.Add(reader.GetValue(1).ToString());
break;
}
}
reader.Close();
}
调试时,行if (combomedicine.SelectedItem.ToString()
== reader.GetValue(0).ToString())
警告:Possible unintended reference comparison; to get a value comparison, cast the right hand side to type string
(尽管我已经施放了)并且在从下拉列表中选择新项目时不产生任何输出。
我搜索过,到处都建议的解决方案是添加 ToString()
,但仍然无效。
为了比较字符串,我更喜欢使用 string.Equals,您可以提供比较类型,我建议尝试使用 InvariantCultureIgnoreCase 来避免大小写问题。
你有没有调试确认这两个字符串其实是一样的?
此外,请考虑将您的数据访问代码从 UI 移开,从长远来看,这将更易于维护和理解您的代码。
编辑:将 DataReader 封装在 using 块中也是一种更好的做法:
using(SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
if (string.Equals(combomedicine.SelectedItem.ToString(),
reader.GetValue(0).ToString(),
StringComparison.InvariantCultureIgnoreCase)
{
combocompany.Items.Add(reader.GetValue(1).ToString());
break;
}
}
}
这是我在 here.
之后所做的
private void combomedicine_SelectedIndexChanged(object sender, EventArgs e)
{
combocompany.Items.Clear();
GetAutoCombobox_filled();
}
//Helper method
public void GetAutoCombobox_filled()
{
// int select_item;
string str = "select med_name,mnf_name,mfg_date,exp_date from tbl_mdcnentry";
command.Connection = connection;
command.CommandText = str;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
if (combomedicine.SelectedItem.ToString() == (string)reader["med_name"])
{
combocompany.Items.Add(reader.GetValue(1).ToString());
dateTimePicker2.Text=reader.GetValue(2).ToString();
dateTimePicker3.Text = reader.GetValue(3).ToString();
break;
}
}
reader.Close();
}
对Select某药的company_name
,date_of_mfg
,date_of_exp
,先medicine_name
选出其SelectedIndexchanged
事件,我使用以下代码:
private void combomedicine_SelectedIndexChanged(object sender, EventArgs e)
{
combocompany.Items.Clear();
string str = "select med_name,mnf_name from tbl_mdcnentry";
command.Connection = connection;
command.CommandText = str;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
if (combomedicine.SelectedItem.ToString() == reader.GetValue(0).ToString())
{
combocompany.Items.Add(reader.GetValue(1).ToString());
break;
}
}
reader.Close();
}
调试时,行if (combomedicine.SelectedItem.ToString()
== reader.GetValue(0).ToString())
警告:Possible unintended reference comparison; to get a value comparison, cast the right hand side to type string
(尽管我已经施放了)并且在从下拉列表中选择新项目时不产生任何输出。
我搜索过,到处都建议的解决方案是添加 ToString()
,但仍然无效。
为了比较字符串,我更喜欢使用 string.Equals,您可以提供比较类型,我建议尝试使用 InvariantCultureIgnoreCase 来避免大小写问题。
你有没有调试确认这两个字符串其实是一样的?
此外,请考虑将您的数据访问代码从 UI 移开,从长远来看,这将更易于维护和理解您的代码。
编辑:将 DataReader 封装在 using 块中也是一种更好的做法:
using(SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
if (string.Equals(combomedicine.SelectedItem.ToString(),
reader.GetValue(0).ToString(),
StringComparison.InvariantCultureIgnoreCase)
{
combocompany.Items.Add(reader.GetValue(1).ToString());
break;
}
}
}
这是我在 here.
之后所做的 private void combomedicine_SelectedIndexChanged(object sender, EventArgs e)
{
combocompany.Items.Clear();
GetAutoCombobox_filled();
}
//Helper method
public void GetAutoCombobox_filled()
{
// int select_item;
string str = "select med_name,mnf_name,mfg_date,exp_date from tbl_mdcnentry";
command.Connection = connection;
command.CommandText = str;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
if (combomedicine.SelectedItem.ToString() == (string)reader["med_name"])
{
combocompany.Items.Add(reader.GetValue(1).ToString());
dateTimePicker2.Text=reader.GetValue(2).ToString();
dateTimePicker3.Text = reader.GetValue(3).ToString();
break;
}
}
reader.Close();
}