将int数据类型转换为字符串数据类型c#access database

时间:2013-07-31 12:40:17

标签: c# database winforms

我在我的项目中创建了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);
        }
    }
}

3 个答案:

答案 0 :(得分:1)

  

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

string s = Convert.ToString(yourIntvalue);

答案 1 :(得分:0)

使用

 dReader.GetString("Code");

所以

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

OleDbDataReader.GetString

  

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

答案 2 :(得分:0)

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

&#34; SELECT DISTINCT CStr([Code])FROM [Data] ORDER BY [Code] ASC&#34;

ACCESS支持一些VBscript,因此CStr可以做到这一点。这样,DBReader会将CODE视为字符串