将int转换为string并在c#windows应用程序中读取它 [英] Convert int to string and read it in c# windows application
问题描述
大家好,我是c#的新手,想问:
i已经有一个数据库,里面有数字,想在我的表单上显示作为字符串。如何从int到string调用该数字的值?当用户键入0这是数字的开头时,它会根据我的数据库显示从0开始的数字
这是我的到目前为止的代码:
string connectionString =( @ Provider = Microsoft.ACE.OLEDB.12.0; Data Source = \Archives\Projects\Program\Sell System \ Sell System\App_Data\db1.accdb; Persist Security Info = False;);
OleDbDataReader dReader;
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
OleDbCommand cmd = new OleDbCommand( SELECT DISTINCT [代码] FROM [数据] ORDER BY [代码] ASC,conn);
dReader = cmd.ExecuteReader();
AutoCompleteStringCollection codesCollection = new AutoCompleteStringCollection();
while (dReader.Read())
{
/ / 我必须填写什么?
}
dReader.Close();
conn.Close();
谢谢
< blockquote>希望这是你所期望的
string number = ;
while (dReader.Read())
{
number = 000 + dReader [ 代码跨度>]的ToString();
}
希望这有帮助
此代码:
string numString = 000 + dReader [ 代码]。ToString();
如果值2
,您将获得0002
。
但如果该值200
,您将获得000200
,我认为这不是您想要的。
如果值200
,结果应为0200
。
您可以使用PadLeft()
来解决此问题:
while (dReader.Read())
{
string number = dReader [ 代码]。ToString()。PadLeft( 4 ,'跨度> <跨度类= 代码串> 0' 跨度>));
}
谢谢大家,我已经自己解决了这个问题
这是代码:
< pre lang =c#> string numString =000+ dReader [Code]。ToString();
codesCollection.Add(numString);
Hi guys, i am new in c# and want to ask:
i already had a database that filled up with numbers, and want to show it on my form as string. How do i call the value of that numbers from int to string? When user type "0" which is start of the number, it will display to the screen the numbers that start from "0" based on my database
Here is my code so far:
string connectionString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\Archives\Projects\Program\Sell System\Sell System\App_Data\db1.accdb;Persist Security Info=False;");
OleDbDataReader dReader;
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT DISTINCT [Code] FROM [Data] ORDER BY [Code] ASC", conn);
dReader = cmd.ExecuteReader();
AutoCompleteStringCollection codesCollection = new AutoCompleteStringCollection();
while (dReader.Read())
{
// I must fill this with what?
}
dReader.Close();
conn.Close();
Thank you
Hope this are what you expect
string number=""; while (dReader.Read()) { number ="000"+ dReader["Code"].ToString(); }
Hope this helps
This code:
string numString = "000" + dReader["Code"].ToString();
If the value is2
, you will get0002
.
But if the value is200
, you will get000200
, which I think is not what you want.
If the value is200
, the outcome should be0200
.
You can usePadLeft()
to solve this:
while (dReader.Read()) { string number = dReader["Code"].ToString().PadLeft(4, '0')); }
Thanks guys, i already solved this myself
This is the code:
<pre lang="c#">string numString = "000" + dReader["Code"].ToString(); codesCollection.Add(numString);
这篇关于将int转换为string并在c#windows应用程序中读取它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!