一个抽象工厂数据访问模块(a data access component using abstract factory pattern SQL/OLEDB/ODBC)...

来到这里已经半个月了,感谢MSTC不惜重金聘请MVP来教我们这48个菜鸟(整体素质之低有时候自己都觉得丢人),两本基础书籍的学习完成后,作为练笔,写了一个分布式的图书管理系统,规定时间10天,我用4天就搞定了,如果不是练玩带写的话,估计会更快。

这里第一次用到了自己的抽象工厂模式(Abstract Factory Pattern),但实际上却很失败——系统所有的数据访问基本上都要求用SP来做,这使得我的工厂形同虚设
但无论如何,还是写一下这个工厂吧:

首先是抽象工厂类(AbstractDbFactory.cs)
 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.Text;
 4 None.gif using  System.Data;
 5 None.gif
 6 None.gif namespace  DAL
 7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary> 
 9InBlock.gif    /// 数据库抽象工厂接口 
10ExpandedSubBlockEnd.gif    /// </summary> 

11InBlock.gif    public interface AbstractDbFactory
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
14InBlock.gif        /// 建立默认连接 
15InBlock.gif        /// </summary> 
16ExpandedSubBlockEnd.gif        /// <returns>数据库连接</returns> 

17InBlock.gif        IDbConnection CreateConnection();
18InBlock.gif
19ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
20InBlock.gif        /// 根据连接字符串建立Connection对象 
21InBlock.gif        /// </summary> 
22InBlock.gif        /// <param name="strConn">连接字符串</param> 
23ExpandedSubBlockEnd.gif        /// <returns>Connection对象</returns> 

24InBlock.gif        IDbConnection CreateConnection(string strConn);
25InBlock.gif
26ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
27InBlock.gif        /// 建立Command对象 
28InBlock.gif        /// </summary> 
29ExpandedSubBlockEnd.gif        /// <returns>Command对象</returns> 

30InBlock.gif        IDbCommand CreateCommand();
31InBlock.gif
32ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
33InBlock.gif        /// 建立DataAdapter对象 
34InBlock.gif        /// </summary> 
35ExpandedSubBlockEnd.gif        /// <returns>DataAdapter对象</returns> 

36InBlock.gif        IDbDataAdapter CreateDataAdapter();
37InBlock.gif
38ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
39InBlock.gif        /// 根据Connection建立Transaction 
40InBlock.gif        /// </summary> 
41InBlock.gif        /// <param name="myDbConnection">Connection对象</param> 
42ExpandedSubBlockEnd.gif        /// <returns>Transaction对象</returns> 

43InBlock.gif        IDbTransaction CreateTransaction(IDbConnection myDbConnection);
44InBlock.gif
45ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
46InBlock.gif        /// 根据Command建立DataReader 
47InBlock.gif        /// </summary> 
48InBlock.gif        /// <param name="myDbCommand">Command对象</param> 
49ExpandedSubBlockEnd.gif        /// <returns>DataReader对象</returns> 

50InBlock.gif        IDataReader CreateDataReader(IDbCommand myDbCommand);
51InBlock.gif
52ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
53InBlock.gif        /// 根据参数名和参数值建立DataParamter
54InBlock.gif        /// </summary>
55InBlock.gif        /// <param name="strProcName">参数名称</param>
56InBlock.gif        /// <param name="value">参数值</param>
57ExpandedSubBlockEnd.gif        /// <returns>DataParamter对象</returns>

58InBlock.gif        IDataParameter CreateDataParamter(string strParaName,object value);
59InBlock.gif
60ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
61InBlock.gif        /// 获得连接字符串 
62InBlock.gif        /// </summary> 
63ExpandedSubBlockEnd.gif        /// <returns>连接字符串</returns> 

64InBlock.gif        string GetConnectionString();
65ExpandedSubBlockEnd.gif    }

66ExpandedBlockEnd.gif}

然后是通用工厂接口(Factory.cs)
 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.Text;
 4 None.gif using  System.Configuration;
 5 None.gif using  System.Xml;
 6 None.gif
 7 None.gif namespace  DAL
 8 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary> 
10InBlock.gif    /// Factory类 
11ExpandedSubBlockEnd.gif    /// </summary> 

12InBlock.gif    public sealed class Factory
13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
14InBlock.gif        private static volatile Factory singleFactory = null;
15InBlock.gif        private static object syncObj = new object();
16ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
17InBlock.gif        /// Factory类构造函数 
18ExpandedSubBlockEnd.gif        /// </summary> 

19InBlock.gif        private Factory()
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21ExpandedSubBlockEnd.gif        }

22InBlock.gif
23ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
24InBlock.gif        /// 获得Factory类的实例 
25InBlock.gif        /// </summary> 
26ExpandedSubBlockEnd.gif        /// <returns>Factory类实例</returns> 

27InBlock.gif        public static Factory GetInstance()
28ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
29InBlock.gif            if (singleFactory == null)
30ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
31InBlock.gif                lock (syncObj)
32ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
33InBlock.gif                    if (singleFactory == null)
34ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
35InBlock.gif                        singleFactory = new Factory();
36ExpandedSubBlockEnd.gif                    }

37ExpandedSubBlockEnd.gif                }

38ExpandedSubBlockEnd.gif            }

39InBlock.gif            return singleFactory;
40ExpandedSubBlockEnd.gif        }

41InBlock.gif
42ExpandedSubBlockStart.gifContractedSubBlock.gif        /**////// <summary>
43InBlock.gif        ///// Read confing
44InBlock.gif        ///// </summary>
45InBlock.gif        ///// <param name="appKey">参数</param>
46ExpandedSubBlockEnd.gif        ///// <returns></returns>

