将int数据类型转换为字符串数据类型c#访问数据库
本文关键字:数据类型 访问 数据库 字符串 int 数据 类型转换 | 更新日期: 2023-09-27 18:25:43
我从我的项目中创建了Windows应用程序,现在我面临一个问题。问题是:我已经有了一个文本框数组,并且想要连接那些数据库文本框,但是,我无法获得数据,因为我想要连接的数据是int数据类型。
我已经创建了两个表单,一个用于登录表单,对于登录表单,数据库是字符串,因此自动完成正在工作。第二个是second Form,对于second Form来说,数据库是int,所以自动完成不起作用。
我的问题是:如何将int数据类型转换为string数据类型?
这是我的第二表格代码:
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"等。
编辑:*完整代码***
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);
}
}
}
我的问题是:如何将int数据类型转换为string数据类型?
string s = Convert.ToString(yourIntvalue);
使用
dReader.GetString("Code");
所以
while (dReader.Read())
{
codesCollection.Add(dReader.GetString("Code"));
}
OleDbDataReader.GetString
以字符串形式获取指定列的值。
如果您要在所有代码中使用CODE作为字符串,为什么不从查询中更改它呢?
"通过[代码]ASC从[数据]订单中选择DISTINCT CStr([代码])"
ACCESS支持一些VBscript,所以CStr可以做到这一点。通过这种方式,DBReader将CODE视为字符串