这代码很……虽然我用 vim 写起来不累,但读代码感觉也很郁闷。能化简不?
            ProductAdapter.SelectCommand = new SQLiteCommand(ProductSelectCmd, Connection);
            ProductAdapter.InsertCommand = new SQLiteCommand(ProductInsertCmd, Connection);
            ProductAdapter.UpdateCommand = new SQLiteCommand(ProductUpdateCmd, Connection);
            ProductAdapter.DeleteCommand = new SQLiteCommand(ProductDeleteCmd, Connection);
            ClientAdapter.SelectCommand  = new SQLiteCommand(ClientSelectCmd,  Connection);
            ClientAdapter.InsertCommand  = new SQLiteCommand(ClientInsertCmd,  Connection);
            ClientAdapter.UpdateCommand  = new SQLiteCommand(ClientUpdateCmd,  Connection);
            ClientAdapter.DeleteCommand  = new SQLiteCommand(ClientDeleteCmd,  Connection);
            TradeAdapter.SelectCommand   = new SQLiteCommand(TradeSelectCmd,   Connection);
            TradeAdapter.InsertCommand   = new SQLiteCommand(TradeInsertCmd,   Connection);
            TradeAdapter.UpdateCommand   = new SQLiteCommand(TradeUpdateCmd,   Connection);
            TradeAdapter.DeleteCommand   = new SQLiteCommand(TradeDeleteCmd,   Connection);
            BillAdapter.SelectCommand    = new SQLiteCommand(BillSelectCmd,    Connection);
            BillAdapter.InsertCommand    = new SQLiteCommand(BillInsertCmd,    Connection);
            BillAdapter.UpdateCommand    = new SQLiteCommand(BillUpdateCmd,    Connection);
            BillAdapter.DeleteCommand    = new SQLiteCommand(BillDeleteCmd,    Connection);

解决方案 »

  1.   

    SQLite 没有实现 CommandBuilder 吗? 有了SelectCommand 可以用 CommandBuilder 直接构造其他Commander
      

  2.   

    不用sqllite所以没发言权常规情况,我们通常用CommandBuilder让ado。net自己去构建相应代码如果sqlite没有CommandBuilder实现,个人会用T4引擎自己写个模板,让他自动生成代码
      

  3.   

    把所有的命令文本写到一个配置文件中,然后从配置文件中加载,例如:
    public DataAdapter CreateAdapterFromConfiguration(string tableName, SQLLiteConnection connection)
    {
      DataAdapter da = new DataAdapter();  da.SelectCommand = new SQLLiteCommand(GetCommandTextFromConfiguration(tableName, "Select"), connection);
      da.InsertCommand = new SQLLiteCommand(GetCommandTextFromConfiguration(tableName, "Insert"), connection);
      da.UpdateCommand = new SQLLiteCommand(GetCommandTextFromConfiguration(tableName, "Update"), connection);
      da.DeleteCommand = new SQLLiteCommand(GetCommandTextFromConfiguration(tableName, "Delete"), connection);
      
      return da;
    }public string GetCommandTextFromConfiguration(string tableName, string commandName)
    {
      // 从一个XML配置文件中读取内容
      // ...
    }
    配置文件参考下面的格式:<Configuration>
      <Table Name="Product">
        <Command Name="Select">SELECT * FROM Product</Command>
        <Command Name="Insert">INSERT Product VALUES(@p1, @p2, ...)</Command>
        <Command Name="Update">...</Command>
        <Command Name="Delete">...</Command>
      </Table>
    </Configuration>
      

  4.   

    然后,这样的代码读起来应该不会累了吧:
    ProductAdapter = CreateAdapterFromConfiguration("Product", Connection);
    ClientAdapter = CreateAdapterFromConfiguration("Client", Connection);
    TradeAdapter = CreateAdapterFromConfiguration("Trade", Connection);
    BillAdapter = CreateAdapterFromConfiguration("Bill", Connection);