问题描述
我已将我的数据库链接到我的项目,并且我正在尝试在数据网格视图中查看我的数据库数据.我的数据库是用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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!