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

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):无法将参数 1 从“const c
本文介绍了错误 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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)