C#--一次写入多条数据-sqlserver和access用时对比

 

1,access代码:

        private void SaveResultToMyaccess(PMOpenProtocol.TighteningResultData data)
        {
            try
            {
                myAccess.AccessDbClass();
                OleDbTransaction myTrans = myAccess.Conn.BeginTransaction();
                OleDbCommand incmd = myAccess.Conn.CreateCommand();
                incmd.Transaction = myTrans;

                //【1】编写SQL语句
                StringBuilder sqlBuilder = new StringBuilder();//如果字符串比较长,可以用StringBuilder
                sqlBuilder.Append("insert into mydata (TighteningID,ProductSN,PsetName,StationName,BoltNumber,TighteningStatus,ResultDateTime,FinalTorque,FinalAngle,TorqueMax,TorqueMin,OperateDateTime,OperateFlag,ErrorInfo)");
                sqlBuilder.Append("  values(‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,‘{11}‘,{12},‘{13}‘)");
                //【2】解析对象

                #region 普通方法:写18次

                //string sql = string.Format(sqlBuilder.ToString(),
                //    data.TighteningID, data.IDRes, data.ModeName, data.StationName, data.OrdinalBoltNumber_1, data.TighteningStatus_1, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_1, data.FinalAngle_1, "", 0, "");
                //incmd.CommandText = sql;
                //incmd.ExecuteNonQuery();

                //if (data.Number_of_Bolts >= 2)
                //{
                //    sql = string.Format(sqlBuilder.ToString(),
                //        data.TighteningID, data.IDRes, data.ModeName, data.StationName, data.OrdinalBoltNumber_2, data.TighteningStatus_2, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_2, data.FinalAngle_2, "", 0, "");
                //    incmd.CommandText = sql;
                //    incmd.ExecuteNonQuery();
                //}
                //if (data.Number_of_Bolts >= 3)
                //{
                //    sql = string.Format(sqlBuilder.ToString(),
                //        data.TighteningID, data.IDRes, data.ModeName, data.StationName, data.OrdinalBoltNumber_3, data.TighteningStatus_3, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_3, data.FinalAngle_3, "", 0, "");
                //    incmd.CommandText = sql;
                //    incmd.ExecuteNonQuery();
                //}
                //if (data.Number_of_Bolts >= 4)
                //{
                //    sql = string.Format(sqlBuilder.ToString(),
                //        data.TighteningID, data.IDRes, data.ModeName, data.StationName, data.OrdinalBoltNumber_4, data.TighteningStatus_4, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_4, data.FinalAngle_4, "", 0, "");
                //    incmd.CommandText = sql;
                //    incmd.ExecuteNonQuery();
                //}
                //if (data.Number_of_Bolts >= 5)
                //{
                //    sql = string.Format(sqlBuilder.ToString(),
                //        data.TighteningID, data.IDRes, data.ModeName, data.StationName, data.OrdinalBoltNumber_5, data.TighteningStatus_5, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_5, data.FinalAngle_5, "", 0, "");
                //    incmd.CommandText = sql;
                //    incmd.ExecuteNonQuery();
                //}
                //if (data.Number_of_Bolts >= 6)
                //{
                //    sql = string.Format(sqlBuilder.ToString(),
                //        data.TighteningID, data.IDRes, data.ModeName, data.StationName, data.OrdinalBoltNumber_6, data.TighteningStatus_6, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_6, data.FinalAngle_6, "", 0, "");
                //    incmd.CommandText = sql;
                //    incmd.ExecuteNonQuery();
                //}
                //if (data.Number_of_Bolts >= 7)
                //{
                //    sql = string.Format(sqlBuilder.ToString(),
                //        data.TighteningID, data.IDRes, data.ModeName, data.StationName, data.OrdinalBoltNumber_7, data.TighteningStatus_7, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_7, data.FinalAngle_7, "", 0, "");
                //    incmd.CommandText = sql;
                //    incmd.ExecuteNonQuery();
                //}
                //if (data.Number_of_Bolts >= 8)
                //{
                //    sql = string.Format(sqlBuilder.ToString(),
                //        data.TighteningID, data.IDRes, data.ModeName, data.StationName, data.OrdinalBoltNumber_8, data.TighteningStatus_8, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_8, data.FinalAngle_8, "", 0, "");
                //    incmd.CommandText = sql;
                //    incmd.ExecuteNonQuery();
                //}
                //if (data.Number_of_Bolts >= 9)
                //{
                //    sql = string.Format(sqlBuilder.ToString(),
                //        data.TighteningID, data.IDRes, data.ModeName, data.StationName, data.OrdinalBoltNumber_9, data.TighteningStatus_9, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_9, data.FinalAngle_9, "", 0, "");
                //    incmd.CommandText = sql;
                //    incmd.ExecuteNonQuery();
                //}
                //if (data.Number_of_Bolts >= 10)
                //{
                //    sql = string.Format(sqlBuilder.ToString(),
                //        data.TighteningID, data.IDRes, data.ModeName, data.StationName, data.OrdinalBoltNumber_10, data.TighteningStatus_10, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_10, data.FinalAngle_10, "", 0, "");
                //    incmd.CommandText = sql;
                //    incmd.ExecuteNonQuery();
                //}
                //if (data.Number_of_Bolts >= 11)
                //{
                //    sql = string.Format(sqlBuilder.ToString(),
                //        data.TighteningID, data.IDRes, data.ModeName, data.StationName, data.OrdinalBoltNumber_11, data.TighteningStatus_11, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_11, data.FinalAngle_11, "", 0, "");
                //    incmd.CommandText = sql;
                //    incmd.ExecuteNonQuery();
                //}
                //if (data.Number_of_Bolts >= 12)
                //{
                //    sql = string.Format(sqlBuilder.ToString(),
                //        data.TighteningID, data.IDRes, data.ModeName, data.StationName, data.OrdinalBoltNumber_12, data.TighteningStatus_12, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_12, data.FinalAngle_12, "", 0, "");
                //    incmd.CommandText = sql;
                //    incmd.ExecuteNonQuery();
                //}
                //if (data.Number_of_Bolts >= 13)
                //{
                //    sql = string.Format(sqlBuilder.ToString(),
                //        data.TighteningID, data.IDRes, data.ModeName, data.StationName, data.OrdinalBoltNumber_13, data.TighteningStatus_13, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_13, data.FinalAngle_13, "", 0, "");
                //    incmd.CommandText = sql;
                //    incmd.ExecuteNonQuery();
                //}
                //if (data.Number_of_Bolts >= 14)
                //{
                //    sql = string.Format(sqlBuilder.ToString(),
                //        data.TighteningID, data.IDRes, data.ModeName, data.StationName, data.OrdinalBoltNumber_14, data.TighteningStatus_14, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_14, data.FinalAngle_14, "", 0, "");
                //    incmd.CommandText = sql;
                //    incmd.ExecuteNonQuery();
                //}
                //if (data.Number_of_Bolts >= 15)
                //{
                //    sql = string.Format(sqlBuilder.ToString(),
                //        data.TighteningID, data.IDRes, data.ModeName, data.StationName, data.OrdinalBoltNumber_15, data.TighteningStatus_15, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_15, data.FinalAngle_15, "", 0, "");
                //    incmd.CommandText = sql;
                //    incmd.ExecuteNonQuery();
                //}
                //if (data.Number_of_Bolts >= 16)
                //{
                //    sql = string.Format(sqlBuilder.ToString(),
                //        data.TighteningID, data.IDRes, data.ModeName, data.StationName, data.OrdinalBoltNumber_16, data.TighteningStatus_16, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_16, data.FinalAngle_16, "", 0, "");
                //    incmd.CommandText = sql;
                //    incmd.ExecuteNonQuery();
                //}
                //if (data.Number_of_Bolts >= 17)
                //{
                //    sql = string.Format(sqlBuilder.ToString(),
                //        data.TighteningID, data.IDRes, data.ModeName, data.StationName, data.OrdinalBoltNumber_17, data.TighteningStatus_17, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_17, data.FinalAngle_17, "", 0, "");
                //    incmd.CommandText = sql;
                //    incmd.ExecuteNonQuery();
                //}
                //if (data.Number_of_Bolts >= 18)
                //{
                //    sql = string.Format(sqlBuilder.ToString(),
                //        data.TighteningID, data.IDRes, data.ModeName, data.StationName, data.OrdinalBoltNumber_18, data.TighteningStatus_18, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_18, data.FinalAngle_18, "", 0, "");
                //    incmd.CommandText = sql;
                //    incmd.ExecuteNonQuery();
                //}

                #endregion

                #region 反射方法:遍历一次就可以

                Type type = data.GetType();

                string sql;
       
                for (int i = 1; i <= data.Number_of_Bolts; i++)
                {
                        string OrdinalBoltNumber= type.GetField("OrdinalBoltNumber_" + i).GetValue(data).ToString();
                        string TighteningStatus= type.GetField("TighteningStatus_" + i).GetValue(data).ToString();
                        double FinalTorque = Convert.ToDouble(type.GetField("FinalTorque_" + i).GetValue(data)) ;
                        double FinalAngle = Convert.ToDouble(type.GetField("FinalAngle_" + i).GetValue(data)) ;
                        double TorqueMax = Convert.ToDouble(type.GetField("TorqueMax_" + i).GetValue(data)) ;
                        double TorqueMin = Convert.ToDouble(type.GetField("TorqueMin_" + i).GetValue(data)) ;

                    sql = string.Format(sqlBuilder.ToString(),
                            data.TighteningID, data.IDRes, data.ModeName, data.StationName, OrdinalBoltNumber, TighteningStatus, Convert.ToDateTime(data.t_D_REAL_TIME), FinalTorque, FinalAngle, TorqueMax, TorqueMin, "", 0, "");
                        incmd.CommandText = sql;
                        incmd.ExecuteNonQuery();
                }

                #endregion

                try
                {
                    myTrans.Commit();
                    myAccess.Close();
                }
                catch
                {
                    myAccess.Close();
                }
            }
            catch (Exception e)
            {
                log.Warn("写入数据库失败,请检查数据库:" + e.ToString());
            }
        }

  

