DataBaseFactory基础了解

 <add name="Connection str" connectionString="data source=.;pwd=12;uid=sa;database=12;" providerName="System.Data.SqlClient" />
//上面的是webconfig中的配置文件
static string connstring_STR = "Connection str"; public static DataSet ExeQuery(string Cmd) { //下面的DbFactory是微软自带的类库,用于创建一个数据库链接
//以后多用下DataBaseFactory这个词, Database db = DatabaseFactory.CreateDatabase(connstring_STR); DbConnection conn = db.CreateConnection();//创建一个Db的链接通道 try { conn.Open(); return db.ExecuteDataSet(CommandType.Text, Cmd);//这里执行动作 } finally { conn.Close(); conn = null; } }

  

//加入事务,可以回滚
public static void ExeNonQuery(string Cmd) { Database db = DatabaseFactory.CreateDatabase(connstring_TMS); DbConnection conn = db.CreateConnection(); conn.Open(); DbTransaction transaction = conn.BeginTransaction(); try { db.ExecuteNonQuery(CommandType.Text, Cmd); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } finally { conn.Close(); conn = null; } }

  

posted @ 2018-08-13 11:11  ProZkb  阅读(495)  评论(0编辑  收藏  举报