0

我从我的项目中创建了 Windows 应用程序,我现在面临一个问题。问题是:我已经有一个文本框数组,并且想要连接这些数据库文本框,但是,我无法获取数据,因为我想要连接的数据是 int 数据类型。

我已经创建了 2 个表单,一个用于登录表单,用于登录表单,数据库是字符串,所以自动完成功能正在工作。第二个是Second Form,对于Second Form,数据库是int,所以自动完成不起作用。

我的问题是:如何将 int 数据类型转换为字符串数据类型?

这是我的第二种形式的代码:

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);
        }
    }
}
4

3 回答 3

1

我的问题是:如何将 int 数据类型转换为字符串数据类型?

string s = Convert.ToString(yourIntvalue);
于 2013-07-31T12:43:24.167 回答
0

利用

 dReader.GetString("Code");

所以

while (dReader.Read())
{
    codesCollection.Add(dReader.GetString("Code"));
}

OleDbDataReader.GetString

以字符串形式获取指定列的值。

于 2013-07-31T12:46:50.513 回答
0

如果您要在所有代码中使用 CODE 作为字符串,为什么不从查询中更改它?

“SELECT DISTINCT CStr([Code]) FROM [Data] ORDER BY [Code] ASC”

ACCESS 支持一些 VBscript,因此 CStr 可以解决问题。Donig 这样,DBReader 会将 CODE 视为字符串

于 2014-11-03T17:49:03.683 回答