entity framework扩展实战,小项目重构,不折腾
一个控制台程序,MSSQL 2008 数据库,其中一个大表数据超过6000千万,开发时间有限不可能花很多时间设计、架构。使用控制台一是为了实现高效运行,二是程序是在服务器上定时运行无人干预。要保证程序可维护性和快速开发和程序高效运行,没时间去分层开发,直接使用了entity framework并结合ado.net对EF进行了扩展。不折腾是因为怕EF有性能问题使用了sqlbuckcopy进行批量插入数据,使用DB FIRST。重构是因为原来是oracle数据库,数据库已经改为MSSQL 2008。
1、解决方案、架构
解决方案:
2、业务说明
程序主要业务是读取指定目录下面的XML文件,将里面的文件里的记录整理拼凑为数据库里对应表的datatable,然后使用SqlBulkCopy批量插入数据库。文件是远程计算机通过FTP上传的。XML里面的记录和需要存储的数据库里对应表的表结构不一致,需要转换和补全。程序是定时运行,使用操作系统的计划任务进行调度。日志组件使用了log4net,来实现控制台显示和数据库记录日志。
3、关键代码和编码思路
1、业务逻辑实现
2、EF扩展类
using System.Data; using System.Data.EntityClient; using System; using System.Data.SqlClient; using System.Configuration; namespace XXX_App { public partial class XXX_DBEntities : global::System.Data.Objects.ObjectContext { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); /// <summary> /// 使用ado.net执行sqlbulkcopy /// </summary> /// <param name="dt"></param> /// <param name="efConnectStr">webconfig里的数据库连接字符串</param> /// <returns></returns> public bool ExecuteSqlBulkCopy(DataTable dt) { string dbConnStr = this.GetEF_ConnectStr2Ado_ConnectStr(); bool result = false; #region ado.net execute sqlbulkcopy using (SqlConnection dbConn = new SqlConnection(dbConnStr)) { try { #region 执行sqlbulkcopy dbConn.Open(); SqlCommand cmd = dbConn.CreateCommand(); cmd.CommandText = "delete sc_productsource_temp"; cmd.ExecuteNonQuery(); new SqlBulkCopy(dbConn) { DestinationTableName = "sc_productsource_temp", BatchSize = dt.Rows.Count, BulkCopyTimeout = 1800 }.WriteToServer(dt); #endregion result = true; } catch (Exception ex) { result = false; throw new Exception(ex.Message); } finally { if (result) { #region 临时表流量明细记录转正式 SC_PRODUCTSOURCE SqlCommand cmd = dbConn.CreateCommand(); cmd.CommandText = " INSERT INTO SC_PRODUCTSOURCE (PRODUCTSOURCEID,FACTORYID,PRDLINEID "+ " ,MACRANDOMCODE ,PRODUCTBATCHIDX ,LASERDNA_CODE ,PRODUCTNO ,PRODUCTNAME,PRODUCTTYPENAME"+ " ,PRODUCTDATE ,PRINTCODEDT ,FACTORYSHIFTNO ,OUTBOX_DNACODE ,VIRTUALBALECODE" + " ,SENDTONDC_DT,FAC_NO,PRODUCTVERSION) " + " SELECT NEWID() PRODUCTSOURCEID,FACTORYID,PRDLINEID,MACRANDOMCODE,PRODUCTBATCHIDX " + " ,LASERDNA_CODE,PRODUCTNO,PRODUCTNAME,PRODUCTTYPENAME,PRODUCTDATE,PRINTCODEDT " + " ,FACTORYSHIFTNO,OUTBOX_DNACODE,VIRTUALBALECODE,SENDTONDC_DT,FAC_NO,PRODUCTVERSION " + " FROM sc_productsource_temp"; cmd.ExecuteNonQuery(); #endregion } dbConn.Close(); } } #endregion return result; } /// <summary> /// 准备临时表 /// </summary> /// <param name="efConnectStr"></param> /// <returns></returns> public DataTable GetTmpNewDataTable() { return this.GetDbHelperSQL_Instance().Query("select * from sc_productsource_temp where 1=2").Tables[0]; } /// <summary> /// 将EF的连接字符串转为Ado.net的连接字符串 /// </summary> /// <param name="efConnectStr"></param> /// <returns></returns> private string GetEF_ConnectStr2Ado_ConnectStr() { string[] tmpStr = config.ConnectionStrings.ConnectionStrings["XXX_DBEntities"].ConnectionString.Split(";".ToCharArray()); string dbConnStr = string.Empty; if (tmpStr.Length == 8) { //拼接出新的可用的ado.net连接字符串 dbConnStr = string.Join(";", new string[] { "Data Source =" + base.Connection.DataSource, tmpStr[3], tmpStr[4], tmpStr[5], tmpStr[6].TrimEnd(new char[] { '"' }) }); return dbConnStr; } else { return string.Empty; throw new Exception("请配置数据库连接方式为:sql认证方式;不支持WinNT集成安全方式。"); } } /// <summary> /// 获取第一行第一列数据 /// </summary> /// <param name="efConnectStr"></param> /// <param name="sqlTxt"></param> /// <returns></returns> public string AdoExecuteScalar(string sqlTxt) { string dbConnStr = this.GetEF_ConnectStr2Ado_ConnectStr(); using (DbHelperSQL db = new DbHelperSQL(dbConnStr)) { return db.GetSingle(sqlTxt).ToString(); } } public DbHelperSQL GetDbHelperSQL_Instance() { string dbConnStr = this.GetEF_ConnectStr2Ado_ConnectStr(); return new DbHelperSQL(dbConnStr); } } }
3、修改过的动软DbHelper,Ado.net存储类
using System; using System.Collections; using System.Collections.Specialized; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Data.Common; using System.Collections.Generic; namespace XXX_App { /// <summary> /// 数据访问抽象基础类 /// Copyright (C) Maticsoft /// </summary> public class DbHelperSQL : IDisposable { //数据库连接字符串(web.config来配置),多数据库可使用DbHelperSQLP来实现. private string connectionString; /// <summary> /// 数据库连接字符串 /// </summary> public string ConnectionString { get { return connectionString; } } public DbHelperSQL(string connStr) { connectionString = connStr; } #region 公用方法 /// <summary> /// 判断是否存在某表的某个字段 /// </summary> /// <param name="tableName">表名称</param> /// <param name="columnName">列名称</param> /// <returns>是否存在</returns> public bool ColumnExists(string tableName, string columnName) { string sql = "select count(1) from syscolumns where [id]=object_id('" + tableName + "') and [name]='" + columnName + "'"; object res = GetSingle(sql); if (res == null) { return false; } return Convert.ToInt32(res) > 0; } public int GetMaxID(string FieldName, string TableName) { string strsql = "select max(" + FieldName + ")+1 from " + TableName; object obj = GetSingle(strsql); if (obj == null) { return 1; } else { return int.Parse(obj.ToString()); } } public bool Exists(string strSql) { object obj = GetSingle(strSql); int cmdresult; if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { cmdresult = 0; } else { cmdresult = int.Parse(obj.ToString()); //也可能=0 } if (cmdresult == 0) { return false; } else { return true; } } /// <summary> /// 表是否存在 /// </summary> /// <param name="TableName"></param> /// <returns></returns> public bool TabExists(string TableName) { string strsql = "select count(*) from sysobjects where id = object_id(N'[" + TableName + "]') and OBJECTPROPERTY(id, N'IsUserTable') = 1"; //string strsql = "SELECT count(*) FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[" + TableName + "]') AND type in (N'U')"; object obj = GetSingle(strsql); int cmdresult; if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { cmdresult = 0; } else { cmdresult = int.Parse(obj.ToString()); } if (cmdresult == 0) { return false; } else { return true; } } public bool Exists(string strSql, params SqlParameter[] cmdParms) { object obj = GetSingle(strSql, cmdParms); int cmdresult; if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { cmdresult = 0; } else { cmdresult = int.Parse(obj.ToString()); } if (cmdresult == 0) { return false; } else { return true; } } #endregion #region 执行简单SQL语句 /// <summary> /// 执行SQL语句,返回影响的记录数 /// </summary> /// <param name="SQLString">SQL语句</param> /// <returns>影响的记录数</returns> public int ExecuteSql(string SQLString) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { connection.Open(); int rows = cmd.ExecuteNonQuery(); return rows; } catch (System.Data.SqlClient.SqlException e) { connection.Close(); throw e; } } } } public int ExecuteSqlByTime(string SQLString, int Times) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { connection.Open(); cmd.CommandTimeout = Times; int rows = cmd.ExecuteNonQuery(); return rows; } catch (System.Data.SqlClient.SqlException e) { connection.Close(); throw e; } } } } /// <summary> /// 执行多条SQL语句,实现数据库事务。 /// </summary> /// <param name="SQLStringList">多条SQL语句</param> public int ExecuteSqlTran(List<String> SQLStringList) { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; SqlTransaction tx = conn.BeginTransaction(); cmd.Transaction = tx; try { int count = 0; for (int n = 0; n < SQLStringList.Count; n++) { string strsql = SQLStringList[n]; if (strsql.Trim().Length > 1) { cmd.CommandText = strsql; count += cmd.ExecuteNonQuery(); } } tx.Commit(); return count; } catch { tx.Rollback(); return 0; } } } /// <summary> /// 执行带一个存储过程参数的的SQL语句。 /// </summary> /// <param name="SQLString">SQL语句</param> /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param> /// <returns>影响的记录数</returns> public int ExecuteSql(string SQLString, string content) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(SQLString, connection); System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@content", SqlDbType.NText); myParameter.Value = content; cmd.Parameters.Add(myParameter); try { connection.Open(); int rows = cmd.ExecuteNonQuery(); return rows; } catch (System.Data.SqlClient.SqlException e) { throw e; } finally { cmd.Dispose(); connection.Close(); } } } /// <summary> /// 执行带一个存储过程参数的的SQL语句。 /// </summary> /// <param name="SQLString">SQL语句</param> /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param> /// <returns>影响的记录数</returns> public object ExecuteSqlGet(string SQLString, string content) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(SQLString, connection); System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@content", SqlDbType.NText); myParameter.Value = content; cmd.Parameters.Add(myParameter); try { connection.Open(); object obj = cmd.ExecuteScalar(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return null; } else { return obj; } } catch (System.Data.SqlClient.SqlException e) { throw e; } finally { cmd.Dispose(); connection.Close(); } } } /// <summary> /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例) /// </summary> /// <param name="strSQL">SQL语句</param> /// <param name="fs">图像字节,数据库的字段类型为image的情况</param> /// <returns>影响的记录数</returns> public int ExecuteSqlInsertImg(string strSQL, byte[] fs) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(strSQL, connection); System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@fs", SqlDbType.Image); myParameter.Value = fs; cmd.Parameters.Add(myParameter); try { connection.Open(); int rows = cmd.ExecuteNonQuery(); return rows; } catch (System.Data.SqlClient.SqlException e) { throw e; } finally { cmd.Dispose(); connection.Close(); } } } /// <summary> /// 执行一条计算查询结果语句,返回查询结果(object)。 /// </summary> /// <param name="SQLString">计算查询结果语句</param> /// <returns>查询结果(object)</returns> public object GetSingle(string SQLString) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { connection.Open(); object obj = cmd.ExecuteScalar(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return null; } else { return obj; } } catch (System.Data.SqlClient.SqlException e) { connection.Close(); throw e; } } } } public object GetSingle(string SQLString, int Times) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { connection.Open(); cmd.CommandTimeout = Times; object obj = cmd.ExecuteScalar(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return null; } else { return obj; } } catch (System.Data.SqlClient.SqlException e) { connection.Close(); throw e; } } } } /// <summary> /// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close ) /// </summary> /// <param name="strSQL">查询语句</param> /// <returns>SqlDataReader</returns> public SqlDataReader ExecuteReader(string strSQL) { SqlConnection connection = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(strSQL, connection); try { connection.Open(); SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); return myReader; } catch (System.Data.SqlClient.SqlException e) { throw e; } } /// <summary> /// 执行查询语句,返回DataSet /// </summary> /// <param name="SQLString">查询语句</param> /// <returns>DataSet</returns> public DataSet Query(string SQLString) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet ds = new DataSet(); try { connection.Open(); SqlDataAdapter command = new SqlDataAdapter(SQLString, connection); command.Fill(ds, "ds"); } catch (System.Data.SqlClient.SqlException ex) { throw new Exception(ex.Message); } return ds; } } public DataSet Query(string SQLString, int Times) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet ds = new DataSet(); try { connection.Open(); SqlDataAdapter command = new SqlDataAdapter(SQLString, connection); command.SelectCommand.CommandTimeout = Times; command.Fill(ds, "ds"); } catch (System.Data.SqlClient.SqlException ex) { throw new Exception(ex.Message); } return ds; } } #endregion #region 执行带参数的SQL语句 /// <summary> /// 执行SQL语句,返回影响的记录数 /// </summary> /// <param name="SQLString">SQL语句</param> /// <returns>影响的记录数</returns> public int ExecuteSql(string SQLString, params SqlParameter[] cmdParms) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); int rows = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return rows; } catch (System.Data.SqlClient.SqlException e) { throw e; } } } } /// <summary> /// 执行多条SQL语句,实现数据库事务。 /// </summary> /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param> public void ExecuteSqlTran(Hashtable SQLStringList) { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { SqlCommand cmd = new SqlCommand(); try { //循环 foreach (DictionaryEntry myDE in SQLStringList) { string cmdText = myDE.Key.ToString(); SqlParameter[] cmdParms = (SqlParameter[])myDE.Value; PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); } trans.Commit(); } catch { trans.Rollback(); throw; } } } } /// <summary> /// 执行多条SQL语句,实现数据库事务。 /// </summary> /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param> public int ExecuteSqlTran(System.Collections.Generic.List<CommandInfo> cmdList) { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { SqlCommand cmd = new SqlCommand(); try { int count = 0; //循环 foreach (CommandInfo myDE in cmdList) { string cmdText = myDE.CommandText; SqlParameter[] cmdParms = (SqlParameter[])myDE.Parameters; PrepareCommand(cmd, conn, trans, cmdText, cmdParms); if (myDE.EffentNextType == EffentNextType.WhenHaveContine || myDE.EffentNextType == EffentNextType.WhenNoHaveContine) { if (myDE.CommandText.ToLower().IndexOf("count(") == -1) { trans.Rollback(); return 0; } object obj = cmd.ExecuteScalar(); bool isHave = false; if (obj == null && obj == DBNull.Value) { isHave = false; } isHave = Convert.ToInt32(obj) > 0; if (myDE.EffentNextType == EffentNextType.WhenHaveContine && !isHave) { trans.Rollback(); return 0; } if (myDE.EffentNextType == EffentNextType.WhenNoHaveContine && isHave) { trans.Rollback(); return 0; } continue; } int val = cmd.ExecuteNonQuery(); count += val; if (myDE.EffentNextType == EffentNextType.ExcuteEffectRows && val == 0) { trans.Rollback(); return 0; } cmd.Parameters.Clear(); } trans.Commit(); return count; } catch { trans.Rollback(); throw; } } } } /// <summary> /// 执行多条SQL语句,实现数据库事务。 /// </summary> /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param> public void ExecuteSqlTranWithIndentity(System.Collections.Generic.List<CommandInfo> SQLStringList) { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { SqlCommand cmd = new SqlCommand(); try { int indentity = 0; //循环 foreach (CommandInfo myDE in SQLStringList) { string cmdText = myDE.CommandText; SqlParameter[] cmdParms = (SqlParameter[])myDE.Parameters; foreach (SqlParameter q in cmdParms) { if (q.Direction == ParameterDirection.InputOutput) { q.Value = indentity; } } PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = cmd.ExecuteNonQuery(); foreach (SqlParameter q in cmdParms) { if (q.Direction == ParameterDirection.Output) { indentity = Convert.ToInt32(q.Value); } } cmd.Parameters.Clear(); } trans.Commit(); } catch { trans.Rollback(); throw; } } } } /// <summary> /// 执行多条SQL语句,实现数据库事务。 /// </summary> /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param> public void ExecuteSqlTranWithIndentity(Hashtable SQLStringList) { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { SqlCommand cmd = new SqlCommand(); try { int indentity = 0; //循环 foreach (DictionaryEntry myDE in SQLStringList) { string cmdText = myDE.Key.ToString(); SqlParameter[] cmdParms = (SqlParameter[])myDE.Value; foreach (SqlParameter q in cmdParms) { if (q.Direction == ParameterDirection.InputOutput) { q.Value = indentity; } } PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = cmd.ExecuteNonQuery(); foreach (SqlParameter q in cmdParms) { if (q.Direction == ParameterDirection.Output) { indentity = Convert.ToInt32(q.Value); } } cmd.Parameters.Clear(); } trans.Commit(); } catch { trans.Rollback(); throw; } } } } /// <summary> /// 执行一条计算查询结果语句,返回查询结果(object)。 /// </summary> /// <param name="SQLString">计算查询结果语句</param> /// <returns>查询结果(object)</returns> public object GetSingle(string SQLString, params SqlParameter[] cmdParms) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); object obj = cmd.ExecuteScalar(); cmd.Parameters.Clear(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return null; } else { return obj; } } catch (System.Data.SqlClient.SqlException e) { throw e; } } } } /// <summary> /// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close ) /// </summary> /// <param name="strSQL">查询语句</param> /// <returns>SqlDataReader</returns> public SqlDataReader ExecuteReader(string SQLString, params SqlParameter[] cmdParms) { SqlConnection connection = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(); try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); cmd.Parameters.Clear(); return myReader; } catch (System.Data.SqlClient.SqlException e) { throw e; } } /// <summary> /// 执行查询语句,返回DataSet /// </summary> /// <param name="SQLString">查询语句</param> /// <returns>DataSet</returns> public DataSet Query(string SQLString, params SqlParameter[] cmdParms) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, connection, null, SQLString, cmdParms); using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { DataSet ds = new DataSet(); try { da.Fill(ds, "ds"); cmd.Parameters.Clear(); } catch (System.Data.SqlClient.SqlException ex) { throw new Exception(ex.Message); } return ds; } } } private void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms) { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn; cmd.CommandText = cmdText; if (trans != null) cmd.Transaction = trans; cmd.CommandType = CommandType.Text;//cmdType; if (cmdParms != null) { foreach (SqlParameter parameter in cmdParms) { if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) && (parameter.Value == null)) { parameter.Value = DBNull.Value; } cmd.Parameters.Add(parameter); } } } #endregion #region 存储过程操作 /// <summary> /// 执行存储过程,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close ) /// </summary> /// <param name="storedProcName">存储过程名</param> /// <param name="parameters">存储过程参数</param> /// <returns>SqlDataReader</returns> public SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters) { SqlConnection connection = new SqlConnection(connectionString); SqlDataReader returnReader; connection.Open(); SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters); command.CommandType = CommandType.StoredProcedure; returnReader = command.ExecuteReader(CommandBehavior.CloseConnection); return returnReader; } /// <summary> /// 执行存储过程 /// </summary> /// <param name="storedProcName">存储过程名</param> /// <param name="parameters">存储过程参数</param> /// <param name="tableName">DataSet结果中的表名</param> /// <returns>DataSet</returns> public DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet dataSet = new DataSet(); connection.Open(); SqlDataAdapter sqlDA = new SqlDataAdapter(); sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters); sqlDA.Fill(dataSet, tableName); connection.Close(); return dataSet; } } public DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName, int Times) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet dataSet = new DataSet(); connection.Open(); SqlDataAdapter sqlDA = new SqlDataAdapter(); sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters); sqlDA.SelectCommand.CommandTimeout = Times; sqlDA.Fill(dataSet, tableName); connection.Close(); return dataSet; } } /// <summary> /// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值) /// </summary> /// <param name="connection">数据库连接</param> /// <param name="storedProcName">存储过程名</param> /// <param name="parameters">存储过程参数</param> /// <returns>SqlCommand</returns> private SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters) { SqlCommand command = new SqlCommand(storedProcName, connection); command.CommandType = CommandType.StoredProcedure; foreach (SqlParameter parameter in parameters) { if (parameter != null) { // 检查未分配值的输出参数,将其分配以DBNull.Value. if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) && (parameter.Value == null)) { parameter.Value = DBNull.Value; } command.Parameters.Add(parameter); } } return command; } /// <summary> /// 执行存储过程,返回影响的行数 /// </summary> /// <param name="storedProcName">存储过程名</param> /// <param name="parameters">存储过程参数</param> /// <param name="rowsAffected">影响的行数</param> /// <returns></returns> public int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected) { using (SqlConnection connection = new SqlConnection(connectionString)) { int result; connection.Open(); SqlCommand command = BuildIntCommand(connection, storedProcName, parameters); rowsAffected = command.ExecuteNonQuery(); result = (int)command.Parameters["ReturnValue"].Value; return result; } } /// <summary> /// 创建 SqlCommand 对象实例(用来返回一个整数值) /// </summary> /// <param name="storedProcName">存储过程名</param> /// <param name="parameters">存储过程参数</param> /// <returns>SqlCommand 对象实例</returns> private SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters) { SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters); command.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null)); return command; } #endregion #region IDisposable 成员 public void Dispose() { connectionString = string.Empty; } #endregion } }
4、EF的使用和扩展类的使用
/// <summary> /// 解析并入数据库 /// </summary> /// <param name="ds"></param> /// <param name="factoryNo"></param> /// <param name="db"></param> public static void XmlFile2DataBase(ref DataSet ds, string factoryNo, XXX_DBEntities db) { var dtProductLine = ds.Tables["line"]; var dtLinkData = ds.Tables["bc"]; //出现过xml缺少字段的情况,84K大小的zip包里的xml bc表只有3个字段,没有批号和日期。如果是这个文件跳过。 if (dtLinkData.Columns.Count != 5) return; string outBoxNo = dtLinkData.Rows[0]["out"].ToString(); //检测是否有重复记录,有则忽略,该文件只要有一个箱码重复,说明此文件是重复上传。事务控制到文件。 decimal cnt = decimal.Parse(db.AdoExecuteScalar(string.Format("select count(*) as cnt from SC_PRODUCTSOURCE s where s.outbox_dnacode = '{0}'", outBoxNo)).ToString()); if (cnt > 0) { return; } var dt = db.GetTmpNewDataTable(); #region 准备临时数据,并使用OracleBulkCopy批量插入,每产线插一次 foreach (DataRow dr in dtProductLine.Rows) { if (!string.IsNullOrEmpty(dr["pid"].ToString())) { dt.Rows.Clear(); int prdCode = int.Parse(dr["psid"].ToString()); var product = db.ProductInfo.Where(p => p.PRODUCT_CODE == prdCode).FirstOrDefault(); string prdName = product.PRODUCT_CNNAME; string prdBrandName = product.PRD_BRAND; DataView dv = dtLinkData.DefaultView; dv.RowFilter = string.Format("data_Id={0}", dr["line_Id"].ToString()); foreach (DataRowView itemRow in dv) { string[] itemList = itemRow["bc_Text"].ToString().Split(",".ToCharArray()); string batchNo = itemRow["batchno"].ToString(); string outBoxNoSub = itemRow["out"].ToString(); for (int idx = 0; idx < itemList.Length; idx++) { DateTime dtProduct = new DateTime(1901, 01, 01); DateTime.TryParse(itemRow["maketime"].ToString(), out dtProduct); var drProductLinkData = dt.NewRow(); #region 一盒的生产数据 drProductLinkData["PRODUCTSOURCEID"] = Guid.NewGuid().ToString("N").ToUpper(); drProductLinkData["FACTORYID"] = factoryNo; drProductLinkData["PRDLINEID"] = dr["no"].ToString(); drProductLinkData["MACRANDOMCODE"] = itemList[idx]; drProductLinkData["PRODUCTBATCHIDX"] = batchNo; drProductLinkData["LASERDNA_CODE"] = ""; drProductLinkData["PRODUCTNO"] = int.Parse(dr["psid"].ToString()); drProductLinkData["PRODUCTNAME"] = prdName; drProductLinkData["PRODUCTTYPENAME"] = prdBrandName; drProductLinkData["PRODUCTDATE"] = dtProduct; drProductLinkData["PRINTCODEDT"] = dtProduct; drProductLinkData["FACTORYSHIFTNO"] = ""; drProductLinkData["OUTBOX_DNACODE"] = outBoxNoSub; drProductLinkData["VIRTUALBALECODE"] = ""; drProductLinkData["SENDTONDC_DT"] = DateTime.Now; drProductLinkData["FAC_NO"] = factoryNo; drProductLinkData["PRODUCTVERSION"] = DateTime.Now; #endregion dt.Rows.Add(drProductLinkData); } } if (dt.Rows.Count > 0) { db.ExecuteSqlBulkCopy(dt); } } } #endregion }
4、程序运行效果及截图
1、日志
2、截图
暂无,因为程序效率太高,一时没有文件需要解析了。呵呵
结束语:做项目不需要定势的思维,比如一定要分层架构,一定要什么牛X的技术,一定要高可扩展性、高可重用性。小项目,快速、稳定、高效和可维护性才是需要考量的要点。天下武功,唯快不破。呵呵...
PS:CSS样式是扒园友的,原来不是在博客设置里写样式,是在页面的HTML模式下加入自定义样式。
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"作者:" 数据酷软件工作室
"出处:" http://datacool.cnblogs.com
"专注于CMS(综合赋码系统),MES,WCS(智能仓储设备控制系统),WMS,商超,桑拿、餐饮、客房、足浴等行业收银系统的开发,15年+从业经验。因为专业,所以出色。"
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"作者:" 数据酷软件工作室
"出处:" http://datacool.cnblogs.com
"专注于CMS(综合赋码系统),MES,WCS(智能仓储设备控制系统),WMS,商超,桑拿、餐饮、客房、足浴等行业收银系统的开发,15年+从业经验。因为专业,所以出色。"
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++