现在的位置: 首页 > 综合 > 正文

2.20-2.21知识总结

2013年12月02日 ⁄ 综合 ⁄ 共 3643字 ⁄ 字号 评论关闭

一、C#连接数据库字符串:

    混合身份登录:Data Source=.;Initial Catalog=MyTest;Persist Security Info=True;User ID=sa;Password=900315  

    windows登录:data source=.;database=csdnnews;integrated security=true  

    连接SQLExpressData Source=.;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True

 

二、 C#实现用户登录的代码:

 string str = "server=.;database=数据库名;integrated security=true";

         SqlConnection sqlcon = new SqlConnection();

         sqlcon.ConnectionString = str;

         SqlCommand sqlcmd = new SqlCommand();

         sqlcmd.Connection = sqlcon;

         sqlcmd.CommandText = "select *from 表名 where Name='"+this.TextBox1.Text+"' and Shouji='"+this.TextBox2.Text+"'";

         sqlcon.Open();

         SqlDataReader reader = sqlcmd.ExecuteReader();

         if (reader.Read())

         {

             this.Response.Write("登陆成功");

         }

         else

         {

             this.Response.Write("登陆失败");

         }

         reader.Close();

         sqlcon.Close();

 

三、C#中时间对比的应用:

 

 DateTime DateTime1,   DateTime2 = DateTime.Now  ;//现在时间

   DateTime1 =Convert.ToDateTime("2009-04-24 20:00:00"); //设置要求的减的时间             

 string dateDiff = null;             

 TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);             

 TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);            

   TimeSpan ts = ts1.Subtract(ts2).Duration();              //显示时间             

 dateDiff = ts.Days.ToString() + "" + ts.Hours.ToString() + "小时"  + ts.Minutes.ToString() + "分钟"  ;

四、c#获取系统时间的方法(zt

今天 DateTime.Now.Date.ToShortDateString();

昨天,就是今天的日期减一 DateTime.Now.AddDays(-1).ToShortDateString();

明天,同理,加一 DateTime.Now.AddDays(1).ToShortDateString();

本周(要知道本周的第一天就得先知道今天是星期几,从而得知本周的第一天就是几天前的那一天,要注意的是这里的每一周是从周日始至周六止 DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString(); DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();

如果你还不明白,再看一下中文显示星期几的方法就应该懂了

由于DayOfWeek返回的是数字的星期几,我们要把它转换成汉字方便我们阅读,有些人可能会用switch来一个一个地对照,其实不用那么麻烦的 string[] Day = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; Day[Convert.ToInt16(DateTime.Now.DayOfWeek)];

上周,同理,一个周是7天,上周就是本周再减去7天,下周也是一样 DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString(); DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();

下周 DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString(); DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();

本月,很多人都会说本月的第一天嘛肯定是1号,最后一天就是下个月一号再减一天。当然这是对的

一般的写法 DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1";

第一天 DateTime.Parse(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1").AddMonths(1).AddDays(-1).ToShortDateString();

最后一天 --DateTime 数字型 System.DateTime currentTime=new System.DateTime();

1.1 取当前年月日时分秒 currentTime=System.DateTime.Now;

1.2 取当前年 int =currentTime.Year;

1.3 取当前月 int =currentTime.Month;

1.4 取当前日 int =currentTime.Day;

1.5 取当前时 int =currentTime.Hour;

1.6 取当前分 int =currentTime.Minute;

1.7 取当前秒 int =currentTime.Second;

1.8 取当前毫秒 int 毫秒=currentTime.Millisecond; (变量可用中文)

1.9 取中文日期显示——年月日时分 string strY=currentTime.ToString("f");

不显示秒

1.10 取中文日期显示_年月 string strYM=currentTime.ToString("y");

1.11 取中文日期显示_月日 string strMD=currentTime.ToString("m");

1.12 取当前年月日,格式为:2003-9-23 string strYMD=currentTime.ToString("d");

1.13 取当前时分,格式为:1424 string strT=currentTime.ToString("t");

五、就像腾讯qq一样 点击winform上 按钮自动跳转到浏览器页面(linkLabel):

linkLabel1.LinkVisited = true;

System.Diagnostics.Process.Start("http://www.baidu.com");

抱歉!评论已关闭.