2016-06-28 7 views
0

リストボックスに列を表示できるように、ローカルSQL Server Expressインスタンスへの接続を設定しようとしています。 Th buildは正常に動作し、エラーは表示されませんが、リストボックスにはデータはありません。私はクエリをテストして、それは問題ありません。私は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(connstring, commstring); 

によって

SqlDataAdapter customerDataAdapater = new SqlDataAdapter(commstring, connstring); 

:フォローラインを変更した後

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 catchを追加しました。これを押すとsql接続が開き、成功した場合はそれほど多くのメッセージボックスが表示され、これが機能しています。それで、それは接続することができます。接続文字列は問題ありません。クエリはですか?複数のテーブルを試してもまだ何も得られていません –

+0

@NickProud:私がよく理解していないのは、あなたが実際に達成しようとしていることです....あなたの 'customers'リストボックスの選択を変更するたびに、もう一度データを.......少し過度のようだ。 **本当に**あなたは何をしようとしていますか? –

関連する問題