将我的数据从访问2003 db获取到c#(dbReader.GetString错误)的问题

时间:2010-10-24 15:10:13

标签: c# .net ms-access oledb

[进一步了解更新]

您好,我真的希望您能帮助我!

现在我的代码的第一部分确实有用了,我确实在我的组合框中输出了我的报告编号,并且我能够将该编号写入lbl。现在我需要获取该数字并从Access 2003数据库中获取剩余的数据,并将它们放入一个字符串(我的输出)中。 (当我打开程序时,我真的不希望所有数据都加载到我的mem中,所以我相信只有[Rapport nr]直到我选择一个,我将把数据加载到字符串中并保存在那里): )

我的问题是,这不会工作!

output = dbReader.GetString(dbReader.GetOrdinal("Dato")).ToString();

OBS:我的错误现在是说我的行或颜色中没有任何数据

我的代码如下:

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.Collections;
using System.Data.OleDb;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private string aktuelRapportNR = "";
        string output = "";

        private string connectionName = "Provider=Microsoft.Jet.OLEDB.4.0;"
            + "Data Source=semcqdjh-access 2007.mdb;"
            + "Jet OLEDB:Database Password=;";




        public Form1()
        {
            InitializeComponent();
            #region Indlæsning af combobox, med rapport numre
            OleDbConnection Myconnection = null;
            OleDbDataReader dbReader = null;

            Myconnection = new OleDbConnection(connectionName);
            Myconnection.Open();

            OleDbCommand cmd = Myconnection.CreateCommand();
            cmd.CommandText = "SELECT [Rapport nr] FROM AVR";
            dbReader = cmd.ExecuteReader();

            int rapportNR;
            while (dbReader.Read())
            {
                rapportNR = (int)dbReader.GetInt32(dbReader.GetOrdinal("Rapport nr"));

                comboBox1.Items.Add(rapportNR);

            }

            dbReader.Close();
            Myconnection.Close();


#endregion
        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            aktuelRapportNR = comboBox1.SelectedItem.ToString();
            lblAktuelRapportNR.Text = aktuelRapportNR;

            OleDbConnection Myconnection = null;
            OleDbDataReader dbReader = null;

            Myconnection = new OleDbConnection(connectionName);
            Myconnection.Open();

            OleDbCommand cmd = Myconnection.CreateCommand();
            cmd.CommandText = "SELECT [Dato] FROM AVR WHERE [Rapport nr] =" + aktuelRapportNR;
            dbReader = cmd.ExecuteReader();

            try
            {
                output = dbReader.GetString(dbReader.GetOrdinal("Dato")).ToString();

            }
            catch (Exception)
            {
                output = "fejl eller tom";
            } 
            dbReader.Close();
            Myconnection.Close();

            label1.Text = output;
        }

        private void btnExport_Click(object sender, EventArgs e)
        {

        }


    }
}

1 个答案:

答案 0 :(得分:0)

我想出了这一点:D休息后我回到了这里,并试图看看是否有我可以使用的其他方法,并且有:P我错误地认为这是关于类型如此绝对的类型取出一个并试图解决这个问题,我把孔排出来并将它放在一个对象数组中:P通过在dbReader中使用GetValues让它工作,所以我现在可以继续前进:D对于那些可能感兴趣的人!这是我的代码:P它不漂亮,但它的工作原理:P我也接受了som尝试捕获只是为了确保我检查错误并获得友好的响应:)

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.Collections;
using System.Data.OleDb;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private string aktuelRapportNR = "";
        string output;

        private string connectionName = "Provider=Microsoft.Jet.OLEDB.4.0;"
            + "Data Source=semcqdjh-access 2007.mdb;"
            + "Jet OLEDB:Database Password=;";




        public Form1()
        {
            InitializeComponent();
            #region Indlæsning af combobox, med rapport numre
            try
            {

                OleDbConnection Myconnection = null;
                OleDbDataReader dbReader = null;

                Myconnection = new OleDbConnection(connectionName);
                Myconnection.Open();

                OleDbCommand cmd = Myconnection.CreateCommand();
                cmd.CommandText = "SELECT [Rapport nr] FROM AVR";
                dbReader = cmd.ExecuteReader();

                int rapportNR;
                while (dbReader.Read())
                {
                    rapportNR = (int)dbReader.GetInt32(dbReader.GetOrdinal("Rapport nr"));

                    comboBox1.Items.Add(rapportNR);

                }

                dbReader.Close();
                Myconnection.Close();
                txtStatus.Text = "Indlæsning Fuldført! Vælg venligst en rapport";
            }
            catch (Exception)
            {

                txtStatus.Text = "Indlæsning Fejlet!";
            }

#endregion
        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            aktuelRapportNR = comboBox1.SelectedItem.ToString();
            lblAktuelRapportNR.Text = aktuelRapportNR;

            txtStatus.Text = "Du har valgt rapport nr.: " + aktuelRapportNR + "! Klik på export";
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            try
            {

                OleDbConnection Myconnection = null;
                OleDbDataReader dbReader = null;

                Myconnection = new OleDbConnection(connectionName);
                Myconnection.Open();

                OleDbCommand cmd = Myconnection.CreateCommand();
                cmd.CommandText = "SELECT * FROM AVR WHERE [Rapport nr] =" + aktuelRapportNR;
                dbReader = cmd.ExecuteReader();

                object[] liste = new object[dbReader.FieldCount];
                if (dbReader.Read() == true)
                {

                    int NumberOfColums = dbReader.GetValues(liste);

                    for (int i = 0; i < NumberOfColums; i++)
                    {
                        output += "|" + liste[i].ToString();
                    }

                }

                dbReader.Close();
                Myconnection.Close();
                txtStatus.Text = "Export Lykkes! Luk programmet!";
            }
            catch (Exception)
            {

                txtStatus.Text = "Export Fejlet!";
            }


        }


    }
}