我希望能够对我拥有的表适配器执行自定义SQL查询。是否有可能做到这一点?还是只能在数据集设计视图中的每个表适配器上使用预定义的查询?

如果无法执行此操作,该如何对表执行SQL查询,并在绑定到表适配器的datagridview中显示结果?

谢谢。

编辑:我没有正确解释自己。我知道如何使用数据集设计器将查询添加到tableadapter中。我的问题是我需要针对现有的表适配器执行SQL的自定义项(我动态构建)。

最佳答案

我发表了评论,指向使用VB的示例,该示例创建了扩展TableAdapter的类。我将展示如何在不创建扩展TableAdapter的类的情况下完成此操作,而不是使用VB示例并用C#重写它。

基本上创建一个BackgroundWorker来执行sql查询。您不必这样做,但这会很好。根据用户输入来构建查询字符串。

    private void queryBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        //Initialize sqlconnection
        SqlConnection myConnection;

        //Convert date in to proper int format to match db
        int fromDate = int.Parse(dateTimePickerStartDate.Value.ToString("yyyyMMdd"));
        int toDate = int.Parse(dateTimePickerEndDate.Value.ToString("yyyyMMdd"));

        //Setup Parameters
        SqlParameter paramFromDate;
        SqlParameter paramToDate;
        SqlParameter paramItemNo;
        SqlParameter paramCustomerNo;

        //Fill the data using criteria, and throw any errors
        try
        {
            myConnection = new SqlConnection(connectionString);
            myConnection.Open();
            using (myConnection)
            {
                using (SqlCommand myCommand = new SqlCommand())
                {
                    //universal where clause stuff
                    string whereclause = "WHERE ";

                    //Add date portion
                    paramFromDate = new SqlParameter();
                    paramFromDate.ParameterName = "@FromDate";
                    paramFromDate.Value = fromDate;

                    paramToDate = new SqlParameter();
                    paramToDate.ParameterName = "@ToDate";
                    paramToDate.Value = toDate;

                    myCommand.Parameters.Add(paramFromDate);
                    myCommand.Parameters.Add(paramToDate);

                    whereclause += "(TableName.date BETWEEN @FromDate AND @ToDate)";

                    //Add item num portion
                    if (!string.IsNullOrEmpty(itemNo))
                    {
                        paramItemNo = new SqlParameter();
                        paramItemNo.ParameterName = "@ItemNo";
                        paramItemNo.Value = itemNo;

                        myCommand.Parameters.Add(paramItemNo);

                        whereclause += " AND (Tablename.item_no = @ItemNo)";
                    }

                    //Add customer number portion
                    if (!string.IsNullOrEmpty(customerNo))
                    {
                        paramCustomerNo = new SqlParameter();
                        paramCustomerNo.ParameterName = "@CustomerNo";
                        paramCustomerNo.Value = customerNo;

                        myCommand.Parameters.Add(paramCustomerNo);

                        whereclause = whereclause + " AND (Tablename.cus_no = @CustomerNo)";
                    }


                    string sqlquery = "SELECT * FROM TableName ";
                    sqlquery += whereclause;

                    //MessageBox.Show(sqlquery);

                    myCommand.CommandText = sqlquery;
                    myCommand.CommandType = CommandType.Text;
                    myCommand.Connection = myConnection;

                    this.exampleTableAdapter.ClearBeforeFill = true;
                    this.exampleTableAdapter.Adapter.SelectCommand = myCommand;
                    this.exampleTableAdapter.Adapter.Fill(this.ExampleDataSet.ExampleTable);
                }
            }
        }
        catch (System.Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }


我个人喜欢编码扩展TableAdapter的类的想法,但这是回答OP问题的快速简便方法。抱歉,花了一年时间:)

10-07 12:08
查看更多