c#个人记录常用方法(更新中)
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;
}
c#个人记录常用方法(更新中)的更多相关文章
- C# 知识点记录(持续更新中)
从看C#入门经典开始系统的学习C#,本文主要记录学习过程中的一些知识点,也是我博客生涯的开始,比较重要成体系的部分会单重新写文章整理归纳. 1.一字不变的字符串 @字符 使转义序列不被处理,按照原样输 ...
- CLR via C# 第五章学习记录(更新中)
1.设置全局溢出检查,项目属性->生成->高级->检测运算上溢/下溢 2.局部使用溢出检测 Byte b = ; b = ));// 不检测溢出 checked// 检测溢出代码段 ...
- BUUCTF刷题记录(更新中...)
极客大挑战 2019]EasySQL-1 直接通过输入万能密码:' or 1=1#实现注入: 思考:服务端sql语句应该为:select * from users where username='xx ...
- Linux系统编程重要细节记录(持续更新中)
1.在打印rlim_t值时,需要将其转换为long long并使用%lld printf()修饰符.
- 小程序 - API 踩坑记录(更新中...)
API 小程序API结构导览图: 声明: 请尊重博客园原创精神,转载或使用图片请注明: 博主:xing.org1^ 出处:http://www.cnblogs.com/padding1015/
- 【小TIP】记录各种错误【更新中】
最好程序一遍通过,为了提高代码能力,这里将用TIP的形式记录来犯过的错误.不断更新中. *已经转移到闪存.. [150214]WA:检查是否数组开小了. [150212]WA:如果程序中有乘号,需要留 ...
- Atom使用记录(持续更新中)
部分内容取自:http://www.jianshu.com/p/dd97cbb3c22d,我自己也在使用,持续更新中 Atom安装插件在窗口中File---Setting---install 在里面进 ...
- Appium学习实践(五)遇到的坑(记录自己工作中遇到的坑以及解决方案,不定时更新)
1.错误截图,有时候测试用例执行错误的话,相对于复杂的log,一张错误截图也许能更明确哪里出的问题(当然有时,截图+log还是最好了) 坑:本来是想测试用例fail的时候捕获异常来执行截图操作,但是由 ...
- vue项目常用方法封装,持续更新中。。。
vue项目中可以直接使用 1.常用工具类untils.js中 /* * 验证手机号是否合格 * true--说明合格 */ export function isPhone(phoneStr){ let ...
随机推荐
- Android 如何修改默认的searchable items。
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- mysqldump备份7
http://www.cnblogs.com/ivictor/p/5505307.html 对于MySQL的备份,可分为以下两种: 1. 冷备 2. 热备 其中,冷备,顾名思义,就是将数据库关掉, ...
- Tomcat配置域名访问
在server.xml文件中的<Host>标签里面添加 <Alias>你的域名(比如:www.baidu.com)</Alias> <Context path ...
- Android 换肤功能的实现(Apk插件方式)
一.概述 由于Android 没有提供一套统一的换肤机制,我猜可能是因为国外更注重功能和体验的原因 所以国内如果要做一个漂亮的换肤方案,需要自己去实现. 目前换肤的方法大概有三种方案: (1)把皮肤资 ...
- Excel导入导出帮助类
/// <summary> /// Excel导入导出帮助类 /// 记得引入 NPOI /// 下载地址 http://npoi.codeplex.com/rele ...
- Flume简介与使用(二)——Thrift Source采集数据
Flume简介与使用(二)——Thrift Source采集数据 继上一篇安装Flume后,本篇将介绍如何使用Thrift Source采集数据. Thrift是Google开发的用于跨语言RPC通信 ...
- CentOS7.0下载各版本说明 新增Everything版
来源:http://www.centoscn.com/CentOS/2014/0708/3268.html 下载CentOS-7.0-1406的时候,有很多可选则的版本,对于初学者来说,不知道选择哪个 ...
- 关于Struts2中的值栈与OGNL表达式
1.1.1 OGNL概述: Object Graphic Navigation Language(对象图导航语言)的缩写 * EL :OGNL比EL功能强大很多倍. 它是一个开源项目. ...
- 浅谈用java解析xml文档(三)
接上一篇,本文介绍使用JDOM解析xml文档, 首先我们还是应该知道JDOM从何而来,是Breet Mclaughlin和Jason Hunter两大Java高手的创作成果,2000年初, JDOM作 ...
- JavaScript高级程序设计(第三版)学习笔记11、12、17章
章, DOM扩展 选择符 API Selector API Level1核心方法querySelector .querySelectorAll,兼容的浏览器可以使用 Document,Element ...