[英]Convert from string to int C# Windows Form
我正在使用 C# Windows Form 应用程序,我想创建带有两个组合框的搜索表单,其中包含两个字段 -Genres -Author Genres 和 Author 需要从数据库加载,我创建了一个加载 Genres 和 Author 的函数,但是当我从数据库加载流派时出现错误“无法从字符串转换为整数”
我试试
string Genres = dr.GetString("Genres");
this.Genres.Items.Add(Genres);
void FillDropDown()
{
string constring = "Data Source=(localdb)\\mssqllocaldb;Initial Catalog=Libary;Integrated Security=True";
string Query = "Select * from Book ";
SqlConnection conDataBase = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand(Query, conDataBase);
SqlDataReader dr;
try
{
conDataBase.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
string Genres = dr.GetString("Genres");
this.Genres.Items.Add(Genres);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
GetString
采用整数索引,而不是字符串列名称。 使用GetOrdinal
查找给定列名的索引。
string Genres = dr.GetString(dr.GetOrdinal("Genres"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.