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
问题描述
我已将我的数据库链接到我的项目,并且我正在尝试在数据网格视图中查看我的数据库数据.我的数据库是用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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:错误 C2664:“System::String ^System::Data::Common::DbDataR
基础教程推荐
- SQL Server 2016更改对象所有者 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01