47InBlock.gif        //public string GetConfigValue(string appKey)
48InBlock.gif        //{
49InBlock.gif        //    XmlDocument xDoc = new XmlDocument();
50InBlock.gif        //    try
51InBlock.gif        //    {
52InBlock.gif        //        xDoc.Load(".//DAL.dll.config");
53InBlock.gif        //        //xDoc.Load(System.Windows.Forms.Application.ExecutablePath+".config");
54InBlock.gif        //        XmlNode xNode;
55InBlock.gif        //        XmlElement xElem;
56InBlock.gif        //        xNode = xDoc.SelectSingleNode("//appSettings");
57InBlock.gif        //        xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
58InBlock.gif        //        if (xElem != null)
59InBlock.gif        //            return xElem.GetAttribute("value");
60InBlock.gif        //        else
61InBlock.gif        //            return "";
62InBlock.gif        //    }
63InBlock.gif        //    catch (Exception)
64InBlock.gif        //    {
65InBlock.gif        //        return "";
66InBlock.gif        //    }
67InBlock.gif        //
68InBlock.gif
69ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
70InBlock.gif        /// 建立Factory类实例 
71InBlock.gif        /// </summary> 
72ExpandedSubBlockEnd.gif        /// <returns>Factory类实例</returns> 

73InBlock.gif        public AbstractDbFactory CreateInstance()
74ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
75InBlock.gif            AbstractDbFactory abstractDbFactory = null;
76InBlock.gif            switch (ConfigurationSettings.AppSettings["DatabaseType"].ToLower())
77ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
78InBlock.gif                case "sqlserver":
79ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
80InBlock.gif                        abstractDbFactory = new SqlFactory();
81InBlock.gif                        break;
82ExpandedSubBlockEnd.gif                    }

83InBlock.gif                case "oledb":
84ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
85InBlock.gif                        abstractDbFactory = new OleDbFactory();
86InBlock.gif                        break;
87ExpandedSubBlockEnd.gif                    }

88InBlock.gif                case "odbc":
89ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
90InBlock.gif                        abstractDbFactory = new OdbcFactory();
91InBlock.gif                        break;
92ExpandedSubBlockEnd.gif                    }

93ExpandedSubBlockEnd.gif            }

94InBlock.gif            return abstractDbFactory;
95ExpandedSubBlockEnd.gif        }

96ExpandedSubBlockEnd.gif    }

97ExpandedBlockEnd.gif}

98 None.gif
99 None.gif

具体工厂类,这里写一个SQL的作例子吧(SqlFactory.cs)
  1 None.gif using  System;
  2 None.gif using  System.Data;
  3 None.gif using  System.Data.SqlClient;
  4 None.gif using  System.Configuration;
  5 None.gif
  6 None.gif
  7 None.gif namespace  DAL
  8 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  9ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary> 
 10InBlock.gif    /// 针对SqlServer专用连接的工厂 
 11ExpandedSubBlockEnd.gif    /// </summary> 

 12InBlock.gif    public class SqlFactory : AbstractDbFactory
 13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 14ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
 15InBlock.gif        /// 构造函数 
 16ExpandedSubBlockEnd.gif        /// </summary> 

 17InBlock.gif        public SqlFactory()
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 19ExpandedSubBlockEnd.gif        }

 20InBlock.gif
 21ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
 22InBlock.gif        /// 建立默认Connection对象 
 23InBlock.gif        /// </summary> 
 24ExpandedSubBlockEnd.gif        /// <returns>Connection对象</returns> 

 25InBlock.gif        public IDbConnection CreateConnection()
 26ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 27InBlock.gif            return new SqlConnection();
 28ExpandedSubBlockEnd.gif        }

 29InBlock.gif
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
 31InBlock.gif        /// 根据连接字符串建立Connection对象 
 32InBlock.gif        /// </summary> 
 33InBlock.gif        /// <param name="strConn">连接字符串</param> 
 34ExpandedSubBlockEnd.gif        /// <returns>Connection对象</returns> 

 35InBlock.gif        public IDbConnection CreateConnection(string strConn)
 36ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 37InBlock.gif            return new SqlConnection(strConn);
 38ExpandedSubBlockEnd.gif        }

 39InBlock.gif
 40ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
 41InBlock.gif        /// 建立Command对象 
 42InBlock.gif        /// </summary> 
 43ExpandedSubBlockEnd.gif        /// <returns>Command对象</returns> 

 44InBlock.gif        public IDbCommand CreateCommand()
 45ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 46InBlock.gif            return new SqlCommand();
 47ExpandedSubBlockEnd.gif        }

 48InBlock.gif
 49ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
 50InBlock.gif        /// 建立DataAdapter对象 
 51InBlock.gif        /// </summary> 
 52ExpandedSubBlockEnd.gif        /// <returns>DataAdapter对象</returns> 

 53InBlock.gif        public IDbDataAdapter CreateDataAdapter()
 54ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 55InBlock.gif            return new SqlDataAdapter();
 56ExpandedSubBlockEnd.gif        }

 57InBlock.gif
 58ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
 59InBlock.gif        /// 根据Connection建立Transaction 
 60InBlock.gif        /// </summary> 
 61InBlock.gif        /// <param name="myDbConnection">Connection对象</param> 
 62ExpandedSubBlockEnd.gif        /// <returns>Transaction对象</returns> 

 63InBlock.gif        public IDbTransaction CreateTransaction(IDbConnection myDbConnection)
 64ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 65InBlock.gif            return myDbConnection.BeginTransaction();
 66ExpandedSubBlockEnd.gif        }

 67InBlock.gif
 68ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
 69InBlock.gif        /// 根据Command建立DataReader 
 70InBlock.gif        /// </summary> 
 71InBlock.gif        /// <param name="myDbCommand">Command对象</param> 
 72ExpandedSubBlockEnd.gif        /// <returns>DataReader对象</returns> 

 73InBlock.gif        public IDataReader CreateDataReader(IDbCommand myDbCommand)
 74ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 75InBlock.gif            return myDbCommand.ExecuteReader();
 76ExpandedSubBlockEnd.gif        }

 77InBlock.gif
 78ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 79InBlock.gif        /// 根据参数名和参数值建立DataParamter
 80InBlock.gif        /// </summary>
 81InBlock.gif        /// <param name="strProcName">参数名称</param>
 82InBlock.gif        /// <param name="value">参数值</param>
 83ExpandedSubBlockEnd.gif        /// <returns>DataParamter对象</returns>

 84InBlock.gif        public IDataParameter CreateDataParamter(string strParaName, object value)
 85ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 86InBlock.gif            return new SqlParameter(strParaName, value);
 87ExpandedSubBlockEnd.gif        }

 88InBlock.gif
 89InBlock.gif
 90ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
 91InBlock.gif        /// 获得连接字符串 
 92InBlock.gif        /// </summary> 
 93ExpandedSubBlockEnd.gif        /// <returns>连接字符串</returns> 

 94InBlock.gif        public string GetConnectionString()
 95ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 96InBlock.gif            string strServer = ConfigurationSettings.AppSettings["SqlServerServer"];
 97InBlock.gif            string strDatabase = ConfigurationSettings.AppSettings["SqlServerDatabase"];
 98InBlock.gif            string strUid = ConfigurationSettings.AppSettings["SqlServerUid"];
 99InBlock.gif            string strPwd = ConfigurationSettings.AppSettings["SqlServerPwd"];
