2016-06-28 36 views
0

嘗試建立到本地SQL Server Express實例的連接,以便我可以在列表框中顯示列。構建運行良好,我看不到錯誤,但列表框中沒有數據。我測試了查詢,那很好。我正在使用NT身份驗證到數據庫。任何想法,我可能會出錯?通過ADO.NET連接到SQL Server - 空列表框

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void customers_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     string commstring = "Driver ={SQL Server}; Server = DESKTOP-5T4MHHR\\SQLEXPRESS; Database = AdventureWorks2014; Trusted_Connection = Yes;"; 
     string connstring = "SELECT FirstName, LastName FROM Person.Person"; 

     SqlDataAdapter customerDataAdapater = new SqlDataAdapter(commstring, connstring); 

     DataSet customerDataSet = new DataSet(); 
     customerDataAdapater.Fill(customerDataSet, "Person.Person"); 

     DataTable customerDataTable = new DataTable(); 
     customerDataTable = customerDataSet.Tables[0]; 

     foreach (DataRow dataRow in customerDataTable.Rows) 
     { 
      customers.Items.Add(dataRow["FirstName"] + " (" + dataRow["LastName"] + ")"); 
     } 
    } 
} 
+0

您的數據庫是** AdventureWorks2014 **,但您使用** Person.Person **。怎麼樣?那應該是** AdventureWorks2014.Person **。 – Berkay

+2

@BerkayYaylaci表名沒有錯,OP指定表的模式。事實上,'AdventureWorks2014.Person'將是錯誤的,你必須鍵入'AdventureWorks2014.dbo.Person' –

+0

@PanagiotisKanavos謝謝,好點。 – Berkay

回答

1

我在這裏測試了一個示例項目中的代碼,並且我意識到您以錯誤的順序將參數傳遞給SqlDataAdapter構造函數。

改變後續行之後:

SqlDataAdapter customerDataAdapater = new SqlDataAdapter(commstring, connstring); 

通過

SqlDataAdapter customerDataAdapater = new SqlDataAdapter(connstring, commstring); 

列表框被成功填補。

1

連接字符串似乎不可思議.....

你能嘗試使用僅此:

string commstring = "Server=DESKTOP-5T4MHHR\\SQLEXPRESS;Database=AdventureWorks2014;Trusted_Connection=Yes;"; 

另外:你爲什麼先創建DataSet,在一組填充的數據,然後從中提取一個DataTable?這是不必要的複雜代碼 - 只是用這個來代替:

SqlDataAdapter customerDataAdapater = new SqlDataAdapter(commstring, connstring); 

// if you only ever need *one* set of data - just use a DataTable directly! 
DataTable customerDataTable = new DataTable(); 

// Fill DataTable with the data from the query 
customerDataAdapater.Fill(customerDataTable); 

更新:我真的重寫代碼,以這樣的:

// create a separate class - call it whatever you like 
public class DataProvider 
{ 
    // define a method to provide that data to you 
    public List<string> GetPeople() 
    { 
     // define connection string (I'd really load that from CONFIG, in real world) 
     string connstring = "Server=MSEDTOP;Database=AdventureWorks2014;Trusted_Connection=Yes;"; 

     // define your query 
     string query = "SELECT FirstName, LastName FROM Person.Person"; 

     // prepare a variable to hold the results 
     List<string> entries = new List<string>(); 

     // put your SqlConnection and SqlCommand into "using" blocks 
     using (SqlConnection conn = new SqlConnection(connstring)) 
     using (SqlCommand cmd = new SqlCommand(query, conn)) 
     { 
      conn.Open(); 

      // iterate over the results using a SqlDataReader 
      using (SqlDataReader rdr = cmd.ExecuteReader()) 
      { 
       while (rdr.Read()) 
       { 
        // get first and last name from current row of query 
        string firstName = rdr.GetFieldValue<string>(0); 
        string lastName = rdr.GetFieldValue<string>(1); 

        // add entry to result list 
        entries.Add(firstName + " " + lastName); 
       } 

       rdr.Close(); 
      } 

      conn.Close(); 
     } 

     // return the results 
     return entries; 
    } 
} 

而在你的後臺代碼,你只需要做這樣的事情:

protected override void OnLoad(.....) 
{ 
    if (!IsPostback) 
    { 
     List<string> people = new DataProvider().GetPeople(); 

     customers.DataSource = people; 
     customers.DataBind(); 
    } 
} 

,但我還是不明白你試圖當SelectedInde xChanged事件發生.....

+0

感謝您向我展示一個更簡單的方法。我已經嘗試了以上,但它仍然是空白的。有任何想法嗎? –

+0

所以我還添加了一個try按鈕,當按下按鈕打開sql連接,如果成功,它會顯示一個消息框,表示儘可能多,這已經工作。所以它能夠連接。連接字符串很好。是查詢嗎?嘗試了多個表,仍然沒有得到任何東西 –

+0

@NickProud:我不太明白的是你真正想要完成的事情....每當你改變你的客戶列表框的選擇時,你就會出去獲取所有的東西數據再次.......似乎有點矯枉過正。你真的**想做什麼? –