如何通过代码设置TableAdapter某个表的SQL查询语句?

解决方案 »

  1.   

    http://msdn.microsoft.com/zh-cn/library/kda44dwy(VS.80).aspx
    http://msdn.microsoft.com/zh-cn/library/bz9tthwx(VS.80).aspx
      

  2.   

    有一个FillBy
    具体的可以查看:
    http://msdn.microsoft.com/zh-cn/library/kda44dwy(VS.80).aspx
    http://msdn.microsoft.com/zh-cn/library/ms171907(VS.80).aspx
      

  3.   

    1、调用 TableAdapter 的 GetData 或 GetDataBy 查询,返回以查询结果填充的类型化数据表
    NorthwindDataSet.CustomersDataTable newCustomersTable;
    newCustomersTable = customersTableAdapter1.GetData();2、
    执行返回单(标量)值的 TableAdapter 查询
    NorthwindDataSetTableAdapters.QueriesTableAdapter scalarQueriesTableAdapter;
    scalarQueriesTableAdapter = new NorthwindDataSetTableAdapters.QueriesTableAdapter();int returnValue;
    returnValue = (int)scalarQueriesTableAdapter.CustomerCount();3、执行填充现有数据表的 TableAdapter 查询
    customersTableAdapter1.Fill(northwindDataSet1.Customers);
      

  4.   


            public static OleDbDataAdapter CreateDataAdapter(OleDbConnection connection)
            {
                //string selectCommand = "SELECT 表1.编号, 名称,nl FROM 表1,表2 where 表1.编号=表2.编号";
                string selectCommand = "SELECT 表1.编号, 名称  FROM 表1 ";
                OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand, connection);
                adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;            // Create the Insert, Update and Delete commands.
                adapter.InsertCommand = new OleDbCommand("INSERT INTO 表1 (编号, 名称) VALUES (?, ?)", connection);
                // Create the parameters.
                adapter.InsertCommand.Parameters.Add("@编号", OleDbType.Integer, 5, "编号");
                adapter.InsertCommand.Parameters.Add("@名称", OleDbType.VarChar, 40, "名称");            adapter.UpdateCommand = new OleDbCommand(@"UPDATE `表1` SET `名称` = ? WHERE 编号 = ?", connection);
                adapter.UpdateCommand.Parameters.Add("@名称", OleDbType.VarChar, 40, "名称");
                adapter.UpdateCommand.Parameters.Add("@编号", OleDbType.Integer, 5, "编号").SourceVersion
                    = DataRowVersion.Original;            adapter.DeleteCommand = new OleDbCommand("DELETE FROM 表1 WHERE 编号 = ?", connection);
                adapter.DeleteCommand.Parameters.Add("@编号", OleDbType.Integer, 5, "编号").SourceVersion
                    = DataRowVersion.Original;            return adapter;
            }