0

SQL Server Management Studio で SQL クエリを実行したところ、うまくいきました。

これが私のコードです

    private void buttonRunQuery_Click(object sender, EventArgs e)
     {
         if (connection == null)
         {
             connection = ConnectionStateToSQLServer();
             SqlCommand command = new SqlCommand(null, connection);
             command = createSQLQuery(command);
             dataGridView1.DataSource = GetData(command);
         }
         else
         {
             SqlCommand command = new SqlCommand(null, connection);
             command = createSQLQuery(command);
             dataGridView1.DataSource = GetData(command);
         }
     }

    private SqlCommand createSQLQuery(SqlCommand command)
     {
         string[] allTheseWords;
         if (textBoxAllTheseWords.Text.Length > 0)
         {
             allTheseWords = textBoxAllTheseWords.Text.Split(' ');
             string SQLQuery = "SELECT distinct [database].[dbo].[customerTable].[name], [database].[dbo].[customerTable].[dos], [database].[dbo].[customerTable].[accountID], [database].[dbo].[reportTable].[customerID], [database].[dbo].[reportTable].[accountID], [database].[dbo].[reportTable].[fullreport] FROM [database].[dbo].[reportTable], [database].[dbo].[customerTable] WHERE ";
             int i = 1;
             foreach (string word in allTheseWords)
             {
                 var name = "@word" + (i++).ToString();
                 command.Parameters.AddWithValue(name, "'%" + word "%'");
                 SQLQuery = SQLQuery + String.Format(" [database].[dbo].[reportTable].[fullreport] LIKE {0} AND ", name);
             }
             SQLQuery = SQLQuery + " [database].[dbo].[customerTable].[accountID] = [database].[dbo].[reportTable].[accountID]";
             command.CommandText = SQLQuery;
         }
         MessageBox.Show(command.CommandText.ToString());
         return command;
     }

    public DataTable GetData(SqlCommand cmd)
     {
         //SqlConnection con = new SqlConnection(connString);
         //SqlCommand cmd = new SqlCommand(sqlcmdString, cn);
         SqlDataAdapter da = new SqlDataAdapter(cmd);
         connection.Open();
         DataTable dt = new DataTable();
         da.Fill(dt);
         connection.Close();
         return dt;
     } 

VS2012 でエラーが発生せず、DataGridView にデータが表示されない

助言がありますか?

私はこのウェブサイトを見ましたが、本当に役に立ちませんでした

http://bytes.com/topic/c-sharp/answers/530616-datagridview-combobox-column-databound-item-list

SQL Server 2012 を使用しています

更新されたクエリは、SQL Server Management Studio で 1 つの結果をもたらします (これは予想されることです)。同じクエリでデータグリッドに行が生成されません。

何が起こっているのか理解できませんか?VS2012 の GUI を使用して何かをバインドする必要がありますか?

4

2 に答える 2

4

LIKE 検索を行っていることに気付きましたが、パラメーターを追加するときに「%」を使用していません。これを追加してみてください:

command.Parameters.AddWithValue(name, "%" + word +"%");

お役に立てれば。

ところで-このDataBindメソッドは、グリッドビューのwinフォームでは使用されず、Webフォームのみで使用されます。

幸運を。

于 2013-02-01T21:49:07.350 に答える
1

DataGridでメソッドDataBind()を呼び出して、データソースからグリッドにデータを実際にバインドする必要があります。

dataGridView1.DataBind();
于 2013-02-01T21:20:13.577 に答える