public static T ParseValue <T>(object text, T defaultValue) { if (text == null || text.ToString() == "") { return(defaultValue); } try { if (typeof(T) == typeof(String)) { return((T)(object)(text.ToString())); } if (typeof(T) == typeof(DateTime)) { return((T)(object)DateTime.Parse(text.ToString())); } if (typeof(T) == typeof(long)) { return((T)(object)long.Parse(text.ToString())); } if (typeof(T) == typeof(int)) { return((T)(object)int.Parse(text.ToString())); } if (typeof(T) == typeof(double)) { return((T)(object)double.Parse(text.ToString())); } if (typeof(T) == typeof(decimal)) { return((T)(object)decimal.Parse(text.ToString().Replace(",", "."), CultureInfo.InvariantCulture.NumberFormat)); } if (typeof(T) == typeof(decimal?)) { return((T)(object)decimal.Parse(text.ToString().Replace(",", "."), CultureInfo.InvariantCulture.NumberFormat)); } if (typeof(T) == typeof(int?)) { return((T)(object)int.Parse(text.ToString())); } if (typeof(T) == typeof(Guid)) { return((T)(object)(new Guid(text.ToString()))); } if (typeof(T) == typeof(bool)) { return((T)(object)(bool.Parse(text.ToString()))); } if (typeof(T) == typeof(bool?)) { bool b; if (!bool.TryParse(text.ToString(), out b)) { return(defaultValue); } return((T)(object)b); } if (typeof(T) == typeof(Boolean)) { return((T)(object)(Boolean.Parse(text.ToString()))); } if (typeof(T).IsEnum) { return((T)Enum.Parse(typeof(T), text.ToString())); } } catch (Exception e) { LoggerHelper.Error("Error parsing value '" + text + "' to a " + typeof(T).Name, e); return(defaultValue); } return(defaultValue); }
/// <summary> /// 执行Sql和Oracle滴混合事务 /// </summary> /// <param name="list">SQL命令行列表</param> /// <param name="oracleCmdSqlList">Oracle命令行列表</param> /// <returns>执行结果 0-由于SQL造成事务失败 -1 由于Oracle造成事务失败 1-整体事务执行成功</returns> public static int ExecuteSqlTran(List <CommandInfo> list, List <CommandInfo> oracleCmdSqlList) { using (var conn = new SqlConnection(ConnectionString)) { conn.Open(); var cmd = new SqlCommand { Connection = conn }; var tx = conn.BeginTransaction(); cmd.Transaction = tx; try { foreach (var myDe in list) { string cmdText = myDe.CommandText; var cmdParms = (SqlParameter[])myDe.Parameters; PrepareCommand(cmd, conn, tx, cmdText, cmdParms); if (myDe.EffentNextType == EffentNextType.SolicitationEvent) { if (myDe.CommandText.ToLower().IndexOf("count(", StringComparison.Ordinal) == -1) { tx.Rollback(); LoggerHelper.Error("违背要求" + myDe.CommandText + "必须符合select count(..的格式"); //throw new Exception("违背要求" + myDe.CommandText + "必须符合select count(..的格式"); return(0); } var obj = cmd.ExecuteScalar(); var isHave = Convert.ToInt32(obj) > 0; if (isHave) { //引发事件 myDe.OnSolicitationEvent(); } } if (myDe.EffentNextType == EffentNextType.WhenHaveContine || myDe.EffentNextType == EffentNextType.WhenNoHaveContine) { if (myDe.CommandText.ToLower().IndexOf("count(", StringComparison.Ordinal) == -1) { tx.Rollback(); LoggerHelper.Error("SQL:违背要求" + myDe.CommandText + "必须符合select count(..的格式"); //throw new Exception("SQL:违背要求" + myDe.CommandText + "必须符合select count(..的格式"); return(0); } object obj = cmd.ExecuteScalar(); bool isHave = Convert.ToInt32(obj) > 0; if (myDe.EffentNextType == EffentNextType.WhenHaveContine && !isHave) { tx.Rollback(); LoggerHelper.Error("SQL:违背要求" + myDe.CommandText + "返回值必须大于0"); //throw new Exception("SQL:违背要求" + myDe.CommandText + "返回值必须大于0"); return(0); } if (myDe.EffentNextType == EffentNextType.WhenNoHaveContine && isHave) { tx.Rollback(); LoggerHelper.Error("SQL:违背要求" + myDe.CommandText + "返回值必须大于0"); //throw new Exception("SQL:违背要求" + myDe.CommandText + "返回值必须等于0"); return(0); } continue; } int val = cmd.ExecuteNonQuery(); if (myDe.EffentNextType == EffentNextType.ExcuteEffectRows && val == 0) { tx.Rollback(); LoggerHelper.Error("SQL:违背要求" + myDe.CommandText + "必须有影响行"); // throw new Exception("SQL:违背要求" + myDe.CommandText + "必须有影响行"); return(0); } cmd.Parameters.Clear(); } tx.Commit(); return(1); } catch (SqlException e) { tx.Rollback(); LoggerHelper.Error("SqlException 执行回滚操作:", e); return(0); } catch (Exception e) { tx.Rollback(); LoggerHelper.Error("Exception 执行回滚操作:", e); return(0); } } }
/// <summary> /// 执行多条SQL语句,实现数据库事务。 /// </summary> /// <param name="cmdList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param> public static int ExecuteSqlTran(List <CommandInfo> cmdList) { using (var conn = new SqlConnection(ConnectionString)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { var cmd = new SqlCommand(); try { //LoggerHelper.Info(cmdList); int count = 0; //循环 foreach (CommandInfo myDe in cmdList) { string cmdText = myDe.CommandText; var 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(", StringComparison.Ordinal) == -1) { trans.Rollback(); LoggerHelper.Error("public static int ExecuteSqlTran(List<CommandInfo> cmdList) throw exception,trans.Rollback:" + myDe.CommandText); return(0); } object obj = cmd.ExecuteScalar(); bool isHave = Convert.ToInt32(obj) > 0; if (myDe.EffentNextType == EffentNextType.WhenHaveContine && !isHave) { trans.Rollback(); LoggerHelper.Error("public static int ExecuteSqlTran(List<CommandInfo> cmdList) throw exception,trans.Rollback:" + myDe.EffentNextType); return(0); } if (myDe.EffentNextType == EffentNextType.WhenNoHaveContine && isHave) { trans.Rollback(); LoggerHelper.Error("public static int ExecuteSqlTran(List<CommandInfo> cmdList) throw exception,trans.Rollback:" + myDe.EffentNextType); return(0); } continue; } int val = cmd.ExecuteNonQuery(); count += val; if (myDe.EffentNextType == EffentNextType.ExcuteEffectRows && val == 0) { trans.Rollback(); LoggerHelper.Error("public static int ExecuteSqlTran(List<CommandInfo> cmdList) throw exception,trans.Rollback:" + myDe.EffentNextType); return(0); } cmd.Parameters.Clear(); } trans.Commit(); return(count); } catch (Exception e) { trans.Rollback(); LoggerHelper.Error("public static int ExecuteSqlTran(List<CommandInfo> cmdList) throw exception,trans.Rollback:", e); return(0); } } } }
/// <summary> /// 获取中国节日列表 /// </summary> /// <param name="calenderYear">公历年</param> /// <returns></returns> public List <HolidayEntity> GetChinaFestivalList(string calenderYear) { string appkey = Settings.Instance.GetCalendarAppKey; List <HolidayEntity> result = new List <HolidayEntity>(); for (int i = 1; i < 13; i++) { string url2 = "http://japi.juhe.cn/calendar/month"; var parameters2 = new Dictionary <string, string>(); parameters2.Add("key", appkey); //你申请的key parameters2.Add("year-month", calenderYear + "-" + i.ToString()); //指定月份,格式为YYYY-MM,如月份和日期小于10,则取个位,如:2012-1 string result2 = sendPost(url2, parameters2, "get"); JsonObject newObj2 = new JsonObject(result2); String errorCode2 = newObj2["error_code"].Value; if (errorCode2 == "0") { var holidayObject = new JsonObject("{holiday:" + newObj2["result"].Object["holiday"].Value + "}"); var itemsList = holidayObject["holiday"].Items; foreach (var jsonProperty in itemsList) { var name = jsonProperty.Object["name"].Value; if (result.Where(q => q.Name == name).ToList().Count > 0) { continue; } var desc = jsonProperty.Object["desc"].Value; var year = newObj2["result"].Object["year"].Value; var daysList = jsonProperty.Object["list"].Items; string refDateFrom = string.Empty; string refDateTo = string.Empty; string refStatus = string.Empty; for (int j = 0; j < daysList.Count; j++) { var day = daysList[j]; string tempDate = day.Object["date"].Value; string tempStatus = day.Object["status"].Value; if (!string.IsNullOrWhiteSpace(refDateFrom)) { if (refStatus != tempStatus) { result.Add(new HolidayEntity { Description = desc, From = Convert.ToDateTime(refDateFrom), To = Convert.ToDateTime(refDateTo), Name = name, Status = Convert.ToInt32(refStatus) }); refDateFrom = tempDate; refStatus = tempStatus; } } else if (string.IsNullOrWhiteSpace(refDateFrom)) { refDateTo = refDateFrom = tempDate; refStatus = tempStatus; } if (j == daysList.Count - 1) { if (tempDate == refDateTo && tempStatus == refStatus && !string.IsNullOrWhiteSpace(refStatus)) { refDateTo = tempDate; result.Add(new HolidayEntity { Description = desc, From = Convert.ToDateTime(refDateFrom), To = Convert.ToDateTime(refDateTo), Name = name, Status = Convert.ToInt32(refStatus) }); } else if (tempDate != refDateTo && !string.IsNullOrWhiteSpace(tempDate) && !string.IsNullOrWhiteSpace(refDateTo) && refStatus != tempStatus) { result.Add(new HolidayEntity { Description = desc, From = Convert.ToDateTime(refDateFrom), To = Convert.ToDateTime(refDateTo), Name = name, Status = Convert.ToInt32(refStatus) }); result.Add(new HolidayEntity { Description = desc, From = Convert.ToDateTime(tempDate), To = Convert.ToDateTime(tempDate), Name = name, Status = Convert.ToInt32(refStatus) }); } else if (tempDate != refDateTo && !string.IsNullOrWhiteSpace(tempDate) && !string.IsNullOrWhiteSpace(refDateTo) && refStatus == tempStatus) { refDateTo = tempDate; result.Add(new HolidayEntity { Description = desc, From = Convert.ToDateTime(refDateFrom), To = Convert.ToDateTime(refDateTo), Name = name, Status = Convert.ToInt32(refStatus) }); } } } } } else { //Debug.WriteLine("失败"); LoggerHelper.Error("ErrorCode:" + newObj2["error_code"].Value + " ErrorReason:" + newObj2["reason"].Value + " URL:http://japi.juhe.cn/calendar/month" + " year-month:" + calenderYear + " - " + i.ToString()); //Debug.WriteLine(newObj2["error_code"].Value + ":" + newObj2["reason"].Value); } } return(result); }