如何简单地每天生成id与系统日期? [英] how to simple generate id day by day with system date ?

查看:113
本文介绍了如何简单地每天生成id与系统日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一天的代码,

code for one day,

int i;
string str;
SqlConnection cn = null;
protected void Page_Load(object sender, EventArgs e)
   {
         cn = new 
     SqlConnection(ConfigurationManager.ConnectionStrings["concms"].ConnectionString);
        cn.Open();
        txtdate.Text = Convert.ToDateTime(date).ToString("dd-MM-yyyy");
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter("select max(substring(complainid,10,3))
                  from newcomplainuser where dates =''" + txtdate.Text.ToString()+"''",
                  cn);
        da.Fill(dt);
        str = da.SelectCommand.ExecuteScalar().ToString() ;
        i = Convert.ToInt32(str) + 1 ;
        txtcomplain.Text = Convert.ToDateTime(date).ToString("ddMMyyyy" + "-" + i );
        cn.close();
}


输出:
27032012-1
27032012-2
27032012-3 ........ 27032012-n

现在,我该如何生成第二天... ??

第二天的输出将是28032012-1、28032012-2 ..依此类推..

请告诉您您的建议...


output:
27032012-1
27032012-2
27032012-3........ 27032012-n

now, how can i generate for second day ...??

second day output will be 28032012-1, 28032012-2.. so on..

please tell your suggestion...

推荐答案

不是直接的解决方案,但有一些技巧可以提高您的技能.

首先, 从不 接受未经验证的用户输入并连接一个SQL Commmand. EVER .对SQL Injection攻击进行一些研究,您将了解会发生什么不良情况.您应该至少使用参数化查询.

利用使用块.

Not a direct solution but some tips to improve your skills.

First, NEVER accept unvalidated user input and concatenate a SQL Commmand. EVER. Do some research on SQL Injection attacks and you will understand what bad things will happen. You should be using parameterized queries at a minimum.

Make use of using blocks.

using(SqlConnection conn = new SqlConnection(...))
{
   
}



这没道理



This makes no sense

txtdate.Text = Convert.ToDateTime(date).ToString("dd-MM-yyyy");


您正在将某个字符串转换为日期,然后再转换回字符串,仅是将其分配给文本框.然后,在SqlCommand中使用该文本框值.直接使用字符串.


You are converting some string to a date then back to a string only to assign it to a textbox. Then later using that textbox value in the SqlCommand. Use the string directly.

txtdate.Text.ToString()


text属性已经是一个字符串.您不需要在字符串上使用ToString.

请勿将其放在Page_Load事件中.


The text property is already a string. You don''t need to use ToString on a string.

Don''t put this in the Page_Load event.


"BobJanova"和"kishore Rajendran"已经提出了正确的方向.

虽然如果您想第二天,代码片段将是,
date.AddDays(1);
''BobJanova'' and ''kishore Rajendran'' already put the right direction.

Though if you want to get the next day, the code snippets would be,
date.AddDays(1);


这篇关于如何简单地每天生成id与系统日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