白开心

  IT博客 :: 首页 ::  :: 联系 :: 聚合  :: 管理 ::
  9 随笔 :: 76 文章 :: 28 评论 :: 0 Trackbacks

 

/**//**//**//// <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;
        }
posted on 2008-07-14 20:11 白开心 阅读(373) 评论(0)  编辑 收藏 引用 所属分类: .Net(学习ing...)
只有注册用户登录后才能发表评论。