100InBlock.gif            string strConnectionString = "Server = " + strServer + "; Database = " + strDatabase + "; Uid = " + strUid + "; Pwd = " + strPwd + ";";
101InBlock.gif            return strConnectionString;
102ExpandedSubBlockEnd.gif        }

103InBlock.gif
104InBlock.gif
105InBlock.gif
106ExpandedSubBlockEnd.gif    }

107ExpandedBlockEnd.gif}

108 None.gif

最后就可以写数据访问类了,这里最好采用“事务”(DbAccess.cs)
  1 None.gif using  System;
  2 None.gif using  System.Data;
  3 None.gif using  System.Data.SqlClient;
  4 None.gif
  5 None.gif namespace  DAL
  6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  7ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary> 
  8InBlock.gif    /// DbAccess类,即进行数据库访问时需要调用的类 
  9ExpandedSubBlockEnd.gif    /// </summary> 

 10InBlock.gif    public sealed class DbAccess
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 12ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
 13InBlock.gif        /// DbAccess构造函数 
 14ExpandedSubBlockEnd.gif        /// </summary> 

 15InBlock.gif        private DbAccess()
 16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 17ExpandedSubBlockEnd.gif        }

 18InBlock.gif
 19ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
 20InBlock.gif        /// 无条件查询操作,即查询表中所有记录 
 21InBlock.gif        /// </summary> 
 22InBlock.gif        /// <param name="strTableName">表名</param> 
 23InBlock.gif        /// <param name="strColumn">列名组</param> 
 24ExpandedSubBlockEnd.gif        /// <returns>无条件查询结果</returns> 

 25InBlock.gif        public static DataSet SelectAll(string strTableName, string[] strColumn)
 26ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 27InBlock.gif            DataSet ds = new DataSet();
 28InBlock.gif            Factory factory = Factory.GetInstance();
 29InBlock.gif            AbstractDbFactory abstractDbFactory = factory.CreateInstance();
 30InBlock.gif            IDbConnection concreteDbConn = abstractDbFactory.CreateConnection();
 31InBlock.gif            concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString();
 32InBlock.gif            concreteDbConn.Open();
 33InBlock.gif            IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand();
 34InBlock.gif            IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction(concreteDbConn);
 35InBlock.gif            concreteDbCommand.Connection = concreteDbConn;
 36InBlock.gif            concreteDbCommand.Transaction = concreteDbTrans;
 37InBlock.gif            IDbDataAdapter concreteDbAdapter = abstractDbFactory.CreateDataAdapter();
 38InBlock.gif            try
 39ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 40InBlock.gif                string strSql = "SELECT ";
 41InBlock.gif                for (int i = 0; i < strColumn.Length - 1; i++)
 42ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 43InBlock.gif                    strSql += (strColumn[i] + "");
 44ExpandedSubBlockEnd.gif                }

 45InBlock.gif                strSql += (strColumn[strColumn.Length - 1+ " FROM " + strTableName);
 46InBlock.gif                concreteDbCommand.CommandText = strSql;
 47InBlock.gif                concreteDbAdapter.SelectCommand = concreteDbCommand;
 48InBlock.gif                concreteDbAdapter.Fill(ds);
 49InBlock.gif                concreteDbTrans.Commit();
 50ExpandedSubBlockEnd.gif            }

 51InBlock.gif            catch
 52ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 53InBlock.gif                concreteDbTrans.Rollback();
 54InBlock.gif                ds.Clear();
 55InBlock.gif                throw;
 56ExpandedSubBlockEnd.gif            }

 57InBlock.gif            finally
 58ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 59InBlock.gif                concreteDbConn.Close();
 60ExpandedSubBlockEnd.gif            }

 61InBlock.gif            return ds;
 62ExpandedSubBlockEnd.gif        }

 63InBlock.gif
 64ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
 65InBlock.gif        /// 条件查询操作 
 66InBlock.gif        /// </summary> 
 67InBlock.gif        /// <param name="strTableName">表名</param> 
 68InBlock.gif        /// <param name="strColumn">列名组</param> 
 69InBlock.gif        /// <param name="strCondition">条件</param> 
 70ExpandedSubBlockEnd.gif        /// <returns>条件查询结果</returns> 

 71InBlock.gif        public static DataSet Select(string strTableName, string[] strColumn, string strCondition)
 72ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 73InBlock.gif            DataSet ds = new DataSet();
 74InBlock.gif            Factory factory = Factory.GetInstance();
 75InBlock.gif            AbstractDbFactory abstractDbFactory = factory.CreateInstance();
 76InBlock.gif            IDbConnection concreteDbConn = abstractDbFactory.CreateConnection();
 77InBlock.gif            concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString();
 78InBlock.gif            concreteDbConn.Open();
 79InBlock.gif            IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand();
 80InBlock.gif            IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction(concreteDbConn);
 81InBlock.gif            concreteDbCommand.Connection = concreteDbConn;
 82InBlock.gif            concreteDbCommand.Transaction = concreteDbTrans;
 83InBlock.gif            IDbDataAdapter concreteDbAdapter = abstractDbFactory.CreateDataAdapter();
 84InBlock.gif            try
 85ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 86InBlock.gif                string strSql = "SELECT ";
 87InBlock.gif                for (int i = 0; i < strColumn.Length - 1; i++)
 88ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 89InBlock.gif                    strSql += (strColumn[i] + "");
 90ExpandedSubBlockEnd.gif                }

 91InBlock.gif                strSql += (strColumn[strColumn.Length - 1+ " FROM " + strTableName + " WHERE " + strCondition);
 92InBlock.gif                concreteDbCommand.CommandText = strSql;
 93InBlock.gif                concreteDbAdapter.SelectCommand = concreteDbCommand;
 94InBlock.gif                concreteDbAdapter.Fill(ds);
 95InBlock.gif                concreteDbTrans.Commit();
 96ExpandedSubBlockEnd.gif            }

 97InBlock.gif            catch
 98ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 99InBlock.gif                concreteDbTrans.Rollback();
