1

我是 WPF 的新手。我收到以下错误:“System.Data.Common.DbDataReader.GetInt32 的最佳重载消息匹配有一些无效参数”

我的代码如下:

private void comboBoxDisplay_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        string constring = "Data Source=tcp:******.database.windows.net;Initial        Catalog=*****;Persist Security Info=True;User ID=*****;Password=******";

        string Query = "select * from Rewards where Name='" + comboBoxDisplay.Text + "' ;";
        SqlConnection conDataBase = new SqlConnection(constring);
        SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
        SqlDataReader myReader;

        try
        {
            conDataBase.Open();
            myReader = cmdDataBase.ExecuteReader();

            while (myReader.Read())
            {

                 string sReid = myReader.GetInt32(0).ToString();
                string sName = myReader.GetString(1);



            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }`
4

2 回答 2

1

GetXXX 方法使用列号,而不是名称。如果你调用了正确的方法,你也不应该需要强制转换。试试这个

while (myReader.Read())
        {

            string sReid = myReader.GetString(myReader.GetOrdinal("reID"));
            string sName = myReader.GetString(myReader.GetOrdinal("Name"));


        }
于 2013-07-29T20:11:02.550 回答
0

System.Data.Common.DbDataReader.GetInt32 方法需要一个整数,它是列的序号。没有将字符串作为参数的重载方法。

myReader.GetInt32(2);  // gets the 3rd column  (zero based)
于 2013-07-29T20:10:27.720 回答