`
lingyibin
  • 浏览: 191796 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

做项目时遇到的几个关于C#和SQL的细节问题(一)

    博客分类:
  • .Net
阅读更多

 

1、把一个字符串表示的浮点数四舍五入成int型:

(int)(Double.Parse("33.533")+0.5)

也可以:(int)Math.Round(Double.Parse("33.533")

2、SQLServer数据库中对decimal data进行大小的比较时,用data < 33.533这种方式好像行不通,可以用如下几个方法:

data<(decimal)33.533或者data<Convert.ToDecimal(33.533)

3、C#获得系统当前时间:

DateTime.Now.ToString();//获取当前系统时间 完整的日期和时间

string strDate=currentTime.ToString("d"); //2010-10-16

DateTime.Now.ToLongDateString();//显示 xxxx年 xx月xx日

DateTime.Now.ToShortDateString();//显示 xxxx-xx-xx

string strTime=currentTime.ToString("t"); //12:00

DateTime.Now.AddDays(-1).ToShortDateString(); //昨天的日期

DateTime.Now.AddDays(1).ToShortDateString(); //明天的日期

(具体的可以去查看我转载的一篇文章:http://blog.csdn.net/lingyb011/archive/2010/10/17/5946351.aspx

4、把自己常用的方法封装起来,以后做其它项目时候可以用。

5、如下图,只想查询其中某几个空时,可以这样构造条件where:


 string where = "where ";

        bool flag = false;
        string upTime = txtUpTime.Value.Trim();
        if (upTime != "") { where += "DATEDIFF(dd, upTime, '" + upTime + "') =0 "; flag = true; }
        if (txtFileName.Text != "") { where += flag ? " and" : ""; where += " filePath like '%" + txtFileName.Text + "%'"; flag = true; }
        if (txtFileSize.Text != "") 
        { 
            int size = 0;
            try
            {
                size = Int32.Parse(txtFileSize.Text);
                where += flag ? " and" : "";
                where += " fileSize < " + size;
            }
            catch { }
            flag = true;
        }
        if (txtUploader.Text != "") { where += flag ? " and" : ""; where += " uploader=" + txtUploader.Text; flag = true; }
        if (txtDep.Text != "") { where += flag ? " and" : ""; where += " bmbh=" + txtDep.Text; flag = true; }
 

 

  • 大小: 3.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics