本文介绍了将int转换为string并在c#windows应用程序中读取它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好,我是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(); 谢谢解决方案 希望这是你所期望的 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 databaseHere 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 expectstring number="";while (dReader.Read()){ number ="000"+ dReader["Code"].ToString();}Hope this helpsThis code:string numString = "000" + dReader["Code"].ToString();If the value is 2, you will get 0002.But if the value is 200, you will get 000200, which I think is not what you want.If the value is 200, the outcome should be 0200.You can use PadLeft() to solve this:while (dReader.Read()){ string number = dReader["Code"].ToString().PadLeft(4, '0'));}Thanks guys, i already solved this myselfThis is the code:<pre lang="c#">string numString = "000" + dReader["Code"].ToString(); codesCollection.Add(numString); 这篇关于将int转换为string并在c#windows应用程序中读取它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-22 09:10