2,sqlserver代码:

        public void SaveMultiTighteningResult(PMOpenProtocol.TighteningResultData data)
        {
            //【1】编写SQL语句
            StringBuilder sqlBuilder = new StringBuilder();//如果字符串比较长,可以用StringBuilder
            sqlBuilder.Append("insert into TighteningResult(TighteningID,ProductSN,StationCode,StationName,PsetName,BoltNumber,TighteningStatus,ResultDateTime,FinalTorque,FinalAngle,TorqueMax,TorqueMin,OperateDateTime,OperateFlag,ErrorInfo)");
            sqlBuilder.Append(" select ‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,‘{11}‘,‘{12}‘,{13},‘{14}‘");
            //【2】解析对象

            #region 普通方法,要写18次

            //string sql = string.Format(sqlBuilder.ToString(),
            //    data.TighteningID, data.IDRes, "", data.StationName, data.ModeName, data.OrdinalBoltNumber_1, data.TighteningStatus_1, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_1, data.FinalAngle_1,data.TorqueMax_1,data.TorqueMin_1, "", 0, "");

            //if (data.Number_of_Bolts >= 2)
            //{
            //    sqlBuilder = new StringBuilder(sql);
            //    sqlBuilder.Append("union all select ‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,{11},‘{12}‘");
            //    sql = string.Format(sqlBuilder.ToString(),
            //        data.TighteningID, data.IDRes, "", data.StationName, data.ModeName, data.OrdinalBoltNumber_2, data.TighteningStatus_2, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_2, data.FinalAngle_2, "", 0, "");
            //}
            //if (data.Number_of_Bolts >= 3)
            //{
            //    sqlBuilder = new StringBuilder(sql);
            //    sqlBuilder.Append("union all select ‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,{11},‘{12}‘");
            //    sql = string.Format(sqlBuilder.ToString(),
            //        data.TighteningID, data.IDRes, "", data.StationName, data.ModeName, data.OrdinalBoltNumber_3, data.TighteningStatus_3, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_3, data.FinalAngle_3, "", 0, "");
            //}
            //if (data.Number_of_Bolts >= 4)
            //{
            //    sqlBuilder = new StringBuilder(sql);
            //    sqlBuilder.Append("union all select ‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,{11},‘{12}‘");
            //    sql = string.Format(sqlBuilder.ToString(),
            //        data.TighteningID, data.IDRes, "", data.StationName, data.ModeName, data.OrdinalBoltNumber_4, data.TighteningStatus_4, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_4, data.FinalAngle_4, "", 0, "");
            //}
            //if (data.Number_of_Bolts >= 5)
            //{
            //    sqlBuilder = new StringBuilder(sql);
            //    sqlBuilder.Append("union all select ‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,{11},‘{12}‘");
            //    sql = string.Format(sqlBuilder.ToString(),
            //        data.TighteningID, data.IDRes, "", data.StationName, data.ModeName, data.OrdinalBoltNumber_5, data.TighteningStatus_5, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_5, data.FinalAngle_5, "", 0, "");
            //}
            //if (data.Number_of_Bolts >= 6)
            //{
            //    sqlBuilder = new StringBuilder(sql);
            //    sqlBuilder.Append("union all select ‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,{11},‘{12}‘");
            //    sql = string.Format(sqlBuilder.ToString(),
            //        data.TighteningID, data.IDRes, "", data.StationName, data.ModeName, data.OrdinalBoltNumber_6, data.TighteningStatus_6, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_6, data.FinalAngle_6, "", 0, "");
            //}
            //if (data.Number_of_Bolts >= 7)
            //{
            //    sqlBuilder = new StringBuilder(sql);
            //    sqlBuilder.Append("union all select ‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,{11},‘{12}‘");
            //    sql = string.Format(sqlBuilder.ToString(),
            //        data.TighteningID, data.IDRes, "", data.StationName, data.ModeName, data.OrdinalBoltNumber_7, data.TighteningStatus_7, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_7, data.FinalAngle_7, "", 0, "");
            //}
            //if (data.Number_of_Bolts >= 8)
            //{
            //    sqlBuilder = new StringBuilder(sql);
            //    sqlBuilder.Append("union all select ‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,{11},‘{12}‘");
            //    sql = string.Format(sqlBuilder.ToString(),
            //        data.TighteningID, data.IDRes, "", data.StationName, data.ModeName, data.OrdinalBoltNumber_8, data.TighteningStatus_8, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_8, data.FinalAngle_8, "", 0, "");
            //}
            //if (data.Number_of_Bolts >= 9)
            //{
            //    sqlBuilder = new StringBuilder(sql);
            //    sqlBuilder.Append("union all select ‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,{11},‘{12}‘");
            //    sql = string.Format(sqlBuilder.ToString(),
            //        data.TighteningID, data.IDRes, "", data.StationName, data.ModeName, data.OrdinalBoltNumber_9, data.TighteningStatus_9, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_9, data.FinalAngle_9, "", 0, "");
            //}
            //if (data.Number_of_Bolts >= 10)
            //{
            //    sqlBuilder = new StringBuilder(sql);
            //    sqlBuilder.Append("union all select ‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,{11},‘{12}‘");
            //    sql = string.Format(sqlBuilder.ToString(),
            //        data.TighteningID, data.IDRes, "", data.StationName, data.ModeName, data.OrdinalBoltNumber_10, data.TighteningStatus_10, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_10, data.FinalAngle_10, "", 0, "");
            //}
            //if (data.Number_of_Bolts >= 11)
            //{
            //    sqlBuilder = new StringBuilder(sql);
            //    sqlBuilder.Append("union all select ‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,{11},‘{12}‘");
            //    sql = string.Format(sqlBuilder.ToString(),
            //        data.TighteningID, data.IDRes, "", data.StationName, data.ModeName, data.OrdinalBoltNumber_11, data.TighteningStatus_11, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_11, data.FinalAngle_11, "", 0, "");
            //}
            //if (data.Number_of_Bolts >= 12)
            //{
            //    sqlBuilder = new StringBuilder(sql);
            //    sqlBuilder.Append("union all select ‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,{11},‘{12}‘");
            //    sql = string.Format(sqlBuilder.ToString(),
            //        data.TighteningID, data.IDRes, "", data.StationName, data.ModeName, data.OrdinalBoltNumber_12, data.TighteningStatus_12, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_12, data.FinalAngle_12, "", 0, "");
            //}
            //if (data.Number_of_Bolts >= 13)
            //{
            //    sqlBuilder = new StringBuilder(sql);
            //    sqlBuilder.Append("union all select ‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,{11},‘{12}‘");
            //    sql = string.Format(sqlBuilder.ToString(),
            //        data.TighteningID, data.IDRes, "", data.StationName, data.ModeName, data.OrdinalBoltNumber_13, data.TighteningStatus_13, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_13, data.FinalAngle_13, "", 0, "");
            //}
            //if (data.Number_of_Bolts >= 14)
            //{
            //    sqlBuilder = new StringBuilder(sql);
            //    sqlBuilder.Append("union all select ‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,{11},‘{12}‘");
            //    sql = string.Format(sqlBuilder.ToString(),
            //        data.TighteningID, data.IDRes, "", data.StationName, data.ModeName, data.OrdinalBoltNumber_14, data.TighteningStatus_14, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_14, data.FinalAngle_14, "", 0, "");
            //}
            //if (data.Number_of_Bolts >= 15)
            //{
            //    sqlBuilder = new StringBuilder(sql);
            //    sqlBuilder.Append("union all select ‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,{11},‘{12}‘");
            //    sql = string.Format(sqlBuilder.ToString(),
            //        data.TighteningID, data.IDRes, "", data.StationName, data.ModeName, data.OrdinalBoltNumber_15, data.TighteningStatus_15, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_15, data.FinalAngle_15, "", 0, "");
            //}
            //if (data.Number_of_Bolts >= 16)
            //{
            //    sqlBuilder = new StringBuilder(sql);
            //    sqlBuilder.Append("union all select ‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,{11},‘{12}‘");
            //    sql = string.Format(sqlBuilder.ToString(),
            //        data.TighteningID, data.IDRes, "", data.StationName, data.ModeName, data.OrdinalBoltNumber_16, data.TighteningStatus_16, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_16, data.FinalAngle_16, "", 0, "");
            //}
            //if (data.Number_of_Bolts >= 17)
            //{
            //    sqlBuilder = new StringBuilder(sql);
            //    sqlBuilder.Append("union all select ‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,{11},‘{12}‘");
            //    sql = string.Format(sqlBuilder.ToString(),
            //        data.TighteningID, data.IDRes, "", data.StationName, data.ModeName, data.OrdinalBoltNumber_17, data.TighteningStatus_17, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_17, data.FinalAngle_17, "", 0, "");
            //}
            //if (data.Number_of_Bolts >= 18)
            //{
            //    sqlBuilder = new StringBuilder(sql);
            //    sqlBuilder.Append("union all select ‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,{11},‘{12}‘");
            //    sql = string.Format(sqlBuilder.ToString(),
            //        data.TighteningID, data.IDRes, "", data.StationName, data.ModeName, data.OrdinalBoltNumber_18, data.TighteningStatus_18, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_18, data.FinalAngle_18, "", 0, "");
            //}

            #endregion

            #region 反射方法:遍历一次就可以

            string sql="";

            Type type = data.GetType();

            for (int i = 1; i <= data.Number_of_Bolts; i++)
            {
                if (i == 1)
                {
                     sql = string.Format(sqlBuilder.ToString(),
                        data.TighteningID, data.IDRes, "", data.StationName, data.ModeName, data.OrdinalBoltNumber_1, data.TighteningStatus_1, Convert.ToDateTime(data.t_D_REAL_TIME), data.FinalTorque_1, data.FinalAngle_1, data.TorqueMax_1, data.TorqueMin_1, "", 0, "");
                }
                else
                {
                    string OrdinalBoltNumber = type.GetField("OrdinalBoltNumber_" + i).GetValue(data).ToString();
                    string TighteningStatus = type.GetField("TighteningStatus_" + i).GetValue(data).ToString();
                    double FinalTorque = Convert.ToDouble(type.GetField("FinalTorque_" + i).GetValue(data));
                    double FinalAngle = Convert.ToDouble(type.GetField("FinalAngle_" + i).GetValue(data));
                    double TorqueMax = Convert.ToDouble(type.GetField("TorqueMax_" + i).GetValue(data));
                    double TorqueMin = Convert.ToDouble(type.GetField("TorqueMin_" + i).GetValue(data));

                    sqlBuilder = new StringBuilder(sql);
                    sqlBuilder.Append("union all select ‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,‘{11}‘,‘{12}‘,{13},‘{14}‘");
                    sql = string.Format(sqlBuilder.ToString(),
                        data.TighteningID, data.IDRes, "", data.StationName, data.ModeName, OrdinalBoltNumber, TighteningStatus, Convert.ToDateTime(data.t_D_REAL_TIME), FinalTorque, FinalAngle, TorqueMax, TorqueMin, "", 0, "");
                }

            }

            #endregion
            //【3】提交到数据库
            try
            {
                 SQLHelper.Update(sql);
            }
            catch (SqlException ex)
            {
                log.Warn("数据库操作出现异常!具体信息:" + ex.Message);
            }
            catch (Exception ex)
            {
                log.Warn("异常!具体信息:" + ex.Message);
            }
        }

 

