DbHelper.cs and OracleHelper.cs and so on

1 连接Sql Server的ConnectionString:

Data Source=.;Initial Catalog=ViewVideo;User ID=sa;Password=XXXXXX;

2 连接MySql的ConnectionString:

Database=dbname;Data Source=192.168.1.1;Port=3306;User Id=root;Password=****;Charset=utf8;TreatTinyAsBoolean=false;

3 连接Oracle的ConnectionString:

Data Source=(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.117)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl.192.168.1.117)
)
);User Id=scott;Password=XXXXXX //不能有';'

 

OracleHelper.cs:

  1  class OracleHelper

  2     {

  3         private static readonly string connStr = ConfigurationManager.ConnectionStrings["dbconnStr"].ConnectionString;

  4 

  5         /// <summary>

  6         /// 创建一个连接

  7         /// </summary>

  8         /// <returns></returns>

  9         public static OracleConnection CreateConnection()

 10         {

 11             OracleConnection conn = new OracleConnection(connStr);

 12             conn.Open();

 13             return conn;

 14         }

 15 

 16         //使用已有连接 非查询

 17         public static int ExecuteNonQuery(OracleConnection conn,string sql,params OracleParameter[] parameters)

 18         { 

 19             using(OracleCommand cmd=new OracleCommand(sql,conn))

 20             {

 21                 cmd.Parameters.AddRange(parameters);

 22                 return cmd.ExecuteNonQuery();

 23             }

 24         }

 25 

 26      //使用已有连接 非查询 事务处理

 27         public static int ExecuteNonQuery(OracleConnection conn,OracleTransaction tx, string sql, params OracleParameter[] parameters)

 28         {

 29             using (OracleCommand cmd = new OracleCommand(sql, conn))

 30             {

 31                 cmd.Transaction = tx;

 32                 cmd.Parameters.AddRange(parameters);

 33                 return cmd.ExecuteNonQuery();

 34             }

 35         }

 36 

 37         //自己创建连接 非查询

 38         public static int ExecuteNonQuery(string sql,params OracleParameter[] parameters)

 39         { 

 40             using(OracleConnection conn=CreateConnection())

 41             {

 42                 return ExecuteNonQuery(conn, sql, parameters);

 43             }

 44         }

 45 

 46         //使用已有连接 单查询

 47         public static object ExecuteScalar(OracleConnection conn,string sql,params OracleParameter[] parameters)

 48         { 

 49             using(OracleCommand cmd=new OracleCommand(sql,conn))

 50             {

 51                 return cmd.ExecuteScalar();

 52             }

 53         }

 54 

 55         //自己创建连接 单查询

 56         public static object ExecuteScalar(string sql,params OracleParameter[] parameters)

 57         { 

 58             using(OracleConnection conn=CreateConnection())

 59             {

 60                 return ExecuteScalar(conn, sql, parameters);

 61             }

 62         }

 63 

 64         //使用已有连接 Reader多查询

 65         public static DataTable ExecuteReader(OracleConnection conn,string sql,params OracleParameter[] parameters)

 66         {

 67             DataTable table = new DataTable();

 68             using(OracleCommand cmd=new OracleCommand(sql,conn))

 69             {

 70                 cmd.Parameters.AddRange(parameters);

 71                 using (OracleDataReader reader = cmd.ExecuteReader())

 72                 {

 73                     table.Load(reader);

 74                 }

 75             }

 76             return table;

 77         }

 78 

 79         //自己创建连接 Reader多查询

 80         public static DataTable ExecuteReader(string sql,params OracleParameter[] parameters)

 81         { 

 82             using(OracleConnection conn=CreateConnection())

 83             {

 84                 return ExecuteReader(conn, sql, parameters);

 85             }

 86         }

 87 

 88         //使用已有连接 DataAdapter多查询

 89         public static DataTable ExecuteDataAdapter(OracleConnection conn, string sql, params OracleParameter[] parameters)

 90         { 

 91             using(OracleCommand cmd=new OracleCommand(sql,conn))

 92             {

 93                 cmd.Parameters.AddRange(parameters);

 94                 OracleDataAdapter adapter = new OracleDataAdapter(cmd);

 95                 DataSet dataset = new DataSet();

 96                 //adapter.FillSchema(dataset, SchemaType.Source);

 97                 adapter.Fill(dataset);

 98                 return dataset.Tables[0];

 99             }

100         }

101 

102         //自己创建连接 DataAdapter多查询

103         public static DataTable ExecuteDataAdapter(string sql, params OracleParameter[] parameters)

104         { 

105             using(OracleConnection conn=CreateConnection())

106             {

107                 return ExecuteDataAdapter(conn, sql, parameters);

108             }

109         }

110     }
OracleHelper.cs

