SqlDataReader.GetString错误:无效的参数

时间:2015-03-01 18:01:38

标签: c# ado.net

当我运行我的问题时,它会显示以下错误:

  

最佳重载方法匹配' 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");

        }

    }

1 个答案:

答案 0 :(得分:3)

您应该为SqlDataReader.GetString方法提供列序号而不是列名:

var ordinal = dr.GetOrdinal("Name");
while (dr.Read())
{
    string abc = dr.GetString(ordinal);

}