2013-06-18 125 views
2

我在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);

+0

嘗試檢索異常錯誤信息:'趕上(例外EX)'。 – Novak

+0

請用'catch(Exception ex){MessageBox.Show(ex.Message)}'替換你的'catch'塊以查看異常細節。 –

+1

您沒有爲該命令分配任何連接。您打開連接然後創建一個命令,但不要鏈接這兩個。 – Lloyd

回答

6

SqlCommand不知道你打開連接 - 它需要的SqlConnection一個實例。

try 
{ 
     SqlConnection thisConnection = new SqlConnection(@"Server=(local);Database=Sample_db;Trusted_Connection=Yes;"); 
     thisConnection.Open(); 

     string Get_Data = "SELECT * FROM emp";  

     SqlCommand cmd = thisConnection.CreateCommand(); 
     cmd.CommandText = Get_Data; 

     SqlDataAdapter sda = new SqlDataAdapter(cmd);    
     DataTable dt = new DataTable("emp"); 
     sda.Fill(dt); 

     dataGrid1.ItemsSource = dt.DefaultView;   
} 
catch 
{ 
     MessageBox.Show("db error"); 
} 
+0

或使用'SqlCommand'的重載來傳遞'SqlConnection'對象。 –

0

你不指定命令的任何連接。您打開連接然後創建一個命令,但不要鏈接這兩個。

試着這麼做:

SqlConnection conn = new SqlConnection(@"Server(local);Database=Sample_db;Trusted_Connection=Yes;"); 
conn.Open(); 

string sql= "SELECT * FROM emp";  

SqlCommand cmd = new SqlCommand(sql); 
cmd.Connection = conn; 

SqlDataAdapter sda = new SqlDataAdapter(cmd);    
DataTable dt = new DataTable("emp"); 
sda.Fill(dt); 
+0

謝謝,它的工作原理,但可以顯示我的數據grid.I有三個記錄,這裏顯示了一些行。 – EHS

+0

我知道,但我需要解決這個問題。在SQl服務器中,它顯示我的記錄。但爲什麼不在這裏? – EHS

+0

如果您使用了Lloyd所做的確切示例,請確保您設置了'dataGrid1'的'ItemSource'。 – Novak

0

將連接對象分配給SqlCommand對象。

using (SqlConnection connection = new SqlConnection(
      connectionString)) 
{ 
    SqlCommand command = new SqlCommand(); 
    command.Connection = connection; 
    command.CommandTimeout = 15; 
    command.CommandType = CommandType.Text; 
    command.CommandText = queryString; 

    connection.Open(); 
    //Perfom desired Action 

} 
0

添加thisConnection作爲第二個參數中

Sqlcommand cmd = new SqlCommand(Get_Data, thisConnection) 


try { 
    string connectionstring = "@" 
    Server = (local) Database = Sample_dbTrusted_Connection = Yes; 
    ""; 
    SqlConnection thisConnection = new SqlConnection(connectionstring); 
    thisConnection.Open(); 

    string Get_Data = "SELECT * FROM emp"; 

    SqlCommand cmd = new SqlCommand(Get_Data, thisConnection); 
    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"); 
} 
0

我用這個代碼與甲骨文,並希望它會幫助你。

首先添加引用Oracle.DataAccess然後使用Oracle.DataAccess.Client添加命名空間;

並使用下面的代碼

try 
{ 
    string MyConString = "Data Source=localhost;User Id= yourusername;Password=yourpassword;"; 
    using (OracleConnection connection = new OracleConnection(MyConString)) 
    { 
    connection.Open(); 
    sqldb1 = "select * from DEMO_CUSTOMERS;"; 
    using (OracleCommand cmdSe1 = new OracleCommand(sqldb1, connection)) 
    { 
    DataTable dt = new DataTable(); 
    OracleDataAdapter da = new OracleDataAdapter(cmdSe1); 
    da.Fill(dt); 
    db1.ItemsSource = dt.DefaultView; 
    } 
    connection.Close(); 
} 
catch (Exception ex) 
{ 
    MessageBox.Show(ex.ToString()); 
} 

XAML代碼:

<DockPanel> 
    <DataGrid Margin="10.0" DockPanel.Dock="Left" Name="db1" AutoGenerateColumns="True" > 
    </DataGrid> 
</DockPanel> 
0
public DataTable Execute(string cmd) 
{ 
     bool networkUp = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable(); 
     if (networkUp) 
     { 
      try 
      { 
        SqlConnection connection = new SqlConnection("ConnectionString"); 
        SqlCommand command = new SqlCommand(cmd); 
        using (SqlDataAdapter sda = new SqlDataAdapter()) 
        { 
         DataTable dt = new DataTable(); 
         sda.SelectCommand = command; 
         command.Connection = connection; 
         connection.Open(); 
         sda.Fill(dt); 
         connection.Close(); 
         if (dt != null && dt.Columns.Count > 0) 
          return dt; 
         else 
          return null; 
        } 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message); 
       return null; 
      } 
     } 
     else 
     { 
      Console.WriteLine("Network is disconnect"); 
      return null; 
     } 
    return null; 
} 

或:

public void ExecuteNonQuery(string cmd) 
     { 
      bool networkUp = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable(); 
      if (networkUp) 
      { 
       try 
       { 
        SqlConnection connection = new SqlConnection("ConnectionString"); 
        SqlCommand command = new SqlCommand(cmd); 
        command.Connection = connection; 
        connection.Open(); 
        command.ExecuteNonQuery(); 
        connection.Close(); 
       } 
       catch (Exception ex) 
       { 
        Console.WriteLine(ex.Message); 
       } 
      } 
     }