DbHelper.cs

  1 public class DbHelper 

  2     {

  3         private static readonly string connStr = ConfigurationManager.ConnectionStrings["dbconnStr"].ConnectionString;

  4 

  5         //创建连接

  6         public static IDbConnection CreateConnection()

  7         {

  8             IDbConnection conn = new OracleConnection(connStr);

  9             conn.Open();

 10             return conn;

 11         }

 12 

 13         //把字典中的参数都加入到cmd得参数集合中

 14         public static void DictAddToCmdParameters(IDbCommand cmd,Dictionary<string,object> dict)

 15         { 

 16             foreach(KeyValuePair<string,object> kvp in dict)

 17             {

 18                 IDataParameter parameters = cmd.CreateParameter();

 19                 parameters.ParameterName = kvp.Key;

 20                 parameters.Value = kvp.Value;

 21                 cmd.Parameters.Add(parameters);

 22             }

 23         }

 24 

 25         //使用已有连接的 非查询

 26         public static int ExecuteNonQuery(IDbConnection conn,string sql,Dictionary<string,object> dict)

 27         { 

 28             using(IDbCommand cmd=conn.CreateCommand())

 29             {

 30                 cmd.CommandText = sql;

 31                 DictAddToCmdParameters(cmd, dict);

 32                 return cmd.ExecuteNonQuery();

 33             }

 34         }

 35 

 36         //使用已有连接的 非查询 带事务处理

 37         public static int ExecuteNonQuery(IDbConnection conn,IDbTransaction tx,string sql,Dictionary<string,object> dict)

 38         { 

 39             using(IDbCommand cmd=conn.CreateCommand())

 40             {

 41                 cmd.Transaction = tx;

 42                 cmd.CommandText = sql;

 43                 DictAddToCmdParameters(cmd, dict);

 44                 return cmd.ExecuteNonQuery();

 45             }

 46         }

 47 

 48         //自己创建连接的 非查询

 49         public static int ExecuteNonQuery(string sql,Dictionary<string,object> dict)

 50         {

 51             using(IDbConnection conn=CreateConnection())

 52             {

 53                 return ExecuteNonQuery(sql,dict);

 54             }

 55         }

 56 

 57         //使用已有连接的 单查询

 58         public static object ExecuteScalar(IDbConnection conn,string sql,Dictionary<string,object> dict)

 59         { 

 60             using(IDbCommand cmd=conn.CreateCommand())

 61             {

 62                 cmd.CommandText = sql;

 63                 DictAddToCmdParameters(cmd, dict);

 64                 return cmd.ExecuteScalar();

 65             }

 66         }

 67 

 68         //自己创建连接的 单查询

 69         public static object ExecuteScalar(string sql,Dictionary<string,object> dict)

 70         { 

 71             using(IDbConnection conn=CreateConnection())

 72             {

 73                 return ExecuteScalar(conn, sql, dict);

 74             }

 75         }

 76 

 77         //使用已有连接的 Reader查询

 78         public static DataTable ExecuteReader(IDbConnection conn,string sql,Dictionary<string,object> dict)

 79         {

 80             DataTable table = new DataTable();

 81             using(IDbCommand cmd=conn.CreateCommand())

 82             {

 83                 cmd.CommandText = sql;

 84                 DictAddToCmdParameters(cmd, dict);

 85                 using (IDataReader reader = cmd.ExecuteReader())

 86                 {

 87                     table.Load(reader);

 88                 }

 89             }

 90             return table;

 91         }

 92 

 93         //自己创建连接的 Reader查询 

 94         public static DataTable ExecuteReader(string sql,Dictionary<string,object> dict)

 95         { 

 96             using(IDbConnection conn=CreateConnection())

 97             {

 98                 return ExecuteReader(conn, sql, dict);

 99             }

100         }

101 

102         //使用已有连接的 DataAdapter查询

103         public static DataTable ExecuteDataAdapter(IDbConnection conn,string sql,Dictionary<string,object> dict)

104         { 

105             using(IDbCommand cmd=conn.CreateCommand())

106             {

107                 cmd.CommandText = sql;

108                 DictAddToCmdParameters(cmd, dict);

109                 IDataAdapter adapter = GetDataAdapter(cmd);//获得查询的数据库结果

110                 DataSet dataset = new DataSet();

111                 adapter.Fill(dataset);

112                 return dataset.Tables[0];

113             }

114         }

115 

116         //自己创建连接的 DataAdapter查询

117         public static DataTable ExecuteDataAdapter(string sql,Dictionary<string,object> dict)

118         { 

119             using(IDbConnection conn=CreateConnection())

120             {

121                 return ExecuteDataAdapter(conn, sql, dict);

122             }

123         }

124 

125         //获得查询到的数据库结果

126         private static IDataAdapter GetDataAdapter(IDbCommand cmd)

127         {

128             IDataAdapter adapter;//DataAdapter的访问限制为protected,不能new

129             if (cmd is OracleCommand)//需引用Oracle.DataAccess.Client

130             {

131                 OracleCommand oracmd = cmd as OracleCommand;

132                 adapter = new OracleDataAdapter(oracmd);

133             }

134             //else if (cmd is MySqlCommand)//需引用mysql.data.client

135             //{

136             //    MySqlCommand mycom = cmd as MySqlCommand;

137             //    adapter = new MySqlDataAdapter(mycom);

138             //}

139             else if (cmd is SqlCommand)//需引用System.Data.SqlClient;

140             {

141                 SqlCommand sqlcom = cmd as SqlCommand;

142                 adapter = new SqlDataAdapter(sqlcom);

143             }

144             else

145             {

146                 throw new Exception("需要其他数据库的DataAdapter");

147             }

148             return adapter;

149         }

150 

151         /// <summary>

152         /// 执行SqlBulkCopy或OracleBulkCopy批量拷贝,传入一个实体的公共类型,约定其属性名与DB中列名相同

153         /// </summary>

154         /// <param name="type"></param>

155         /// <returns>返回该类型对应的表的架构</returns>

156         public static DataTable ExecuteBulkCopy(Type type)

157         {

158             //创建表的架构

159             DataTable table = new DataTable();

160             DataColumn column = new DataColumn();

161             //Type type = typeof(Person);//传入实体类Person

162             PropertyInfo[] properties = type.GetProperties();

163             //创建表的列

164             foreach (PropertyInfo property in properties)

165             {

166                 column.ColumnName = property.Name;

167                 column.DataType = property.PropertyType;

168                 table.Columns.Add(column);

169             }

170             return table;

171         }

172 

173         /// <summary>

174         /// 遍历: 把每个数据中数据依次填充到该表的row中

175         /// </summary>

176         /// <param name="table"></param>

177         /// <param name="type"></param>

178         /// <param name="line"></param>

179         /// <returns>返回填充了一行数据的表</returns>

180         public static DataTable RowAddTable(DataTable table, Type type, string[] line)//line中个数必须与列数量相同

181         {

182             //填入表的行

183             DataRow row = table.NewRow();

184             PropertyInfo[] properties = type.GetProperties();

185             int i = 0;

186             foreach (PropertyInfo property in properties)//属性名对应列名

187             {

188                 row[property.Name] = line[i];

189                 i++;

190             }

191             table.Rows.Add(row);

192             return table;

193         }

194 

195         /// <summary>

196         /// 把当前表写入目标表(目标表和当前表列名相同)

197         /// </summary>

198         /// <param name="destTableName"></param>

199         /// <param name="currentTabele"></param>

200         public static bool BulkCopyMapping(string destTableName, DataTable currentTabele)

201         {

202             bool falg = false;

203             //using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connStr))

204             using (OracleBulkCopy bulkCopy = new OracleBulkCopy(connStr))//oracle在这里不能用事务,sql server可以

205             {

206                 

207                 bulkCopy.DestinationTableName = destTableName;

208                 foreach (DataColumn column in currentTabele.Columns)

209                 {

210                     bulkCopy.ColumnMappings.Add(column.ColumnName, column.ColumnName);//目标表和当前表列名相同

211                 }

212                 try

213                 {

214                     bulkCopy.WriteToServer(currentTabele);//把当前表写入目标表

215                     falg = true;

216                 }

217                 catch (Exception ex)

218                 {

219                     falg = false;

220                     throw new Exception(ex.ToString());

221                 }

222             }

223             return falg;

224         }

225     }

226     class Person//BulkCopy直接传了Type所以没用到

227     {

228         public int Id{get;set;}

229         public string Name{get;set;}

230     }
DbHelper.cs

 

你可能感兴趣的:(oracle)