sql
  • datagridview
  • inner-join
  • bindingsource
  • 2012-04-12 11 views 0 likes 
    0

    BindingSourceを使用しています。内部結合を実行するためにいくつかのSQLコードを使用したいと思います。 このための私のコードはBindingSource.Filter Inner Joinを使用します。

    ticketsBindingSource.Filter = "SELECT u.CallerName, t.* FROM users u INNER JOIN tickets t ON u.id = t.user WHERE u.CallerName = 'joe.smith' AND t.ProblemStatus = 'Open'"; 
    

    を動作しません。しかし、次は動作しない

    ticketsBindingSource.Filter = "ProblemStatus = 'Open'"; 
    

    がどのように私は私のInnerJoinクエリを実行し、私のDataGridViewを更新することができますか?

    答えて

    1

    BindingSource.Filterは、DataGridViewに既に読み込まれているデータにフィルタを適用します。したがって、アカウントが表示されていて、1つの特定のアカウントを探している場合は、そのデータをアカウント番号でフィルタリングします。

    Filterは、別のSQLクエリを実行できません。

    そのデータにINNER JOINを実行する場合は、別の検索を実行してDataGridViewをロードする必要があります。フィルタを実行する必要はないようですが、同じグリッド内のデータに2つの別々のセットを表示する必要があります。

    あなたの基本的なプロセスは次のようになります。あなたは

  • 使用して、彼らが
  • たいフィルターやレコードがあなたの内で2番目の検索が新しいとあなたのDataGridViewをリロード
  • を結合を実行選択したDataGridView

    1. ロードデータ。

    ここEDITはあなたを始めるかもしれないいくつかのコードです:データがグリッドにバインドされると、その後、ユーザーにフィルタするためにいくつかの方法を提供する

    How to: Bind Data to the Windows Forms DataGridView Control

    private void GetData(string selectCommand) 
    { 
        try 
        { 
         // Specify a connection string. Replace the given value with a 
         // valid connection string for a Northwind SQL Server sample 
         // database accessible to your system. 
         String connectionString = 
          "Integrated Security=SSPI;Persist Security Info=False;" + 
          "Initial Catalog=Northwind;Data Source=localhost"; 
    
         // Create a new data adapter based on the specified query. 
         dataAdapter = new SqlDataAdapter(selectCommand, connectionString); 
    
         // Create a command builder to generate SQL update, insert, and 
         // delete commands based on selectCommand. These are used to 
         // update the database. 
         SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter); 
    
         // Populate a new data table and bind it to the BindingSource. 
         DataTable table = new DataTable(); 
         table.Locale = System.Globalization.CultureInfo.InvariantCulture; 
         dataAdapter.Fill(table); 
         bindingSource1.DataSource = table; 
    
         // Resize the DataGridView columns to fit the newly loaded content. 
         dataGridView1.AutoResizeColumns( 
          DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader); 
        } 
        catch (SqlException) 
        { 
         MessageBox.Show("To run this example, replace the value of the " + 
          "connectionString variable with a connection string that is " + 
          "valid for your system."); 
        } 
    } 
    
    private void Button1_Click(object sender, EventArgs e) 
    { 
        // Bind the DataGridView to the BindingSource 
        // and load the data from the database. 
        dataGridView1.DataSource = bindingSource1; 
        GetData("select * from Customers"); 
    } 
    

    が、データを再度検索する必要があります。したがって、実行するSQLを何らかの方法で選択するコードを使用したいと思うでしょう。だから、あなたは

    private void Button1_Click(object sender, EventArgs e) 
    { 
        // Bind the DataGridView to the BindingSource 
        // and load the data from the database. 
        dataGridView1.DataSource = bindingSource1; 
    
        string sqlQuery = string.Empty; 
    
        if(your check goes here) 
        { 
         sqlQuery = "select * from Customers"; 
        } 
        else 
        { 
         sqlQuery = "your new SQL"; 
        } 
    
        GetData(sqlQuery); 
    } 
    

    は、あなたが新しいクエリに基づいてデータを再バインドうユーザーの選択に基づいて設定されている文字列としてのGetData()にSQLを渡すことができます。

  • +0

    はい、私は混乱しています。 VS10のビジュアルデータバインダーを使用してデータセットを作成する前に、バインディングソースを作成し、視覚ツールを使用して列とそのヘッダーを構成しました。あなたのメソッドでは、新しいデータをリロードしてInner Joinクエリを実行するにはどうしたらいいですか? –

    +0

    データグリッドにデータをバインドするコードがありますか?そうでなければ、私はあなたのための例を見つけるでしょう。 – Taryn

    +0

    私はこれまでに0行のコードを持っています。すべてが視覚的に行われました。しかし、両方の方法が同時に存在する可能性がありますか? –

    1

    データソースを変更してください。

    結合を含むビューを作成します。

    データバインディングのデータソースとしてそのビューを使用します。

    必要に応じてフィルタを使用してください。

    フィルタはwhere句の一部になります。

    関連する問題