/// <summary> /// 设置指定元素的值 /// </summary> /// <param name="fileName">xml文件的完整路径</param> /// <param name="path">元素路径</param> /// <param name="value">值</param> /// <returns></returns> public static OperationReturn SetElementValue(string fileName, string path, string value) { OperationReturn optReturn = new OperationReturn(); optReturn.Result = true; optReturn.Code = Defines.RET_SUCCESS; try { if (!File.Exists(fileName)) { optReturn.Result = false; optReturn.Code = Defines.RET_FILE_NOT_EXIST; optReturn.Message = string.Format("File not exist.\t{0}", fileName); return(optReturn); } XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(fileName); XmlNode node = xmlDoc.SelectSingleNode(path); XmlElement element = node as XmlElement; if (element == null) { optReturn.Result = false; optReturn.Code = Defines.RET_XML_ELE_NOT_EXIST; optReturn.Message = string.Format("XmlElement not exist.\t{0}", path); return(optReturn); } element.InnerText = value; xmlDoc.Save(fileName); optReturn.Data = value; } catch (Exception ex) { optReturn.Result = false; optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; optReturn.Exception = ex; } return(optReturn); }
/// <summary> /// 获取数据集 /// </summary> /// <param name="strConn">连接字符串</param> /// <param name="strSql">查询语句</param> /// <returns></returns> public static OperationReturn GetDataSet(string strConn, string strSql) { OperationReturn optReturn = new OperationReturn(); optReturn.Result = true; optReturn.Code = 0; SqlConnection sqlConnection = new SqlConnection(strConn); SqlDataAdapter sqlAdapter = new SqlDataAdapter(strSql, sqlConnection); DataSet objDataSet = new DataSet(); try { sqlAdapter.Fill(objDataSet); optReturn.Data = objDataSet; } catch (Exception ex) { optReturn.Result = false; optReturn.Code = Defines.RET_DBACCESS_FAIL; optReturn.Message = ex.Message; var err = ex as SqlException; if (err != null) { if (err.Number == DBAccessDefine.MSSQL_ERR_OBJECT_NOT_EXIST) { optReturn.Code = Defines.RET_DBACCESS_TABLE_NOT_EXIST; } } } finally { if (sqlConnection.State == ConnectionState.Open) { sqlConnection.Close(); } sqlConnection.Dispose(); } return(optReturn); }
/// <summary> /// 写入日志 /// </summary> /// <param name="mode">日志模式</param> /// <param name="category">分类</param> /// <param name="msg">消息内容</param> /// <returns></returns> public OperationReturn WriteLog(LogMode mode, string category, string msg) { OperationReturn optReturn = new OperationReturn(); optReturn.Result = true; optReturn.Code = Defines.RET_SUCCESS; try { if ((mode & mLogMode) == 0) { return(optReturn); } DateTime now = DateTime.Now; FileInfo fileInfo = new FileInfo(mLogFile); if (mLastDate.Date != now.Date || mLogWriter == null || (File.Exists(mLogFile) && fileInfo.Length > mLogFileSize * 1024)) { optReturn = Start(); if (!optReturn.Result) { return(optReturn); } } int index = msg.IndexOf("\r\n", StringComparison.Ordinal); string strThis = msg; string strOther = string.Empty; if (index > 0) { strThis = msg.Substring(0, index); strOther = msg.Substring(index + 2); } string formatstr = string.Format("{0,-8} |{1,-15} |({2})| {3,-20} {4}" , mode , DateTime.Now.ToString("HH:mm:ss.fff") , Thread.CurrentThread.ManagedThreadId.ToString("00000") , category , strThis); lock (mLogWriterLocker) { if (mLogWriter != null) { mLogWriter.WriteLine(formatstr); mLogWriter.Flush(); } else { optReturn.Result = false; optReturn.Code = Defines.RET_OBJECT_NULL; optReturn.Message = "LogWriter is null"; return(optReturn); } } if (!string.IsNullOrEmpty(strOther)) { WriteLog(mode, category, strOther); } } catch (Exception ex) { optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; optReturn.Exception = ex; } return(optReturn); }
/// <summary> /// 执行存储过程,获取数据集 /// </summary> /// <param name="strConn"></param> /// <param name="procedureName"></param> /// <param name="parameters"></param> /// <returns></returns> public static OperationReturn GetDataSetFromStoredProcedure(string strConn, string procedureName, DBCmdParameter[] parameters) { OperationReturn optReturn = new OperationReturn(); optReturn.Result = true; optReturn.Code = 0; SqlConnection sqlConnection = new SqlConnection(strConn); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.Connection = sqlConnection; sqlCmd.CommandType = CommandType.StoredProcedure; sqlCmd.CommandText = procedureName; SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCmd); DataSet objDataSet = new DataSet(); for (int i = 0; i < parameters.Length; i++) { var parameter = parameters[i]; string paramName = parameter.Name; int dataType = parameter.DataType; int length = parameter.Length; int direction = parameter.Direction; string paramValue = parameter.ParamValue; DbParameter dbParam; if (length > 0) { dbParam = GetDbParameter(paramName, (MssqlDataType)dataType, length); } else { dbParam = GetDbParameter(paramName, (MssqlDataType)dataType); } dbParam.Direction = (ParameterDirection)direction; dbParam.Value = paramValue; sqlCmd.Parameters.Add(dbParam); } try { sqlConnection.Open(); sqlAdapter.Fill(objDataSet); optReturn.Data = objDataSet; } catch (Exception ex) { optReturn.Result = false; optReturn.Code = Defines.RET_DBACCESS_FAIL; optReturn.Message = ex.Message; optReturn.Exception = ex; var err = ex as SqlException; if (err != null) { if (err.Number == DBAccessDefine.MYSQL_ERR_OBJECT_NOT_EXIST) { optReturn.Code = Defines.RET_DBACCESS_TABLE_NOT_EXIST; } } } finally { if (sqlConnection.State == ConnectionState.Open) { sqlConnection.Close(); } sqlConnection.Dispose(); } return(optReturn); }