380 lines
13 KiB
C#
380 lines
13 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Configuration;
|
||
using System.Text;
|
||
using SuperMap.RealEstate.Web.UI.WebControls;
|
||
|
||
namespace HZQR.Common.Common
|
||
{
|
||
/// <summary>
|
||
/// 通用方法
|
||
/// </summary>
|
||
public class Common
|
||
{
|
||
#region 方法 -> 获得GridView的查询条件
|
||
/// <summary>
|
||
/// 获得GridView的查询条件
|
||
/// </summary>
|
||
/// <param name="e">SelectMethodParametersArgs</param>
|
||
/// <returns></returns>
|
||
public static string GetSqlWhereByParameter(SelectMethodParametersArgs e)
|
||
{
|
||
string sqlWhere = "";
|
||
string whereSqlString = e.WhereSqlString;
|
||
foreach (var item in e.Parameters)
|
||
{
|
||
var key = item.Key;
|
||
var val = item.Value.ToString();
|
||
if (val.StartsWith("%") && val.EndsWith("%"))
|
||
{
|
||
whereSqlString = whereSqlString.Replace(":" + key + " ", "'" + val + "' ").Replace(":" + key + ")", "'" + val + "')");
|
||
}
|
||
else
|
||
{
|
||
whereSqlString = whereSqlString.Replace(":" + key, val);
|
||
}
|
||
}
|
||
sqlWhere = whereSqlString.TrimStart(' ');
|
||
if (!string.IsNullOrEmpty(sqlWhere) && !sqlWhere.StartsWith("where"))
|
||
{
|
||
sqlWhere = " WHERE " + sqlWhere;
|
||
}
|
||
if (sqlWhere.IndexOf("order by") > -1)
|
||
{
|
||
sqlWhere = sqlWhere.Substring(0, sqlWhere.IndexOf("order by"));
|
||
}
|
||
return sqlWhere;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> SQL中拼IN最大表达式数为1000,超过则用OR替换
|
||
public static string GetInString(string id, List<string> list)
|
||
{
|
||
StringBuilder sb = new StringBuilder();
|
||
String returnString = "";
|
||
if (list.Count == 0 || null == list)
|
||
{
|
||
returnString = sb.Append(id).Append("=''").ToString();
|
||
}
|
||
for (int i = 0; i < list.Count; i++)
|
||
{
|
||
if (i == 0)
|
||
{
|
||
sb.Append(id);
|
||
sb.Append(" in (");
|
||
}
|
||
sb.Append("'");
|
||
sb.Append(list[i].ToString());
|
||
sb.Append("'");
|
||
if (i >= 900 && i < list.Count - 1)
|
||
{
|
||
if (i % 900 == 0)
|
||
{
|
||
sb.Append(") or ");
|
||
sb.Append(id);
|
||
sb.Append(" in (");
|
||
}
|
||
else
|
||
{
|
||
sb.Append(",");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (i < list.Count - 1)
|
||
{
|
||
sb.Append(",");
|
||
}
|
||
}
|
||
if (i == list.Count - 1)
|
||
{
|
||
sb.Append(")");
|
||
}
|
||
}
|
||
returnString = "(" + sb.ToString() + ")";
|
||
return returnString;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 获取上个月第一天和最后一天
|
||
//获取上个月第一天
|
||
public static DateTime FirstDayOfPreviousMonth(DateTime datetime)
|
||
{
|
||
return datetime.AddDays(1 - datetime.Day).AddMonths(-1);
|
||
}
|
||
|
||
//获取上个月的最后一天
|
||
public static DateTime LastDayOfPrdviousMonth(DateTime datetime)
|
||
{
|
||
return datetime.AddDays(1 - datetime.Day).AddDays(-1);
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 将DateTime类型转换为long类型
|
||
/// <summary>
|
||
/// 将DateTime类型转换为long类型
|
||
/// </summary>
|
||
/// <param name="dt">时间</param>
|
||
/// <returns></returns>
|
||
public static long ConvertDataTimeLong(DateTime dt)
|
||
{
|
||
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
|
||
TimeSpan toNow = dt.Subtract(dtStart);
|
||
long timeStamp = toNow.Ticks;
|
||
timeStamp = long.Parse(timeStamp.ToString().Substring(0, timeStamp.ToString().Length - 7));
|
||
return timeStamp;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 将Long类型转换为DateTime类型
|
||
/// <summary>
|
||
/// 将Long类型转换为DateTime类型
|
||
/// </summary>
|
||
/// <param name="d">long</param>
|
||
/// <returns></returns>s
|
||
public static DateTime ConvertLongDateTime(long d)
|
||
{
|
||
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
|
||
long lTime = long.Parse(d + "0000000");
|
||
TimeSpan toNow = new TimeSpan(lTime);
|
||
DateTime dtResult = dtStart.Add(toNow);
|
||
return dtResult;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 时间戳(毫秒值)String转换为DateTime类型转换
|
||
/// </summary>
|
||
/// <param name="time">毫秒时间戳</param>
|
||
/// <returns></returns>
|
||
public static DateTime TicksToDate(string time)
|
||
{
|
||
return new DateTime((Convert.ToInt64(time) * 10000) + 621355968000000000);
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 解析数字型的日期
|
||
/// <summary>
|
||
/// 解析数字型的日期
|
||
/// </summary>
|
||
/// <param name="DateTimeString">数字型的日期</param>
|
||
/// <returns></returns>
|
||
public static string TranslateDateTime(string DateTimeString)
|
||
{
|
||
string str = "";
|
||
|
||
switch (DateTimeString.Length)
|
||
{
|
||
case 6:
|
||
str = DateTimeString.Substring(0, 4) + "/" + DateTimeString.Substring(4, 2);
|
||
break;
|
||
case 8:
|
||
str = DateTimeString.Substring(0, 4) + "/" + DateTimeString.Substring(4, 2) +
|
||
"/" + DateTimeString.Substring(6, 2);
|
||
break;
|
||
default:
|
||
if (DateTimeString.Length >= 14)
|
||
{
|
||
str = DateTimeString.Substring(0, 4) + "/" + DateTimeString.Substring(4, 2) +
|
||
"/" + DateTimeString.Substring(6, 2) + " " + DateTimeString.Substring(8, 2) + ":" +
|
||
DateTimeString.Substring(10, 2) + ":" + DateTimeString.Substring(12, 2);
|
||
}
|
||
break;
|
||
}
|
||
|
||
return str;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 将数字型月份转成日期格式(当月第一天)
|
||
/// <summary>
|
||
/// 将数字型月份转成日期格式(当月第一天)
|
||
/// </summary>
|
||
/// <param name="MonthString">月份字符串</param>
|
||
/// <returns></returns>
|
||
public static DateTime TranslateMonth(string MonthString)
|
||
{
|
||
string str = MonthString.Substring(0, 4) + "-" + MonthString.Substring(4, 2) + "-01";
|
||
|
||
return DateTime.Parse(str);
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 将数字型数据插入到Oracle数据库
|
||
/// <summary>
|
||
/// 将数字型数据插入到Oracle数据库
|
||
/// </summary>
|
||
/// <param name="data">数字型数据</param>
|
||
/// <returns></returns>
|
||
public static string InsertNumberToOracle(int? data)
|
||
{
|
||
if (data != null)
|
||
{
|
||
return data.ToString();
|
||
}
|
||
else
|
||
{
|
||
return "NULL";
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将数字型数据插入到Oracle数据库
|
||
/// </summary>
|
||
/// <param name="data">字符串数据</param>
|
||
/// <returns></returns>
|
||
public static string InsertNumberToOracle(string data)
|
||
{
|
||
if (!string.IsNullOrWhiteSpace(data))
|
||
{
|
||
return data;
|
||
}
|
||
else
|
||
{
|
||
return "NULL";
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 将日期型数据插入到Oracle数据库
|
||
/// <summary>
|
||
/// 将日期型数据插入到Oracle数据库
|
||
/// </summary>
|
||
/// <param name="data">日期型数据</param>
|
||
/// <returns></returns>
|
||
public static string InsertDateToOracle(DateTime? data)
|
||
{
|
||
if (data != null)
|
||
{
|
||
return "TO_DATE('" + data + "','YYYY/MM/DD HH24:MI:SS')";
|
||
}
|
||
else
|
||
{
|
||
return "NULL";
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将日期型数据插入到Oracle数据库
|
||
/// </summary>
|
||
/// <param name="data">字符串数据</param>
|
||
/// <returns></returns>
|
||
public static string InsertDateToOracle(string data)
|
||
{
|
||
if (!string.IsNullOrWhiteSpace(data))
|
||
{
|
||
return "TO_DATE('" + data + "','YYYY/MM/DD HH24:MI:SS')";
|
||
}
|
||
else
|
||
{
|
||
return "NULL";
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 获取字符串中某个字符在第几次出现的位置
|
||
/// <summary>
|
||
/// 获取字符串中某个字符在第几次出现的位置
|
||
/// </summary>
|
||
/// <param name="originValue">字符串</param>
|
||
/// <param name="searchValue">检索字符</param>
|
||
/// <param name="startIndex">搜索起始位置</param>
|
||
/// <param name="searchTime">第几次出现</param>
|
||
/// <returns></returns>
|
||
public static int ValueIndexOf(string originValue, char searchValue, int startIndex = 0, uint searchTime = 1)
|
||
{
|
||
int position = -1;
|
||
//判断字符串中是否存在检索字符串
|
||
if (originValue.Substring(startIndex).IndexOf(searchValue) > 0)
|
||
{
|
||
//将字符串按照originValue进行分组
|
||
string[] values = originValue.Substring(startIndex).Split(searchValue);
|
||
//遍历数组,查找searchValue出现的位置
|
||
for (int showTime = 0; showTime < values.Length && showTime < searchTime; showTime++)
|
||
{
|
||
position += values[showTime].Length + 1;
|
||
}
|
||
}
|
||
|
||
return position;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取字符串中某个字符在第几次出现的位置
|
||
/// </summary>
|
||
/// <param name="originValue">字符串</param>
|
||
/// <param name="searchValue">检索字符串</param>
|
||
/// <param name="startIndex">搜索起始位置</param>
|
||
/// <param name="searchTime">第几次出现</param>
|
||
/// <returns></returns>
|
||
public static int ValueIndexOf(string originValue, string searchValue, int startIndex = 0, uint searchTime = 1)
|
||
{
|
||
int position = -1;
|
||
//判断字符串中是否存在检索字符串
|
||
if (originValue.Substring(startIndex).IndexOf(searchValue) > 0)
|
||
{
|
||
//将字符串按照originValue进行分组
|
||
string[] values = originValue.Substring(startIndex).Split(searchValue);
|
||
//遍历数组,查找searchValue出现的位置
|
||
for (int showTime = 0; showTime < values.Length && showTime < searchTime; showTime++)
|
||
{
|
||
position += values[showTime].Length + 1;
|
||
}
|
||
}
|
||
|
||
return position;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 更新AppSettings中的键值
|
||
/// <summary>
|
||
/// 更新AppSettings中的键值
|
||
/// </summary>
|
||
/// <param name="key">指定键的字符串</param>
|
||
/// <param name="value">指定值的字符串</param>
|
||
public static void AddUpdateAppSettings(string key, string value)
|
||
{
|
||
var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
|
||
var settings = configFile.AppSettings.Settings;
|
||
if (settings[key] == null)
|
||
{
|
||
settings.Add(key, value);
|
||
}
|
||
else
|
||
{
|
||
settings[key].Value = value;
|
||
}
|
||
configFile.Save(ConfigurationSaveMode.Modified);
|
||
ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 获取数据表字段查询语句
|
||
/// <summary>
|
||
/// 获取数据表字段查询语句
|
||
/// </summary>
|
||
/// <param name="OwnerName">用户名称</param>
|
||
/// <param name="TableName">数据表名</param>
|
||
/// <returns></returns>
|
||
public static string GetTableColumnSQL(string OwnerName, string TableName)
|
||
{
|
||
string SQLString = string.Format(@"SELECT
|
||
A.OWNER,A.TABLE_NAME,A.COLUMN_NAME,B.COMMENTS,
|
||
A.DATA_TYPE,A.CHAR_LENGTH,A.DATA_PRECISION,
|
||
A.DATA_SCALE,A.NULLABLE,A.DATA_DEFAULT,A.COLUMN_ID
|
||
FROM
|
||
DBA_TAB_COLUMNS A
|
||
LEFT JOIN
|
||
ALL_COL_COMMENTS B
|
||
ON
|
||
A.OWNER = B.OWNER AND
|
||
A.TABLE_NAME = B.TABLE_NAME AND
|
||
A.COLUMN_NAME = B.COLUMN_NAME
|
||
WHERE
|
||
A.OWNER = '{0}' AND A.TABLE_NAME = '{1}'", OwnerName, TableName);
|
||
|
||
return SQLString;
|
||
}
|
||
#endregion
|
||
}
|
||
}
|