【发布时间】:2016-11-03 02:45:23
【问题描述】:
我正在尝试将数据读取器中的列读入标签(c#winform) 我的代码如下:
SqlCommand command1 = new SqlCommand("select plant_name,plant_id from plant order by plant_id ", connection);
try
{
connection.Open();
SqlDataReader dr = command1.ExecuteReader();
while (dr.Read())
{
string plantlable = dr.GetInt32("plant_id").ToString();
labelplantid.Text = plantlable.ToString();
comboBoxplant.Items.Add(dr["plant_name"]);
}
dr.Close();
dr.Dispose();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
我在下一行收到错误“Argument 1: cannot convert from 'string' to 'int'”
string plantlable = dr.GetInt32("plant_id").ToString();
带有红色下划线的plant_id。
我做错了什么? 我似乎无法弄清楚。 plant_id 是一个 Int 列类型。 数据库使用 Sql Server 2008。
任何提示将不胜感激。
【问题讨论】:
-
您试图从非数字字符串中获取整数值,这没有意义。
dr["plant_id"]应改为使用。有关GetInt32方法信息,请参阅msdn.microsoft.com/en-us/library/…。 -
谢谢你..这工作..labelplantid.Text= dr["plant_id"].ToString();
标签: c# sql-server ado.net sqldatareader