100InBlock.gif                ds.Clear();
101InBlock.gif                throw;
102ExpandedSubBlockEnd.gif            }

103InBlock.gif            finally
104ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
105InBlock.gif                concreteDbConn.Close();
106ExpandedSubBlockEnd.gif            }

107InBlock.gif            return ds;
108ExpandedSubBlockEnd.gif        }

109InBlock.gif
110ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
111InBlock.gif        /// 单条记录的插入操作 
112InBlock.gif        /// </summary> 
113InBlock.gif        /// <param name="strTableName">表名</param> 
114InBlock.gif        /// <param name="strColumn">列名组</param> 
115ExpandedSubBlockEnd.gif        /// <param name="strvalue">值组</param> 

116InBlock.gif        public static void Insert(string strTableName, string[] strColumn, object[] strvalue)
117ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
118InBlock.gif            Factory factory = Factory.GetInstance();
119InBlock.gif            AbstractDbFactory abstractDbFactory = factory.CreateInstance();
120InBlock.gif            IDbConnection concreteDbConn = abstractDbFactory.CreateConnection();
121InBlock.gif            concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString();
122InBlock.gif            concreteDbConn.Open();
123InBlock.gif            IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand();
124InBlock.gif            IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction(concreteDbConn);
125InBlock.gif            concreteDbCommand.Connection = concreteDbConn;
126InBlock.gif            concreteDbCommand.Transaction = concreteDbTrans;
127InBlock.gif            try
128ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
129InBlock.gif                string strSql = "INSERT INTO " + strTableName + " (";
130InBlock.gif                for (int i = 0; i < strColumn.Length - 1; i++)
131ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
132InBlock.gif                    strSql += (strColumn[i] + "");
133ExpandedSubBlockEnd.gif                }

134InBlock.gif                strSql += (strColumn[strColumn.Length - 1+ ") valueS (’");
135InBlock.gif                for (int i = 0; i < strvalue.Length - 1; i++)
136ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
137InBlock.gif                    strSql += (strvalue[i] + "’, ’");
138ExpandedSubBlockEnd.gif                }

139InBlock.gif                strSql += (strvalue[strvalue.Length - 1+ "’)");
140InBlock.gif                concreteDbCommand.CommandText = strSql;
141InBlock.gif                concreteDbCommand.ExecuteNonQuery();
142InBlock.gif                concreteDbTrans.Commit();
143ExpandedSubBlockEnd.gif            }

144InBlock.gif            catch
145ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
146InBlock.gif                concreteDbTrans.Rollback();
147InBlock.gif                throw;
148ExpandedSubBlockEnd.gif            }

149InBlock.gif            finally
150ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
151InBlock.gif                concreteDbConn.Close();
152ExpandedSubBlockEnd.gif            }

153ExpandedSubBlockEnd.gif        }

154InBlock.gif
155ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
156InBlock.gif        /// 批量记录的插入操作,即可一次向多张表中插入不同的批量记录 
157InBlock.gif        /// </summary> 
158ExpandedSubBlockEnd.gif        /// <param name="ds">批量记录组成的DataSet,DataSet中的各个DataTable名为表名,各DataTable中的DataColumn名为列名</param> 

