当我运行我的问题时,它会显示以下错误:
最佳重载方法匹配' System.Data.Common.DbDataReader.GetString(int)'有一些无效的论点。
我的代码:
SqlConnection con = new SqlConnection(ConnectionClass.connectionclass);
SqlCommand com;
SqlDataReader dr;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
con.Open();
string query = "SELECT * FROM student WHERE Name = '"+comboBox1.Text+"'";
com = new SqlCommand(query, con);
dr = com.ExecuteReader();
while (dr.Read())
{
string abc = dr.GetString("Name");
}
}
答案 0 :(得分:3)
您应该为SqlDataReader.GetString
方法提供列序号而不是列名:
var ordinal = dr.GetOrdinal("Name");
while (dr.Read())
{
string abc = dr.GetString(ordinal);
}