我在SQL Server 2008中有一個數據庫並將其連接到WPF
應用程序中。我想從表中讀取數據並在datagrid
中顯示。連接成功創建,但當我在網格中顯示時,它顯示數據庫錯誤(異常處理)。 這是我正在做的事。提前感謝。WPF中的SQL Server連接
try
{
SqlConnection thisConnection = new SqlConnection(@"Server=(local);Database=Sample_db;Trusted_Connection=Yes;");
thisConnection.Open();
string Get_Data = "SELECT * FROM emp";
SqlCommand cmd = new SqlCommand(Get_Data);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable("emp");
sda.Fill(dt);
MessageBox.Show("connected");
//dataGrid1.ItemsSource = dt.DefaultView;
}
catch
{
MessageBox.Show("db error");
}
它顯示connected
當我評論線sda.Fill(dt);
嘗試檢索異常錯誤信息:'趕上(例外EX)'。 – Novak
請用'catch(Exception ex){MessageBox.Show(ex.Message)}'替換你的'catch'塊以查看異常細節。 –
您沒有爲該命令分配任何連接。您打開連接然後創建一個命令,但不要鏈接這兩個。 – Lloyd