159InBlock.gif        public static void InsertSet(ref DataSet ds)
160ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
161InBlock.gif            Factory factory = Factory.GetInstance();
162InBlock.gif            AbstractDbFactory abstractDbFactory = factory.CreateInstance();
163InBlock.gif            IDbConnection concreteDbConn = abstractDbFactory.CreateConnection();
164InBlock.gif            concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString();
165InBlock.gif            concreteDbConn.Open();
166InBlock.gif            IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand();
167InBlock.gif            IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction(concreteDbConn);
168InBlock.gif            concreteDbCommand.Connection = concreteDbConn;
169InBlock.gif            concreteDbCommand.Transaction = concreteDbTrans;
170InBlock.gif            try
171ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
172InBlock.gif                foreach (DataTable dt in ds.Tables)
173ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
174InBlock.gif                    foreach (DataRow dr in dt.Rows)
175ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
176InBlock.gif                        string strSql = "INSERT INTO " + dt.TableName + " (";
177InBlock.gif                        for (int i = 0; i < dt.Columns.Count - 1; i++)
178ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
179InBlock.gif                            strSql += (dt.Columns[i].Caption + "");
180ExpandedSubBlockEnd.gif                        }

181InBlock.gif                        strSql += (dt.Columns[dt.Columns.Count - 1].Caption + ") valueS (’");
182InBlock.gif                        for (int i = 0; i < dt.Columns.Count - 1; i++)
183ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
184InBlock.gif                            strSql += (dr[i] + "’, ’");
185ExpandedSubBlockEnd.gif                        }

186InBlock.gif                        strSql += (dr[dt.Columns.Count - 1+ "’)");
187InBlock.gif                        concreteDbCommand.CommandText = strSql;
188InBlock.gif                        concreteDbCommand.ExecuteNonQuery();
189ExpandedSubBlockEnd.gif                    }

190ExpandedSubBlockEnd.gif                }

191InBlock.gif                concreteDbTrans.Commit();
192ExpandedSubBlockEnd.gif            }

193InBlock.gif            catch
194ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
195InBlock.gif                concreteDbTrans.Rollback();
196InBlock.gif                throw;
197ExpandedSubBlockEnd.gif            }

198InBlock.gif
199InBlock.gif            finally
200ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
201InBlock.gif                concreteDbConn.Close();
202ExpandedSubBlockEnd.gif            }

203ExpandedSubBlockEnd.gif        }

204InBlock.gif
205ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
206InBlock.gif        /// 无条件删除操作,即删除表中所有记录 
207InBlock.gif        /// </summary> 
208ExpandedSubBlockEnd.gif        /// <param name="strTableName">表名</param> 

209InBlock.gif        public static void DeleteAll(string strTableName)
210ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
211InBlock.gif            Factory factory = Factory.GetInstance();
212InBlock.gif            AbstractDbFactory abstractDbFactory = factory.CreateInstance();
213InBlock.gif            IDbConnection concreteDbConn = abstractDbFactory.CreateConnection();
214InBlock.gif            concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString();
215InBlock.gif            concreteDbConn.Open();
216InBlock.gif            IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand();
217InBlock.gif            IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction(concreteDbConn);
218InBlock.gif            concreteDbCommand.Connection = concreteDbConn;
219InBlock.gif            concreteDbCommand.Transaction = concreteDbTrans;
220InBlock.gif            try
221ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
222InBlock.gif                string strSql = "DELETE FROM " + strTableName;
223InBlock.gif                concreteDbCommand.CommandText = strSql;
224InBlock.gif                concreteDbCommand.ExecuteNonQuery();
225InBlock.gif                concreteDbTrans.Commit();
226ExpandedSubBlockEnd.gif            }

227InBlock.gif            catch
228ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
229InBlock.gif                concreteDbTrans.Rollback();
230InBlock.gif                throw;
231ExpandedSubBlockEnd.gif            }

232InBlock.gif            finally
233ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
234InBlock.gif                concreteDbConn.Close();
235ExpandedSubBlockEnd.gif            }

236ExpandedSubBlockEnd.gif        }

237InBlock.gif
238ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
239InBlock.gif        /// 条件删除操作 
240InBlock.gif        /// </summary> 
241InBlock.gif        /// <param name="strTableName">表名</param> 
242ExpandedSubBlockEnd.gif        /// <param name="strCondition">条件</param> 

243InBlock.gif        public static void Delete(string strTableName, string strCondition)
244ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
245InBlock.gif            Factory factory = Factory.GetInstance();
246InBlock.gif            AbstractDbFactory abstractDbFactory = factory.CreateInstance();
247InBlock.gif            IDbConnection concreteDbConn = abstractDbFactory.CreateConnection();
248InBlock.gif            concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString();
249InBlock.gif            concreteDbConn.Open();
250InBlock.gif            IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand();
251InBlock.gif            IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction(concreteDbConn);
252InBlock.gif            concreteDbCommand.Connection = concreteDbConn;
253InBlock.gif            concreteDbCommand.Transaction = concreteDbTrans;
254InBlock.gif            try
255ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
256InBlock.gif                string strSql = "DELETE FROM " + strTableName + " WHERE " + strCondition;
257InBlock.gif                concreteDbCommand.CommandText = strSql;
258InBlock.gif                concreteDbCommand.ExecuteNonQuery();
259InBlock.gif                concreteDbTrans.Commit();
260ExpandedSubBlockEnd.gif            }

261InBlock.gif            catch
262ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
263InBlock.gif                concreteDbTrans.Rollback();
264InBlock.gif                throw;
265ExpandedSubBlockEnd.gif            }

266InBlock.gif            finally
267ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
268InBlock.gif                concreteDbConn.Close();
269ExpandedSubBlockEnd.gif            }

270ExpandedSubBlockEnd.gif        }

271InBlock.gif
272ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
273InBlock.gif        /// 无条件更新操作,即更新表中所有记录 
274InBlock.gif        /// </summary> 
275InBlock.gif        /// <param name="strTableName">表名</param> 
276InBlock.gif        /// <param name="strColumn">列名组</param> 
277ExpandedSubBlockEnd.gif        /// <param name="strvalue">值组</param> 

