错误 C2664:“System::String ^System::Data::Common::DbDataReader

error C2664: #39;System::String ^System::Data::Common::DbDataReader::GetString(int)#39; : cannot convert parameter 1 from #39;const char [12]#39; to #39;int#39;(错误 C2664:“System::String ^System::Data::Common::DbDataReader::GetString(int)
本文介绍了错误 C2664:“System::String ^System::Data::Common::DbDataReader::GetString(int)":无法将参数 1 从“const char [12]"转换为“int"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I have my database linked to my project and I am trying to view my database data in a data grid view. My database is written in sql server and My code is

          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);
             }
         }

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

The errors I receive when I try and run my program are

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

error C2664: 'System::String ^System::Data::Common::DbDataReader::GetString(int)' : cannot convert parameter 1 from 'const char [12]' to 'int'

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

function "System::Data::SqlClient::SqlDataReader::GetString" cannot be called with the given argument list argument types are: (const char [12]) object type is: System::Data::SqlClient::SqlDataReader ^

我不知道如何解决我的问题,如果能帮到我就好了.

I am not sure how to fix my problem and help would be great.

推荐答案

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

As it says on the box GetString expects an Integer and returns a string. https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getstring(v=vs.110).aspx

SqlDataReader.GetString 方法 (Int32)

SqlDataReader.GetString Method (Int32)

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

here is a good article on how to use a data reader in c++ http://www.digitalcoding.com/Code-Snippets/CPP-CLI/C-CLI-Code-Snippet-Get-Data-From-SqlDataReader.html

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

Correct syntax that you are looking for is C++

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

C# 语法

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

这篇关于错误 C2664:“System::String ^System::Data::Common::DbDataReader::GetString(int)":无法将参数 1 从“const char [12]"转换为“int"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Sending parameters to stored procedures vb.net(将参数发送到存储过程 vb.net)
Insert multiple rows, count based on another table columns(插入多行,根据另一个表列计数)
How to hold the location of an image in a SQL Server database?(如何在 SQL Server 数据库中保存图像的位置?)
How to send to email (outlook) the selected items in SQL Server database using vb.net(如何使用 vb.net 将 SQL Server 数据库中的选定项目发送到电子邮件(Outlook))
datagrid checkbox writes Null instead of 0 (false) into database(datagrid 复选框将 Null 而不是 0 (false) 写入数据库)
DBCC CheckDb-any ways to detect errors vb.net?(DBCC CheckDb-vb.net 有什么检测错误的方法吗?)