【问题标题】:error C2664: 'System::String ^System::Data::Common::DbDataReader::GetString(int)' : cannot convert parameter 1 from 'const char [12]' to 'int'错误 C2664:“System::String ^System::Data::Common::DbDataReader::GetString(int)”:无法将参数 1 从“const char [12]”转换为“int”
【发布时间】:2016-02-28 23:02:38
【问题描述】:

我将我的数据库链接到我的项目,我正在尝试在数据网格视图中查看我的数据库数据。我的数据库是用 sql server 编写的,我的代码是

          SqlCommand^ myCommand = gcnew SqlCommand("SELECT * FROM CSC 289.Customer WHERE Customer_ID = '" + grid + "';", myCon);
          SqlDataReader^ myReader;

            try {
                 myCon->Open();

                  myReader = myCommand -> ExecuteReader();

                  if (myReader -> Read()){
                      String^ ID = myReader -> GetString("Customer_ID");
                      txtID -> Text = ID;
                      /*String^ NameVal = myReader -> GetString("Customer_Name");
                      txtName -> Text = NameVal;
                      String^ PhoneVal = myReader -> GetString("Customer_Phone");
                      txtPhone -> Text = PhoneVal;
                      String^ EmailVal = myReader -> GetString("Customer_Email");
                      txtEmail -> Text = EmailVal;*/
                  }

             }
             catch (Exception^ ex) {
                 MessageBox::Show(ex->Message);
             }
         }

我在尝试运行程序时收到的错误是

错误 C2664: 'System::String ^System::Data::Common::DbDataReader::GetString(int)' : 无法将参数 1 从 'const char [12]' 转换为 'int'

无法使用给定的参数列表调用函数“System::Data::SqlClient::SqlDataReader::GetString” 参数类型是:(const char [12]) 对象类型为:System::Data::SqlClient::SqlDataReader ^

我不确定如何解决我的问题,帮助会很大。

【问题讨论】:

    标签: sql-server visual-c++


    【解决方案1】:

    正如框上所说,GetString 需要一个整数并返回一个字符串。 https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getstring(v=vs.110).aspx

    SqlDataReader.GetString 方法 (Int32)

    这是一篇关于如何在 C++ 中使用数据读取器的好文章 http://www.digitalcoding.com/Code-Snippets/CPP-CLI/C-CLI-Code-Snippet-Get-Data-From-SqlDataReader.html

    您正在寻找的正确语法是 C++

    String^ ID = myReader["Customer_ID"]->ToString();
    

    C#语法

    String ID = myReader["Customer_ID"].ToString();
    

    【讨论】:

    • 感谢您的帮助。我的订单有问题,无法弄清楚。
    猜你喜欢
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多