在数据库中将字符串转换为int [英] Converting string into int in database
本文介绍了在数据库中将字符串转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ASPX.CS代码
int[] no = new int[100];
int i = 0;
DataTable dt = bl.BL_AutogerateKey(txt_req_form_id.Text);
if (dt.Rows.Count == 0)
{
txt_req_form_id.Text = "1000001";
}
else
{
foreach (DataRow dr in dt.Rows)
{
no[i] = Convert.ToInt32(dr["FORM_ID"].ToString());
i++;
}
int auto = no.Max();
txt_req_form_id.Text = "100000" + Convert.ToString(auto + 1);
DataLayer
DataLayer
String str = "select substring(FORM_ID,5,50) as FORM_ID from PREV_REQUEST_TO_RECRUIT";
SqlDataAdapter da = new SqlDataAdapter(str, con);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
return dt;
我正在做自动生成form_id。但值100000在DB中存储为字符串,而我希望将其存储为int。我应该在C#代码中进行哪些更改以将其更改为整数?
I am doing auto generation of form_id. but the value "100000" is storing as string in DB whereas I want this to be stored as int. what alteration should I do in C# code to change it as integer?
推荐答案
您应该更改数据库存储类型 - 当它被发送到SQL时,它不在您的应用程序手中处理 - 字段类型似乎是char类型而不是数据库中的int(我依赖于你对问题的描述)。
SQL将然后将对象作为int返回。
ASIDE:如果存储需要,SQL有能力将int作为字符串发送 - 如果它们是可转换的。
You should change the database storage type - it's out of your applications hands when it's sent to SQL's processing - and the field type would seem to be a char type instead of an int in the database (I am relying upon your description of the problem).
SQL will then return the object as an int.
ASIDE: SQL has the ability to promote int's sent in as strings if required for storage - if they're convertible.
替换
Replace
txt_req_form_id.Text = "100000" + Convert.ToString(auto + 1);
with
with
txt_req_form_id.Text = Convert.ToString((100000 + auto + 1));
将DB表列中的列类型更改为int
Change the column type in DB table column as int
这篇关于在数据库中将字符串转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文