1.日期毫秒转换为标准的C#日期格式
//使用时,先将秒Convert.ToInt64,返回格式2015-2-10 14:03:33
public DateTime JavaTimeToC(long ltime)
{
long time_JAVA_Long = ltime;//java长整型日期,毫秒为单位
DateTime dt_1970 = new DateTime(, , );
long tricks_1970 = dt_1970.Ticks;//1970年1月1日刻度
long time_tricks = tricks_1970 + time_JAVA_Long * ;//日志日期刻度
DateTime dt = new DateTime(time_tricks, DateTimeKind.Unspecified).AddHours();//转化为DateTime
return dt;
}
2.带事务的数据库插入更新删除方法
public void DbOp(List<string> sqlNum, ref string error)
{
if (sqlNum.Count <= )
{
error = "sql语句为空,没有订单数据!";
return;
}
try
{
if (!string.IsNullOrEmpty(strDbConnection))//缺少数据库连接字符串
{
using (OracleConnection con = new OracleConnection(strDbConnection))
{
using (OracleCommand cmd = con.CreateCommand())
{
con.Open();
OracleTransaction trans = con.BeginTransaction();//开始事务
cmd.Transaction = trans;//设置事务
string[] sql = sqlNum.ToArray();
try
{ for (int i = ; i < sql.Length; i++)
{
cmd.CommandText = sql[i];
cmd.ExecuteNonQuery();
}
trans.Commit();//提交事务
}
catch (Exception ex)
{
error += ex;
trans.Rollback();//回滚
}
finally
{
con.Close();//始终关闭数据库连接
}
}
}
}
else
{
//缺少数据库连接字符串
error += "缺少数据库连接字符串";
}
}
catch (Exception ex)
{ error += ex;
}
}
3.数据库查询方法
public DataSet Query(string SQLString)
{
using (OracleConnection connection = new OracleConnection(strDbConnection))
{
DataSet ds = new DataSet();
try
{
connection.Open();
OracleDataAdapter command = new OracleDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.OracleClient.OracleException ex)
{
throw new Exception(ex.Message);
}
finally
{
connection.Close();
}
return ds;
}
}
4.DataTime,转换为string类型
public string DateTimeChange(DateTime dt)
{
string strDT = dt.Year.ToString();
if (dt.Month.ToString().Length == )
{
strDT += "-0" + dt.Month.ToString();
}
else
{
strDT += "-" + dt.Month.ToString();
} if (dt.Day.ToString().Length == )
{
strDT += "-0" + dt.Day.ToString();
}
else
{
strDT += "-" + dt.Day.ToString();
} if (dt.Hour.ToString().Length == )
{
strDT += "" + dt.Hour.ToString();
}
else
{
strDT += " " + dt.Hour.ToString();
} if (dt.Minute.ToString().Length == )
{
strDT += ":0" + dt.Minute.ToString();
}
else
{
strDT += ":" + dt.Minute.ToString();
} if (dt.Second.ToString().Length == )
{
strDT += ":0" + dt.Second.ToString();
}
else
{
strDT += ":" + dt.Second.ToString();
}
return strDT;
}
5.写日志dll
//地址:http://files.cnblogs.com/files/valiant1882331/%E5%86%99%E6%97%A5%E5%BF%97.rar
//使用
Log.ClsLog lo = new Log.ClsLog();
lo.writelogtype("",);
lo.writelogtype("", ,"日志文件名称");
dll源代码
public class ClsLog
{
/// 不限定名称写日志主方法
/// <summary>
/// 不限定名称写日志主方法
/// </summary>
/// <param name="msg">1string2Exception</param>
/// <param name="code">1应用程序日志2SQL日志3应用程序错误4SQL错误5下载错误6上传错误7下载日志8上传日志9初1错10初1日11初2错12初2日13初3错14初3日15初4错16初4日17初5错18初5日19初6错20初6日21初0错22初0日23实1错24实1日25实2错26实2日27实0错28实0日29打0错30打0日31池0错32池0日33判0错34判0日35按键36xssql37upsql38downsql39dayinsql40chu1sql41chu2sql42chu3sql43chu4sql44chu5sql45upbeifensql46upedsql</param>
public void writelogtype(object msg, int code)
{
string strErr = "";
Exception e = null;
switch (code)
{
case :
writelog(msg.ToString().Trim(), "applog");
break;
case :
writelog(msg.ToString().Trim(), "sqllog");
break;
case :
e = msg as Exception;
strErr = "Source:" + e.Source + Environment.NewLine;
strErr += "Message:" + e.Message + Environment.NewLine;
strErr += "StackTrace:" + e.StackTrace;
writelog(strErr, "apperr");
break;
case :
e = msg as Exception;
strErr = "Source:" + e.Source + Environment.NewLine;
strErr += "Message:" + e.Message + Environment.NewLine;
strErr += "StackTrace:" + e.StackTrace;
writelog(strErr, "sqlerr");
break;
case :
e = msg as Exception;
strErr = "Source:" + e.Source + Environment.NewLine;
strErr += "Message:" + e.Message + Environment.NewLine;
strErr += "StackTrace:" + e.StackTrace;
writelog(strErr, "downerr");
break;
case :
e = msg as Exception;
strErr = "Source:" + e.Source + Environment.NewLine;
strErr += "Message:" + e.Message + Environment.NewLine;
strErr += "StackTrace:" + e.StackTrace;
writelog(strErr, "uperr");
break;
case :
writelog(msg.ToString().Trim(), "downlog");
break;
case :
writelog(msg.ToString().Trim(), "uplog");
break;
case :
e = msg as Exception;
strErr = "Source:" + e.Source + Environment.NewLine;
strErr += "Message:" + e.Message + Environment.NewLine;
strErr += "StackTrace:" + e.StackTrace;
writelog(strErr, "initerr1");
break;
case :
writelog(msg.ToString().Trim(), "initlog1");
break;
case :
e = msg as Exception;
strErr = "Source:" + e.Source + Environment.NewLine;
strErr += "Message:" + e.Message + Environment.NewLine;
strErr += "StackTrace:" + e.StackTrace;
writelog(strErr, "initerr2");
break;
case :
writelog(msg.ToString().Trim(), "initlog2");
break;
case :
e = msg as Exception;
strErr = "Source:" + e.Source + Environment.NewLine;
strErr += "Message:" + e.Message + Environment.NewLine;
strErr += "StackTrace:" + e.StackTrace;
writelog(strErr, "initerr3");
break;
case :
writelog(msg.ToString().Trim(), "initlog3");
break;
case :
e = msg as Exception;
strErr = "Source:" + e.Source + Environment.NewLine;
strErr += "Message:" + e.Message + Environment.NewLine;
strErr += "StackTrace:" + e.StackTrace;
writelog(strErr, "initerr4");
break;
case :
writelog(msg.ToString().Trim(), "initlog4");
break;
case :
e = msg as Exception;
strErr = "Source:" + e.Source + Environment.NewLine;
strErr += "Message:" + e.Message + Environment.NewLine;
strErr += "StackTrace:" + e.StackTrace;
writelog(strErr, "initerr5");
break;
case :
writelog(msg.ToString().Trim(), "initlog5");
break;
case :
e = msg as Exception;
strErr = "Source:" + e.Source + Environment.NewLine;
strErr += "Message:" + e.Message + Environment.NewLine;
strErr += "StackTrace:" + e.StackTrace;
writelog(strErr, "initerr6");
break;
case :
writelog(msg.ToString().Trim(), "initlog6");
break;
case :
e = msg as Exception;
strErr = "Source:" + e.Source + Environment.NewLine;
strErr += "Message:" + e.Message + Environment.NewLine;
strErr += "StackTrace:" + e.StackTrace;
writelog(strErr, "initerr0");
break;
case :
writelog(msg.ToString().Trim(), "initlog0");
break;
case :
e = msg as Exception;
strErr = "Source:" + e.Source + Environment.NewLine;
strErr += "Message:" + e.Message + Environment.NewLine;
strErr += "StackTrace:" + e.StackTrace;
writelog(strErr, "shierr1");
break;
case :
writelog(msg.ToString().Trim(), "shilog1");
break;
case :
e = msg as Exception;
strErr = "Source:" + e.Source + Environment.NewLine;
strErr += "Message:" + e.Message + Environment.NewLine;
strErr += "StackTrace:" + e.StackTrace;
writelog(strErr, "shierr2");
break;
case :
writelog(msg.ToString().Trim(), "shilog2");
break;
case :
e = msg as Exception;
strErr = "Source:" + e.Source + Environment.NewLine;
strErr += "Message:" + e.Message + Environment.NewLine;
strErr += "StackTrace:" + e.StackTrace;
writelog(strErr, "shierr0");
break;
case :
writelog(msg.ToString().Trim(), "shilog0");
break;
case :
e = msg as Exception;
strErr = "Source:" + e.Source + Environment.NewLine;
strErr += "Message:" + e.Message + Environment.NewLine;
strErr += "StackTrace:" + e.StackTrace;
writelog(strErr, "dayinerr0");
break;
case :
writelog(msg.ToString().Trim(), "dayinlog0");
break;
case :
e = msg as Exception;
strErr = "Source:" + e.Source + Environment.NewLine;
strErr += "Message:" + e.Message + Environment.NewLine;
strErr += "StackTrace:" + e.StackTrace;
writelog(strErr, "chierr0");
break;
case :
writelog(msg.ToString().Trim(), "chilog0");
break;
case :
e = msg as Exception;
strErr = "Source:" + e.Source + Environment.NewLine;
strErr += "Message:" + e.Message + Environment.NewLine;
strErr += "StackTrace:" + e.StackTrace;
writelog(strErr, "panerr0");
break;
case :
writelog(msg.ToString().Trim(), "panlog0");
break;
case :
writelog(msg.ToString().Trim(), "anjian");
break;
case :
//writelog(msg.ToString().Trim(), "xssql");
break;
case :
writelog(msg.ToString().Trim(), "upsql");
break;
case :
writelog(msg.ToString().Trim(), "downsql");
break;
case :
writelog(msg.ToString().Trim(), "dayinsql");
break;
case :
writelog(msg.ToString().Trim(), "chu1sql");
break;
case :
writelog(msg.ToString().Trim(), "chu2sql");
break;
case :
writelog(msg.ToString().Trim(), "chu3sql");
break;
case :
writelog(msg.ToString().Trim(), "chu4sql");
break;
case :
writelog(msg.ToString().Trim(), "chu5sql");
break;
case :
writelog(msg.ToString().Trim(), "upbeifensql");
break;
case :
writelog(msg.ToString().Trim(), "upedsql");
break;
case :
writelog(msg.ToString().Trim(), "lianjisql");
break;
case :
writelog(msg.ToString().Trim(), "JieKou");
break;
case :
writelog(msg.ToString().Trim(), "ShuHao");
break;
//40chu1sql41chu2sql42chu3sql43chu4sql44chu5sql
}
} ///写日志,自定义日志名称,类型
/// <summary>
/// 写日志,自定义日志名称,类型
/// </summary>
/// <param name="msg">错误信息</param>
/// <param name="code">1:异常错误信息ex.Message 2,ex</param>
/// <param name="logName">日志名称</param>
/// <returns></returns>
public string writeLogType(object msg, int code, string logName)
{
string strErr = "";
logName = logName.Replace(':','!');
Exception e = null;
switch (code)
{
case :
return writelog(msg.ToString().Trim(), logName);
case :
e = msg as Exception;
strErr = "Source:" + e.Source + Environment.NewLine;
strErr += "Message:" + e.Message + Environment.NewLine;
strErr += "StackTrace:" + e.StackTrace;
return writelog(strErr, logName);
}
return "";
}
///写日志主方法,仅调用
/// <summary>
/// 写日志主方法,仅调用
/// </summary>
/// <param name="value">错误信息</param>
/// <param name="name">日志名称</param>
/// <returns></returns>
private string writelog(string value, string name)
{
string strPath = "";//定义日志路径
try
{
//如果是http,web请求,则路径为web服务器上的虚拟路径对应的物理路径
strPath = System.Web.HttpContext.Current.Server.MapPath("log");
}
catch (Exception)
{
}
if (strPath == "")
{
//如果没有web路径,就设置路径为本地可执行文件个目录
strPath = Application.StartupPath;
}
FileStream logFile = null;
//
string strDate = DateTime.Now.Year.ToString();//获取当前如期年份部分
strDate += "-" + DateTime.Now.Month.ToString();//2000-01
strDate += "-" + DateTime.Now.Day.ToString();//2000-01-01
if(!Directory.Exists(strPath+"\\log"))
{
Directory.CreateDirectory(strPath+"\\log");
}
if (!Directory.Exists(strPath + "\\log\\" + strDate))
{
Directory.CreateDirectory(strPath + "\\log\\" + strDate);
}
//设置文件路径下的文件名称
strPath = strPath + "\\log\\" + strDate + "\\" + name + ".txt";
if (logFile == null)
{
logFile = new FileStream(strPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);//FileShare.ReadWrite允许以后打开文件读写
}
string strLine = Environment.NewLine;//换行符
string strTime = DateTime.Now.ToString("HH:mm:ss");//当前时间格式
string str = "";
if (name != "upbeifensql" && name != "upedsql" && name != "lianjisql")
str = strTime + strLine;
str += value + strLine;
if (name != "upbeifensql" && name != "upedsql" && name != "lianjisql")
str += "*****************************************************************************************************************************" + strLine;
#region old
//if (name == "upbeifensql" || name == "upedsql" || name == "lianjisql")
//{
// CompressionHelper compress = new CompressionHelper();
// str = compress.CompressToString(str);
// str += strLine;
//}
#endregion
byte[] bytes = System.Text.Encoding.Default.GetBytes(str);
logFile.Position = logFile.Length;
logFile.Write(bytes, , (int)bytes.Length);
logFile.Close();
logFile = null;
return strPath;
}
}
6. 省市区三级联动js版(自定义控件)
7.正则表达式去除html标签
public static string NoHTML(string Htmlstring)
{
//删除脚本
//删除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
Htmlstring = Htmlstring.Replace("<", "<");
Htmlstring = Htmlstring.Replace(">", ">");
Htmlstring = Htmlstring.Replace("\r\n", "");
return Htmlstring;
}