简体   繁体   English

使用 int64 值格式化字符串

[英]format string with int64 value

i have a web service in which training number is assigning as per my database我有一个网络服务,其中根据我的数据库分配了培训编号

there is a line有一条线

dates + "1"; this lines add 1 in date此行在日期中加 1

like 27012017 will become 270120171比如27012017会变成270120171

then i convert this into int64然后我将其转换为int64

newid = Convert.ToInt64(dates);

but now i want to add trainer id in this但现在我想在此添加培训师 ID

so i updated my line with this所以我用这个更新了我的线路

dates ="00"+trainerid +"-"+ dates + "1";
newid = Convert.ToInt64(dates);

the error is coming input string is not in correct format,错误即将到来 输入字符串的格式不正确,

i know this is because of the addition of +"-"+我知道这是因为添加了+"-"+

but i want to store the data in this format only但我只想以这种格式存储数据

and my whole portion look like我的整个部分看起来像

DateTime dt = DateTime.Now;
string dates = dt.ToString();
dates = dates.Replace("-", "");
dates = dates.Substring(0, 8);
        SqlCommand cmdbill = new SqlCommand("select top 1 * from listmybill where bill_id like @trid order by bill_id desc", con);
        con.Open();
        cmdbill.Parameters.AddWithValue("@trid", "%" + dates + "%");
        SqlDataReader dr = cmdbill.ExecuteReader();
        while (dr.Read())
        {
            value = dr["bill_id"].ToString();
        }

        con.Close();
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmdbill);
        DataTable dat = new DataTable();
        da.Fill(dat);
        if (dat.Rows.Count > 0)
        {
            newid = Convert.ToInt64(value);
            newid = newid + 1;
        }
        else
        {
            dates ="00"+trainerid +"-"+ dates + "1";
            newid = Convert.ToInt64(dates);
        }
        con.Close();

what should i do here,我应该在这里做什么,

i want to enter the data like 001-270120171 and if i convert toInt64 into string,我想输入001-270120171之类的数据,如果我将 toInt64 转换为字符串,

there can be problem when a row found in the table在表中找到一行时可能会出现问题

if (dat.Rows.Count > 0) 
{
    newid = Convert.ToInt64(value);
    newid = newid + 1; 
}

what i need to do now?我现在需要做什么?

i want to store this into my database我想将它存储到我的数据库中

I hardly think you will be doing mathematical calculations on this training number string.我几乎不认为您会对这个训练数字字符串进行数学计算。 I suggest that you convert the int in your database into a varchar using alter statement.我建议您使用alter语句将数据库中的int转换为varchar Then you you will be able to store the training number in whichever format you like.然后,您将能够以您喜欢的任何格式存储培训编号。

You can change new id to string and do the following您可以将新 ID 更改为字符串并执行以下操作

if (dat.Rows.Count > 0) 
{
    newid = Convert.ToString(Convert.ToInt64(value)+1);
}
 else
{
   newid="00"+trainerid +"-"+ dates + "1";

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM