/**//**//**//**//**//**//**//// <summary> /// 数据库连接 Name 值 /// </summary> private const string DB_CONNECTION = "CRMConnectionString";private Database _DB = null; /**//// <summary> /// 数据操作对象 /// </summary> protected Database DB { get { if (_DB == null) { _DB = DatabaseFactory.CreateDatabase(DB_CONNECTION); } return _DB; } }private DbProviderFactory _DBFactory = null; /**//**//**//**//**//**//**//// <summary> /// /// </summary> protected DbProviderFactory DBFactory { get { if (_DBFactory == null) { /**//**//**//// 两种方法都一样构造该对象 /// string dbProviderName = ConfigurationManager.ConnectionStrings[DB_CONNECTION].ProviderName; /// _DBFactory = DbProviderFactories.GetFactory(dbProviderName); _DBFactory = DB.DbProviderFactory; } return _DBFactory; } }/**//**//**//**//**//**//**//// <summary> /// 保存方法 /// </summary> /// <param name="dataCompany"></param> /// <param name="dataProduct"></param> /// <returns></returns> public bool Save(DataTable dataCompany, DataTable dataProduct) { DbCommand Cmd; DbDataAdapter Adp; DbCommandBuilder Cmb; using (DbConnection Conn = DB.CreateConnection()) { Conn.Open(); DbTransaction tran = null; try { tran = Conn.BeginTransaction(); // Create the select command. Cmd = DBFactory.CreateCommand(); Cmd.CommandText = "Select * from CompeteCompany"; Cmd.Connection = Conn; Cmd.Transaction = tran; // Create the DbDataAdapter. Adp = DBFactory.CreateDataAdapter(); Adp.SelectCommand = Cmd; // Create the DbCommandBuilder. Cmb = DBFactory.CreateCommandBuilder(); Cmb.DataAdapter = Adp; // Get the insert, update and delete commands. Adp.InsertCommand = Cmb.GetInsertCommand(); Adp.UpdateCommand = Cmb.GetUpdateCommand(); Adp.DeleteCommand = Cmb.GetDeleteCommand(); /**//**//**//**//**//**//**//// 更新竞争对手表 Adp.Update(dataCompany); // Create the select command. Cmd = DBFactory.CreateCommand(); Cmd.CommandText = "Select * from CompeteProduct"; Cmd.Connection = Conn; Cmd.Transaction = tran; // Create the DbDataAdapter. Adp = DBFactory.CreateDataAdapter(); Adp.SelectCommand = Cmd; // Create the DbCommandBuilder. Cmb = DBFactory.CreateCommandBuilder(); Cmb.DataAdapter = Adp; // Get the insert, update and delete commands. Adp.InsertCommand = Cmb.GetInsertCommand(); Adp.UpdateCommand = Cmb.GetUpdateCommand(); Adp.DeleteCommand = Cmb.GetDeleteCommand(); /**//**//**//**//**//**//**//// 更新竞争产品表 Adp.Update(dataProduct); } catch { if (tran.Connection != null) tran.Rollback(); } finally { if (tran.Connection != null) tran.Commit(); } } return true; }
.csharpcode, .csharpcode pre { font-size:small; color:black; font-family:consolas, "Courier New", courier, monospace; background-color:#ffffff; } .csharpcode pre { margin:0em; } .csharpcode .rem { color:#008000; } .csharpcode .kwrd { color:#0000ff; } .csharpcode .str { color:#006080; } .csharpcode .op { color:#0000c0; } .csharpcode .preproc { color:#cc6633; } .csharpcode .asp { background-color:#ffff00; } .csharpcode .html { color:#800000; } .csharpcode .attr { color:#ff0000; } .csharpcode .alt { background-color:#f4f4f4; width:100%; margin:0em; } .csharpcode .lnum { color:#606060; }