C# 将int数据类型转换为字符串数据类型c access数据库
我从我的项目中创建了Windows应用程序,现在我面临一个问题。 问题是:我已经有一个文本框数组,并且想要连接那些数据库文本框,但是,我无法获取数据,因为我想要连接的数据是int数据类型 我已经创建了两个表单,一个用于登录表单,对于登录表单,数据库是字符串,因此自动完成正在工作。第二个是第二个表单,对于第二个表单,数据库是int,因此自动完成不起作用 我的问题是:如何将int数据类型转换为string数据类型 这是我第二张表格的代码:C# 将int数据类型转换为字符串数据类型c access数据库,c#,database,winforms,C#,Database,Winforms,我从我的项目中创建了Windows应用程序,现在我面临一个问题。 问题是:我已经有一个文本框数组,并且想要连接那些数据库文本框,但是,我无法获取数据,因为我想要连接的数据是int数据类型 我已经创建了两个表单,一个用于登录表单,对于登录表单,数据库是字符串,因此自动完成正在工作。第二个是第二个表单,对于第二个表单,数据库是int,因此自动完成不起作用 我的问题是:如何将int数据类型转换为string数据类型 这是我第二张表格的代码: string connectionString = (@"P
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;");
private List<List<TextBox>> textBoxCodeContainer = new List<List<TextBox>>();
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())
{
codesCollection.Add(Convert.ToString(dReader.GetInt32(dReader.GetOrdinal("Code")))); // This line is not working
}
//****TextBox for Code****
for (int y = 0; y <= 16; y++)
{
textBoxCodeContainer.Add(new List<TextBox>());
textBoxCodeContainer[0].Add(new TextBox());
textBoxCodeContainer[0][y].Size = new Size(100, 50);
textBoxCodeContainer[0][y].Location = new Point(25, 150 + (y * 25));
textBoxCodeContainer[0][y].AutoCompleteMode = AutoCompleteMode.Suggest;
textBoxCodeContainer[0][y].AutoCompleteSource = AutoCompleteSource.CustomSource;
textBoxCodeContainer[0][y].AutoCompleteCustomSource = codesCollection;
dReader.Close();
conn.Close();
Controls.Add(textBoxCodeContainer[0][y]);
}
编辑:代码的数据类型为数字,字段大小为长整数,格式为0000。。后面的零替换为数字1、2等
编辑:
*完整代码****
我的问题是:如何将int数据类型转换为string数据类型
使用
所以
以字符串形式获取指定列的值
如果要在所有代码中使用代码作为字符串,为什么不从查询中更改它 由[Code]ASC从[Data]订单中选择不同的CStr[Code]
ACCESS支持一些VBscript,因此CStr可以实现这一点。如果这样做,DBReader会将代码视为字符串我简直不敢相信,在这里发布问题之前,你会花一秒钟的时间搜索这个问题……你说“codesCollection.AddConvert.ToStringReader.GetInt32dReader.GetOrdinalCode;”它不起作用。假设代码字段是整数,而不是null@Steve:我的意思是,不工作是int数据类型的autocomplete不工作,跟踪时的代码给了我autos,所以这意味着没有错误,对吗?AutoCompleteStringCollection需要字符串而不是int。所以我还是不明白你的问题在哪里。如果您开始在文本框中键入数字,则自动完成功能是否有效?否,当我在文本框中键入数字时,自动完成功能不起作用,但当我将代码更改为:OleDbCommand cmd=new OleDbCommand从[Member]ORDER BY[Username]ASC,conn;我已经尝试过该代码,但它不起作用,我的值在数据库中,所以我如何填写yourIntValue为什么不填写yourIntValue.ToString?codesCollection.AddConvert.ToString.GetOrdinalCode@EhsanUllah:不,在我在这个论坛上发布之前,我已经试过了你给我的代码,但它不起作用either@FuhansPujiSaputradReader.GetOrdinalCode返回什么?它给了我几个错误,它们是:无法将字符串类型转换为int类型,并且dbdatareader.getstringint的最佳重载方法有一些无效参数
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Principal;
using System.Data.OleDb;
namespace Sell_System
{
public partial class Form2 : Form
{
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;");
private Form1 firstForm;
private List<List<TextBox>> textBoxCodeContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxQuantityContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxDescContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxSubTotalContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxTotalContainer = new List<List<TextBox>>();
public Form2()
{
InitializeComponent();
}
public Form2(Form1 firstForm)
: this()
{
this.firstForm = firstForm;
}
private void Form2_Load(object sender, EventArgs e)
{
//var int num = 10;
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())
{
codesCollection.Add(Convert.ToString(dReader.GetInt32(dReader.GetOrdinal("Code"))));
//codesCollection.Add(Convert.ToString(num);
}
if (firstForm.comboBox1.SelectedIndex == 0)
{
label1.Text = "Code:";
label1.Location = new Point(60, 125);
label2.Text = "Welcome to the Selling System.";
label2.Location = new Point(600, 0);
label3.Text = "Quantity:";
label3.Location = new Point(155, 125);
label4.Text = "Description:";
label4.Location = new Point(580, 125);
label5.Text = "Sub Total on Rp:";
label5.Location = new Point(1020, 125);
label6.Text = "Total on Rp:";
label6.Location = new Point(1210, 125);
label7.Text = "Total on Rp:";
label7.Location = new Point(1080, 580);
}
else if (firstForm.comboBox1.SelectedIndex == 1)
{
label1.Text = "Kode:";
label1.Location = new Point(60, 125);
label2.Text = "Selamat datang di Selling System.";
label2.Location = new Point(600, 0);
label3.Text = "Banyaknya:";
label3.Location = new Point(145, 125);
label4.Text = "Keterangan:";
label4.Location = new Point(580, 125);
label5.Text = "Sub Total di Rp:";
label5.Location = new Point(1020, 125);
label6.Text = "Total di Rp:";
label6.Location = new Point(1210, 125);
label7.Text = "Total di Rp:";
label7.Location = new Point(1080, 580);
}
//****TextBox for Code****
for (int y = 0; y <= 16; y++)
{
textBoxCodeContainer.Add(new List<TextBox>());
textBoxCodeContainer[0].Add(new TextBox());
textBoxCodeContainer[0][y].Size = new Size(100, 50);
textBoxCodeContainer[0][y].Location = new Point(25, 150 + (y * 25));
textBoxCodeContainer[0][y].AutoCompleteMode = AutoCompleteMode.Suggest;
textBoxCodeContainer[0][y].AutoCompleteSource = AutoCompleteSource.CustomSource;
textBoxCodeContainer[0][y].AutoCompleteCustomSource = codesCollection;
dReader.Close();
conn.Close();
Controls.Add(textBoxCodeContainer[0][y]);
}
//****TextBox for Quantity****
for (int y = 0; y <= 16; y++)
{
textBoxQuantityContainer.Add(new List<TextBox>());
textBoxQuantityContainer[0].Add(new TextBox());
textBoxQuantityContainer[0][y].Size = new Size(100, 50);
textBoxQuantityContainer[0][y].Location = new Point(125, 150 + (y * 25));
Controls.Add(textBoxQuantityContainer[0][y]);
}
//****TextBox for Description****
for (int y = 0; y <= 16; y++)
{
textBoxDescContainer.Add(new List<TextBox>());
textBoxDescContainer[0].Add(new TextBox());
textBoxDescContainer[0][y].Size = new Size(750, 50);
textBoxDescContainer[0][y].Location = new Point(225, 150 + (y * 25));
Controls.Add(textBoxDescContainer[0][y]);
}
//****TextBox for Sub Total****
for (int y = 0; y <= 16; y++)
{
textBoxSubTotalContainer.Add(new List<TextBox>());
textBoxSubTotalContainer[0].Add(new TextBox());
textBoxSubTotalContainer[0][y].Size = new Size(175, 50);
textBoxSubTotalContainer[0][y].Location = new Point(975, 150 + (y * 25));
Controls.Add(textBoxSubTotalContainer[0][y]);
}
//****TextBox for Total****
for (int y = 0; y <= 16; y++)
{
textBoxTotalContainer.Add(new List<TextBox>());
textBoxTotalContainer[0].Add(new TextBox());
textBoxTotalContainer[0][y].Size = new Size(175, 50);
textBoxTotalContainer[0][y].Location = new Point(1150, 150 + (y * 25));
Controls.Add(textBoxTotalContainer[0][y]);
}
//****TextBox for Total All****
TextBox textBoxAllTotal = new TextBox();
textBoxAllTotal.Size = new Size(175, 50);
textBoxAllTotal.Location = new Point(1150, 575);
Controls.Add(textBoxAllTotal);
}
}
}
string s = Convert.ToString(yourIntvalue);
dReader.GetString("Code");
while (dReader.Read())
{
codesCollection.Add(dReader.GetString("Code"));
}