3,用时对比:

                Stopwatch sw=new Stopwatch();
                sw.Start();
                SaveMultiTighteningResult(data);//数据保存在sqlserver数据库
                sw.Stop();
                Console.WriteLine("sql用时:"+sw.Elapsed.TotalMilliseconds);

                Stopwatch sw2 = new Stopwatch();
                sw2.Start();
                SaveResultToMyaccess(data);//数据保存在access数据库
                sw2.Stop();
                Console.WriteLine("access用时:" + sw2.Elapsed.TotalMilliseconds);

 

结果:

sql用时:172.4197
access用时:454.1316

 

建议放入线程中操作,不影响界面的卡顿。

                Stopwatch sw=new Stopwatch();
                sw.Start();
                Thread th2 = new Thread((() => SaveMultiTighteningResult(data)));//数据保存在sqlserver数据库
                th2.Start();
                sw.Stop();
                Console.WriteLine("sql用时:"+sw.Elapsed.TotalMilliseconds);

                Stopwatch sw2 = new Stopwatch();
                sw2.Start();
                Thread th = new Thread((() => SaveResultToMyaccess(data)));//数据保存在access数据库
                th.Start();
                sw2.Stop();
                Console.WriteLine("access用时:" + sw2.Elapsed.TotalMilliseconds);

  

 

C#--一次写入多条数据-sqlserver和access用时对比

上一篇:android视频处理相关资料


下一篇:【nginx反向代理】手把手教你利用nginx的反向代理实现自定义爆款域名跳转到你的博客主页