278InBlock.gif        public static void UpdateAll(string strTableName, string[] strColumn, object[] strvalue)
279ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
280InBlock.gif            Factory factory = Factory.GetInstance();
281InBlock.gif            AbstractDbFactory abstractDbFactory = factory.CreateInstance();
282InBlock.gif            IDbConnection concreteDbConn = abstractDbFactory.CreateConnection();
283InBlock.gif            concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString();
284InBlock.gif            concreteDbConn.Open();
285InBlock.gif            IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand();
286InBlock.gif            IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction(concreteDbConn);
287InBlock.gif            concreteDbCommand.Connection = concreteDbConn;
288InBlock.gif            concreteDbCommand.Transaction = concreteDbTrans;
289InBlock.gif            try
290ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
291InBlock.gif                string strSql = "UPDATE " + strTableName + " SET ";
292InBlock.gif                for (int i = 0; i < strColumn.Length - 1; i++)
293ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
294InBlock.gif                    strSql += (strColumn[i] + " = ’" + strvalue[i] + "’, ");
295ExpandedSubBlockEnd.gif                }

296InBlock.gif                strSql += (strColumn[strColumn.Length - 1+ " = ’" + strvalue[strvalue.Length - 1+ "’ ");
297InBlock.gif                concreteDbCommand.CommandText = strSql;
298InBlock.gif                concreteDbCommand.ExecuteNonQuery();
299InBlock.gif                concreteDbTrans.Commit();
300ExpandedSubBlockEnd.gif            }

301InBlock.gif            catch
302ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
303InBlock.gif                concreteDbTrans.Rollback();
304InBlock.gif                throw;
305ExpandedSubBlockEnd.gif            }

306InBlock.gif            finally
307ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
308InBlock.gif                concreteDbConn.Close();
309ExpandedSubBlockEnd.gif            }

310ExpandedSubBlockEnd.gif        }

311InBlock.gif
312ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
313InBlock.gif        /// 条件更新操作 
314InBlock.gif        /// </summary> 
315InBlock.gif        /// <param name="strTableName">表名</param> 
316InBlock.gif        /// <param name="strColumn">列名组</param> 
317InBlock.gif        /// <param name="strvalue">值组</param> 
318ExpandedSubBlockEnd.gif        /// <param name="strCondition">条件</param> 

319InBlock.gif        public static void Update(string strTableName, string[] strColumn, object[] strvalue, string strCondition)
320ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
321InBlock.gif            Factory factory = Factory.GetInstance();
322InBlock.gif            AbstractDbFactory abstractDbFactory = factory.CreateInstance();
323InBlock.gif            IDbConnection concreteDbConn = abstractDbFactory.CreateConnection();
324InBlock.gif            concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString();
325InBlock.gif            concreteDbConn.Open();
326InBlock.gif            IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand();
327InBlock.gif            IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction(concreteDbConn);
328InBlock.gif            concreteDbCommand.Connection = concreteDbConn;
329InBlock.gif            concreteDbCommand.Transaction = concreteDbTrans;
330InBlock.gif            try
331ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
332InBlock.gif                string strSql = "UPDATE " + strTableName + " SET ";
333InBlock.gif                for (int i = 0; i < strColumn.Length - 1; i++)
334ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
335InBlock.gif                    strSql += (strColumn[i] + " = ’" + strvalue[i] + "’, ");
336ExpandedSubBlockEnd.gif                }

337InBlock.gif                strSql += (strColumn[strColumn.Length - 1+ " = ’" + strvalue[strvalue.Length - 1+ "’ " + " WHERE " + strCondition);
338InBlock.gif                concreteDbCommand.CommandText = strSql;
339InBlock.gif                concreteDbCommand.ExecuteNonQuery();
340InBlock.gif                concreteDbTrans.Commit();
341ExpandedSubBlockEnd.gif            }

342InBlock.gif            catch
343ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
344InBlock.gif                concreteDbTrans.Rollback();
345InBlock.gif                throw;
346ExpandedSubBlockEnd.gif            }

347InBlock.gif            finally
348ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
349InBlock.gif                concreteDbConn.Close();
350ExpandedSubBlockEnd.gif            }

351ExpandedSubBlockEnd.gif        }

352InBlock.gif
353ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
354InBlock.gif        ///  多参数存储过程询操作
355InBlock.gif        /// </summary> 
356InBlock.gif        /// <param name="strTableName">参数名列表</param> 
357InBlock.gif        /// <param name="strColumn">参数值列表</param> 
358ExpandedSubBlockEnd.gif        /// <returns>查询所得结果</returns> 

359InBlock.gif        public static DataSet SelectAllProc(string strProcname, string[] strParam, object[] strValue)
360ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
361InBlock.gif            DataSet ds = new DataSet();
362InBlock.gif            Factory factory = Factory.GetInstance();
363InBlock.gif            AbstractDbFactory abstractDbFactory = factory.CreateInstance();
364InBlock.gif            IDbConnection concreteDbConn = abstractDbFactory.CreateConnection();
365InBlock.gif            concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString();
366InBlock.gif            concreteDbConn.Open();
367InBlock.gif            IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand();
368InBlock.gif            IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction(concreteDbConn);
369InBlock.gif            concreteDbCommand.Connection = concreteDbConn;
370InBlock.gif            concreteDbCommand.Transaction = concreteDbTrans;
371InBlock.gif            concreteDbCommand.CommandText = strProcname;
372InBlock.gif            concreteDbCommand.CommandType = CommandType.StoredProcedure;
373InBlock.gif            IDbDataAdapter concreteDbAdapter = abstractDbFactory.CreateDataAdapter();
374InBlock.gif            try
375ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
376InBlock.gif                concreteDbAdapter.SelectCommand = concreteDbCommand;
377InBlock.gif                for (int i = 0; i < strParam.Length; i++)
378ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
379InBlock.gif                    IDataParameter adminid = abstractDbFactory.CreateDataParamter(strParam[i], strValue[i]);
380InBlock.gif                    concreteDbAdapter.SelectCommand.Parameters.Add(adminid);
381ExpandedSubBlockEnd.gif                }

