2016-02-29 91 views
0

我正在嘗試使用DataGridView將我的數據庫數據顯示到我的表單中。從SQL Server 2012中填充DataGridView

當我在我的代碼運行調試器,它擊中

da -> Fill(data); 

線它拋出一個異常後。我以前使用過類似的代碼,但數據庫是在MySQL中,而不是SQL服務器。我不確定我做錯了什麼。下面是我使用

SqlCommand^ myCommand = gcnew SqlCommand("SELECT * FROM MyDatabase ;", myCon); 

try{ 
    SqlDataAdapter^da = gcnew SqlDataAdapter(); 
    da -> SelectCommand = myCommand; 
    DataTable^ data = gcnew DataTable(); 
    da -> Fill(data); 
    BindingSource^ bSource = gcnew BindingSource(); 

    bSource -> DataSource = data; 
    dataCustomer -> DataSource = bSource; 
    da -> Update(data); 
} 
catch(Exception^ex){ 
    MessageBox::Show("Broke"); 
} 
+0

什麼是例外?同樣在SQL Server中,您傾向於查詢表,而不是SQL Server所說的包含表的數據庫。 – cameront

+0

引發的異常是「289」附近的語法錯誤。我是SQL Server的新手,並將數據庫鏈接到前端項目。所以我想我只是試圖在DataGridView中顯示單個表數據。在使用MYSQL之前,我已經完成了這個操作,並且我使用了有關代碼的細微變化。 – Doe

+0

這與您在另一個線程上提出的其他查詢有關 - 「SELECT * FROM CSC 289.Customer WHERE Customer_ID ='」 – cameront

回答

0

這是從在這之前你在另一個線程上提出了與你的其他查詢代碼error C2664: 'System::String ^System::Data::Common::DbDataReader::GetString(int)' : cannot convert parameter 1 from 'const char [12]' to 'int'

"SELECT * FROM CSC 289.Customer WHERE Customer_ID = '" 

這是不是一個有效的TSQL語句,你可以」模式名稱中沒有空格。我想你想的東西像

SELECT * FROM [CSC 289].Customer 

SELECT * FROM [CSC 289].dbo.Customer 

真的取決於你的數據庫結構。

+0

噢好的。當我創建數據庫時,我並不感謝間距。所以我只需要把CSC 289放在[]中。非常感謝,現在效果很好。 – Doe