問題描述
我已將我的數據庫鏈接到我的項目,并且我正在嘗試在數據網格視圖中查看我的數據庫數據.我的數據庫是用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"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!