382InBlock.gif                concreteDbAdapter.Fill(ds);
383InBlock.gif                concreteDbTrans.Commit();
384ExpandedSubBlockEnd.gif            }

385InBlock.gif            catch
386ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
387InBlock.gif                concreteDbTrans.Rollback();
388InBlock.gif                ds.Clear();
389InBlock.gif                throw;
390ExpandedSubBlockEnd.gif            }

391InBlock.gif            finally
392ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
393InBlock.gif                concreteDbConn.Close();
394ExpandedSubBlockEnd.gif            }

395InBlock.gif            return ds;
396ExpandedSubBlockEnd.gif        }

397InBlock.gif
398ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
399InBlock.gif        /// 单参数存储过程调用
400InBlock.gif        /// </summary>
401InBlock.gif        /// <param name="strProcname">存储过程名</param>
402InBlock.gif        /// <param name="strParam">参数名</param>
403InBlock.gif        /// <param name="strValue">参数值</param>
404ExpandedSubBlockEnd.gif        /// <returns>查询结果</returns>

405InBlock.gif        public static DataSet SelectProc(string strProcname, string strParam, string strValue)
406ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
407InBlock.gif            DataSet ds = new DataSet();
408InBlock.gif            Factory factory = Factory.GetInstance();
409InBlock.gif            AbstractDbFactory abstractDbFactory = factory.CreateInstance();
410InBlock.gif            IDbConnection concreteDbConn = abstractDbFactory.CreateConnection();
411InBlock.gif            concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString();
412InBlock.gif            concreteDbConn.Open();
413InBlock.gif            IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand();
414InBlock.gif            IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction(concreteDbConn);
415InBlock.gif            concreteDbCommand.Connection = concreteDbConn;
416InBlock.gif            concreteDbCommand.Transaction = concreteDbTrans;
417InBlock.gif            concreteDbCommand.CommandText = strProcname;
418InBlock.gif            concreteDbCommand.CommandType = CommandType.StoredProcedure;
419InBlock.gif            IDataParameter concreteDataParameter = abstractDbFactory.CreateDataParamter(strParam, strValue);
420InBlock.gif            IDbDataAdapter concreteDbAdapter = abstractDbFactory.CreateDataAdapter();
421InBlock.gif            try
422ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
423InBlock.gif                concreteDbAdapter.SelectCommand = concreteDbCommand;
424InBlock.gif                concreteDbAdapter.SelectCommand.Parameters.Add(concreteDataParameter);
425InBlock.gif                concreteDbAdapter.Fill(ds);
426InBlock.gif                concreteDbTrans.Commit();
427ExpandedSubBlockEnd.gif            }

428InBlock.gif            catch
429ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
430InBlock.gif                concreteDbTrans.Rollback();
431InBlock.gif                ds.Clear();
432InBlock.gif                throw;
433ExpandedSubBlockEnd.gif            }

434InBlock.gif            finally
435ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
436InBlock.gif                concreteDbConn.Close();
437ExpandedSubBlockEnd.gif            }

438InBlock.gif            return ds;
439ExpandedSubBlockEnd.gif        }

440InBlock.gif
441ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
442InBlock.gif        /// 无返回值的存储过程
443InBlock.gif        /// </summary>
444InBlock.gif        /// <param name="strProcname">存储过程名</param>
445InBlock.gif        /// <param name="strParam">参数列表</param>
446ExpandedSubBlockEnd.gif        /// <param name="strValue">参数值列表</param>

447InBlock.gif        public static void NoReturnProc(string strProcname, string[] strParam, object[] strValue)
448ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
449InBlock.gif            Factory factory = Factory.GetInstance();
450InBlock.gif            AbstractDbFactory abstractDbFactory = factory.CreateInstance();
451InBlock.gif            IDbConnection concreteDbConn = abstractDbFactory.CreateConnection();
452InBlock.gif            concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString();
453InBlock.gif            concreteDbConn.Open();
454InBlock.gif            IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand();
455InBlock.gif            IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction(concreteDbConn);
456InBlock.gif            concreteDbCommand.Connection = concreteDbConn;
457InBlock.gif            concreteDbCommand.Transaction = concreteDbTrans;
458InBlock.gif            concreteDbCommand.CommandText = strProcname;
459InBlock.gif            concreteDbCommand.CommandType = CommandType.StoredProcedure;
460InBlock.gif            try
461ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
462InBlock.gif                for (int i = 0; i < strParam.Length; i++)
463ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
464InBlock.gif                    IDataParameter adminid = abstractDbFactory.CreateDataParamter(strParam[i], strValue[i]);
465InBlock.gif                    concreteDbCommand.Parameters.Add(adminid);
466ExpandedSubBlockEnd.gif                }

467InBlock.gif                concreteDbCommand.ExecuteNonQuery();
468InBlock.gif                concreteDbTrans.Commit();
469ExpandedSubBlockEnd.gif            }

470InBlock.gif            catch
471ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
472InBlock.gif                concreteDbTrans.Rollback();
473InBlock.gif                throw;
474ExpandedSubBlockEnd.gif            }

475InBlock.gif            finally
476ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
477InBlock.gif                concreteDbConn.Close();
478ExpandedSubBlockEnd.gif            }

479ExpandedSubBlockEnd.gif        }

