博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# winform项目用到的部分知识点总结
阅读量:6191 次
发布时间:2019-06-21

本文共 5739 字,大约阅读时间需要 19 分钟。

项目用到的知识点总结,欢迎大家吐槽:

///         /// 转换非yyyy-MM-dd的字符串为DateTime类型        /// 
public static void ConvertDateFormat() {            string orginStr = "test-test-20130607.xls";            string dateStr = orginStr.Split('-')[2].Split('.')[0];            Console.WriteLine(dateStr);            Console.WriteLine(DateTime.ParseExact(dateStr, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture).ToString("yyyy-M-d"));        }

相关资料:

 

 

///         /// get the days of currDate minus lastDate;The result is negative is allowed         ///         ///         ///         /// 
private int minusTwoDate(string lastDateStr,string currDateStr) { DateTime lastDate = Convert.ToDateTime(lastDateStr); DateTime currDate = Convert.ToDateTime(currDateStr); return (currDate - lastDate).Days; }

 

///        /// judage whether cross the month;cross:true;       ///        /// the last page recorded       ///        /// 
private bool whetherCrossMonth(string lastDateStr, string currDateStr) { DateTime lastDate = Convert.ToDateTime(lastDateStr); DateTime currDate = Convert.ToDateTime(currDateStr); if (currDate.Month == lastDate.Month)//the same month { return false; } else { return true; } }

///         /// is last day:true;is not last day:false and return last date of month        ///         ///         /// the last day of month or null        /// 
is last day:true;or false
private bool wetherIsLastDay(string lastDateStr,out string lastDayOfMonth) { DateTime lastPageDate = Convert.ToDateTime(lastDateStr); //first day of month string lastDay = string.Empty; DateTime lastDateOfMonth = Convert.ToDateTime(lastPageDate.Year + "-" + lastPageDate.Month + "-" + DateTime.DaysInMonth(lastPageDate.Year, lastPageDate.Month)); if ((lastDateOfMonth-lastPageDate).Days>0) { lastDayOfMonth =lastDateOfMonth.ToShortDateString();; return false; }// less than The last day of each month else { lastDayOfMonth = lastDay; return true; }// }

sqlite方面:

//sql。如果businessDate字段不格式化为yyyy-MM-dd,则在max()对此字段取值会//出现2013-1-9大于2013-1-10、2013-1-20的情况sqlList.Add(String.Format("insert into test(businessName,businessDate,indb_datetime) values('{0}','{1}','{2}')", EventOrderKeyStr, fileDate.ToString("yyyy-MM-dd"), DateTime.Now.ToString()));//入库环节 DBHelperSqlite dhSqlite = new DBHelperSqlite(); dhSqlite.ExecuteSqlTran(sqlList);

 

dbhelper:

private string dbName = "cmcc2.db";        private string connectionString = "Data Source=cmcc2.db;Pooling=true;FailIfMissing=false";        ///         /// if the db is not exists,create it and create the table        ///         public DBHelperSqlite()        {            string SQLCreateStr = "CREATE TABLE [cmcc_businesses_info] (businessName nvarchar(50),businessDate nvarchar(10),indb_datetime nvarchar(20))";            #region CASE2            FileInfo fi = new FileInfo(dbName);            if (fi.Exists == false)            {                logger.InfoFormat("db不存在");                //Console.WriteLine("db不存在");                SQLiteConnection.CreateFile(dbName);                logger.InfoFormat("db创建完成");                using (SQLiteConnection conn = new SQLiteConnection(connectionString))                {                    conn.Open();                    using (SQLiteCommand cmd = new SQLiteCommand(SQLCreateStr, conn))                    {                        if (cmd.ExecuteNonQuery() > 0)                        {                            logger.InfoFormat("表创建成功");                            //Console.WriteLine("表创建成功");                        }                    }                }            }            #endregion        }//ctor        ///         /// 执行多条SQL语句,实现数据库事务。        ///         /// 多条SQL语句                public void ExecuteSqlTran(IList
SQLStringList) { using (SQLiteConnection conn = new SQLiteConnection(connectionString)) { conn.Open(); SQLiteCommand cmd = new SQLiteCommand(); cmd.Connection = conn; SQLiteTransaction tran = conn.BeginTransaction(); cmd.Transaction = tran; try { for (int n = 0; n < SQLStringList.Count; n++) { string strsql = SQLStringList[n].ToString(); if (strsql.Trim().Length > 1) { logger.Debug(strsql); cmd.CommandText = strsql; cmd.ExecuteNonQuery(); } } tran.Commit(); } catch (System.Data.SQLite.SQLiteException ex) { logger.Error(ex); tran.Rollback(); //throw new Exception(ex.Message); } } }//trans
SubString函数使用时一个隐藏的“雷”:
///         /// str.Substring(startIndex,length)如果length大于str的长度,就会报错        ///         public static void SubstringTest2()        {            string str = "4.333";            string[] r = str.Split('.');            if (r.Length == 2)            {                if (r[1].Length > 4)                {                    r[1] = r[1].Substring(0, 4);                }                Console.WriteLine(r[0] + "." + r[1]);            }        }

转载于:https://www.cnblogs.com/softidea/p/3256889.html

你可能感兴趣的文章
【Android】仿斗鱼滑动拼图验证码控件
查看>>
让 UIWebview 拥有超强的图片处理能力
查看>>
异步社区本周(5.14-5.20)半价电子书
查看>>
Xcode 高级调试技巧
查看>>
理解DDoS防护本质:基于资源较量和规则过滤的智能化系统
查看>>
js 中基础数据结构数组去重问题
查看>>
Android Gradle(一)为什么现在要用Gradle?
查看>>
Python 简单入门指北(试读版)
查看>>
技术变化那么快,程序员如何做到不被淘汰?
查看>>
虚拟DOM总结
查看>>
阿里资深技术专家总结:要怎样努力才可以成为公司主力架构师
查看>>
k-means 聚类算法
查看>>
我们来说一说TCP神奇的40ms
查看>>
基于AOP的MVP框架(一)GoMVP的使用
查看>>
拜托,面试别再问我回文链表了!!!(leetcode 234)
查看>>
iOS冰与火之歌 – UAF and Kernel Pwn
查看>>
通过nginx配置文件抵御攻击
查看>>
攻击JavaWeb应用[7]-Server篇[1]
查看>>
使用新版Android Studio检测内存泄露和性能
查看>>
计算机系统002 - 数值运算
查看>>