当前位置: 首页 > news >正文

c# winform项目用到的部分知识点总结

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

        /// <summary>
        /// 转换非yyyy-MM-dd的字符串为DateTime类型
        /// </summary>
        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"));
        }

相关资料:

http://www.jb51.net/article/37007.htm

http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx

 

 

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

 

       /// <summary>
       /// judage whether cross the month;cross:true;
       /// </summary>
       /// <param name="lastDateStr">the last page recorded</param>
       /// <param name="currDateStr"></param>
       /// <returns></returns>
        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;
            }
        }

        /// <summary>
        /// is last day:true;is not last day:false and return last date of month
        /// </summary>
        /// <param name="lastDateStr"></param>
        /// <param name="lastDayOfMonth">the last day of month or null</param>
        /// <returns>is last day:true;or false</returns>
        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";
        /// <summary>
        /// if the db is not exists,create it and create the table
        /// </summary>
        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

        /// <summary>
        /// 执行多条SQL语句,实现数据库事务。
        /// </summary>
        /// <param name="SQLStringList">多条SQL语句</param>        
        public void ExecuteSqlTran(IList<string> 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函数使用时一个隐藏的“雷”:
        /// <summary>
        /// str.Substring(startIndex,length)如果length大于str的长度,就会报错
        /// </summary>
        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

相关文章:

  • Intellij IDEA 快捷键整理(TonyCody)
  • C optimization tutorial 翻译 C语言优化教程(一)
  • eclipse 的代码着色插件 --Eclipse Color Theme
  • CentOS上yum安装nginx+mysql+php+php-fastcgi
  • 精简操作系统Linux
  • UVa 10827 - Maximum sum on a torus
  • 九大CTO畅谈软件定义未来
  • 磁盘被写保护
  • Eclipse can not find the tag library descriptor for http://java.sun.com/jsf/*
  • 【AaronYang风格】第一篇 CodeFirst 初恋
  • 想看,该看,需要看的书。。。。。。
  • Android之HttpPost与HttpGet使用
  • Linux下基于POSIX标准的共享内存操作示例
  • 分花生
  • 尽量使用ToUpper比较,避免使用ToLower
  • [译] 理解数组在 PHP 内部的实现(给PHP开发者的PHP源码-第四部分)
  • 2017 前端面试准备 - 收藏集 - 掘金
  • android 一些 utils
  • Apache Spark Streaming 使用实例
  • C++类中的特殊成员函数
  • canvas 绘制双线技巧
  • Java知识点总结(JDBC-连接步骤及CRUD)
  • JS变量作用域
  • leetcode-27. Remove Element
  • miniui datagrid 的客户端分页解决方案 - CS结合
  • Rancher如何对接Ceph-RBD块存储
  • Redis学习笔记 - pipline(流水线、管道)
  • 包装类对象
  • 仿天猫超市收藏抛物线动画工具库
  • 理解IaaS, PaaS, SaaS等云模型 (Cloud Models)
  • 力扣(LeetCode)56
  • 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!
  • 小程序01:wepy框架整合iview webapp UI
  • 在 Chrome DevTools 中调试 JavaScript 入门
  • 7行Python代码的人脸识别
  • 数据库巡检项
  • ​LeetCode解法汇总2583. 二叉树中的第 K 大层和
  • ​比特币大跌的 2 个原因
  • #pragma multi_compile #pragma shader_feature
  • #Spring-boot高级
  • #我与Java虚拟机的故事#连载17:我的Java技术水平有了一个本质的提升
  • $L^p$ 调和函数恒为零
  • (1/2)敏捷实践指南 Agile Practice Guide ([美] Project Management institute 著)
  • (C#)Windows Shell 外壳编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令...
  • (二)正点原子I.MX6ULL u-boot移植
  • (附源码)spring boot校园健康监测管理系统 毕业设计 151047
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (正则)提取页面里的img标签
  • (转载)微软数据挖掘算法:Microsoft 时序算法(5)
  • .NET CF命令行调试器MDbg入门(四) Attaching to Processes
  • .NET Core 成都线下面基会拉开序幕
  • .net framwork4.6操作MySQL报错Character set ‘utf8mb3‘ is not supported 解决方法
  • .net 写了一个支持重试、熔断和超时策略的 HttpClient 实例池
  • .NET 应用启用与禁用自动生成绑定重定向 (bindingRedirect),解决不同版本 dll 的依赖问题
  • .NET 中 GetHashCode 的哈希值有多大概率会相同(哈希碰撞)