480InBlock.gif
481ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
482InBlock.gif        /// 用户登录验证
483InBlock.gif        /// </summary>
484InBlock.gif        /// <param name="strProcname">存储过程名</param>
485InBlock.gif        /// <param name="strID">用户ID</param>
486InBlock.gif        /// <param name="btPassword">密码</param>
487ExpandedSubBlockEnd.gif        /// <returns></returns>

488InBlock.gif        public static DataSet Login(string strProcname, string strID, byte[] btPassword)
489ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
490InBlock.gif            DataSet ds = new DataSet();
491InBlock.gif            Factory factory = Factory.GetInstance();
492InBlock.gif            AbstractDbFactory abstractDbFactory = factory.CreateInstance();
493InBlock.gif            IDbConnection concreteDbConn = abstractDbFactory.CreateConnection();
494InBlock.gif            concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString();
495InBlock.gif            concreteDbConn.Open();
496InBlock.gif            IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand();
497InBlock.gif            IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction(concreteDbConn);
498InBlock.gif            concreteDbCommand.Connection = concreteDbConn;
499InBlock.gif            concreteDbCommand.Transaction = concreteDbTrans;
500InBlock.gif            concreteDbCommand.CommandText = strProcname;
501InBlock.gif            concreteDbCommand.CommandType = CommandType.StoredProcedure;
502InBlock.gif            IDbDataAdapter concreteDbAdapter = abstractDbFactory.CreateDataAdapter();
503InBlock.gif            try
504ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
505InBlock.gif                concreteDbAdapter.SelectCommand = concreteDbCommand;
506InBlock.gif                IDataParameter concreteParametersID = abstractDbFactory.CreateDataParamter("@AdminID", SqlDbType.Char);
507InBlock.gif                SqlParameter concreteParametersPW = new SqlParameter("@Password", SqlDbType.Binary);
508InBlock.gif                concreteParametersID.Value = strID;
509InBlock.gif                concreteParametersPW.Value = btPassword;
510InBlock.gif                concreteDbAdapter.SelectCommand.Parameters.Add(concreteParametersID);
511InBlock.gif                concreteDbAdapter.SelectCommand.Parameters.Add(concreteParametersPW);
512InBlock.gif                concreteDbAdapter.Fill(ds);
513InBlock.gif                concreteDbTrans.Commit();
514ExpandedSubBlockEnd.gif            }

515InBlock.gif            catch
516ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
517InBlock.gif                concreteDbTrans.Rollback();
518InBlock.gif                ds.Clear();
519InBlock.gif                throw;
520ExpandedSubBlockEnd.gif            }

521InBlock.gif            finally
522ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
523InBlock.gif                concreteDbConn.Close();
524ExpandedSubBlockEnd.gif            }

525InBlock.gif            return ds;
526ExpandedSubBlockEnd.gif        }

527InBlock.gif
528ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
529InBlock.gif        /// 用户登录验证
530InBlock.gif        /// </summary>
531InBlock.gif        /// <param name="strProcname">存储过程名</param>
532InBlock.gif        /// <param name="strID">用户ID</param>
533InBlock.gif        /// <param name="btPassword">密码</param>
534ExpandedSubBlockEnd.gif        /// <returns></returns>

535InBlock.gif        public static DataSet ULogin(string strProcname, string strID, byte[] btPassword)
536ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
537InBlock.gif            DataSet ds = new DataSet();
538InBlock.gif            Factory factory = Factory.GetInstance();
539InBlock.gif            AbstractDbFactory abstractDbFactory = factory.CreateInstance();
540InBlock.gif            IDbConnection concreteDbConn = abstractDbFactory.CreateConnection();
541InBlock.gif            concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString();
542InBlock.gif            concreteDbConn.Open();
543InBlock.gif            IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand();
544InBlock.gif            IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction(concreteDbConn);
545InBlock.gif            concreteDbCommand.Connection = concreteDbConn;
546InBlock.gif            concreteDbCommand.Transaction = concreteDbTrans;
547InBlock.gif            concreteDbCommand.CommandText = strProcname;
548InBlock.gif            concreteDbCommand.CommandType = CommandType.StoredProcedure;
549InBlock.gif            IDbDataAdapter concreteDbAdapter = abstractDbFactory.CreateDataAdapter();
550InBlock.gif            try
551ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
552InBlock.gif                concreteDbAdapter.SelectCommand = concreteDbCommand;
553InBlock.gif                IDataParameter concreteParametersID = abstractDbFactory.CreateDataParamter("@UserID", SqlDbType.Char);
554InBlock.gif                SqlParameter concreteParametersPW = new SqlParameter("@Password", SqlDbType.Binary);
555InBlock.gif                concreteParametersID.Value = strID;
556InBlock.gif                concreteParametersPW.Value = btPassword;
557InBlock.gif                concreteDbAdapter.SelectCommand.Parameters.Add(concreteParametersID);
558InBlock.gif                concreteDbAdapter.SelectCommand.Parameters.Add(concreteParametersPW);
559InBlock.gif                concreteDbAdapter.Fill(ds);
560InBlock.gif                concreteDbTrans.Commit();
561ExpandedSubBlockEnd.gif            }

562InBlock.gif            catch
563ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
564InBlock.gif                concreteDbTrans.Rollback();
565InBlock.gif                ds.Clear();
566InBlock.gif                throw;
567ExpandedSubBlockEnd.gif            }

568InBlock.gif            finally
569ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
570InBlock.gif                concreteDbConn.Close();
571ExpandedSubBlockEnd.gif            }

572InBlock.gif            return ds;
573ExpandedSubBlockEnd.gif        }

574ExpandedSubBlockEnd.gif    }

575ExpandedBlockEnd.gif}

576 None.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值