?
我与ADO.NET二三事(十四)
.Clear();
91 this.CommandValues.Add(_NextProcedureProvide.CommandName, _Value);
92 return this;
93 }
94
95 public DatabaseOperation WidthExecuteProcedureCommand(Func argument_Command)
96 {
97 _NextProcedureProvide = new DatabaseCommandProcedure(this._ConnectionProvide);
98 OperationValue _Value = argument_Command.Invoke(_NextProcedureProvide);
99 CommandValues.Add(_NextProcedureProvide.CommandName, _Value);
100 return this;
101 }
102
103 public DatabaseOperation BeginTransaction()
104 {
105 this._Transaction = this._ConnectionProvide.Connection.BeginTransaction();
106 return this;
107 }
108
109 public DatabaseOperation EndTransaction()
110 {
111 this._Transaction.Commit();
112 return this;
113 }
114
115 public DatabaseOperation CancelTransaction()
116 {
117 this._Transaction.Rollback();
118 return this;
119 }
120 }
这个主要用于执行过程SQL.设计此类的灵感来源于一次工作任务,当时要整理和搬迁
Oracle,SQL SERVER ,
DB2等服务器上的数据,存储过程和函数等很多操作搬迁特别麻烦.因此设计了一个过程式执行SQL.
?
?
?
*************************************************************************************************************************************************
?
案例1 (备注:这个用的是System.Data.ODBC):
1 Database _Provide = new Database();
2 DatabaseConnection _Connection = new DatabaseConnection(_Provide);
3 IDatabaseCommandProcedure _CommandProcedure = new DatabaseCommandProcedure(_Connection);
4 DbParameter _IDParameter = _CommandProcedure.NewParameter("ID", 1, DbType.Int32, ParameterDirection.Input);
5 OperationValue _Value = _CommandProcedure.Query("Query ?", new DbParameter[] { _IDParameter });
?
案例2:
1 DatabaseOperation _database_operation = new DatabaseOperation(); 2 _database_operation = _database_operation.ExecuteTextCommand(command =>3 { 4 command.CommandName = "INSERT"; 5 return command.TransactionSave("INSERT INTO DatabaseUser(Name) VALUES('五哥哥')", null); 6 7 }).ExecuteProcedureCommand((_command) => 8 { 9 bool _res = Convert.ToBoolean(_database_operation.CommandValues["INSERT"].Value); 10 if (_res) 11 { 12 DbParameter _IDParameter = _command.NewParameter("ID", 2, DbType.Int32, ParameterDirection.Input); 13 _command.CommandName = "Query"; 14 return _command.Query("Query", new DbParameter[] { _IDParameter }); 15 } 16 else 17 { 18 return null; 19 } 20 }).WidthExecuteTextCommand(_command => 21 { 22 _command.CommandName = "UPDATE"; 23 return _command.Modify("UPDATE DatabaseUser SET Name = '张三丰'", null); 24 }); 25 26 DataTable _dt = (DataTable)_database_operation.CommandValues["Query"].Value; 27 for (int i = 0; i < _dt.Rows.Count; i++) 28 { 29 Console.WriteLine("ID:{0}\tName:{1}", _dt.Rows[i]["ID"], _dt.Rows[i]["Name"]); 30 }
?
?案例3:
1 DatabaseOperation _database_operation = new DatabaseOperation();
2 _database_operation.BeginTransaction();
3 _database_operation.ExecuteTextCommand((command, transaction) =>
4 {
5 command.CommandName = "新增";
6 return command.Save("INSERT INTO DatabaseUser(Name) VALUES('五哥哥')", null);
7 }).WidthExecuteTextCommand((command, transaction) =>
8 {
9 command.CommandName = "查询";
10 return command.Query("SELECT * FROM DatabaseUser WHERE Name = '五哥哥'", transaction, null);
11 });
12
13 DataTable _Dt = _database_operation.CommandValues["查询"].Value as DataTable;
14 if ("五哥哥".Equals((_Dt.Rows[0]["Name"])))
15 {
16 _databa