C# 将我的数据从access 2003 db获取到c时出现问题(dbReader.GetString错误)
[更进一步,所以更新了] 你好,我真的希望你能帮助我 现在,我的代码的第一部分开始工作了,我在我的组合框中得到了我的报告编号,并且我能够将该编号写入lbl。现在,我需要从Access 2003数据库中获取该数字,并将其放入输出中的字符串中。我真的不想在打开程序时将所有数据加载到我的mem中,因此我相信在我选择一个之前,我只会获得[Rapport nr],我会将数据加载到字符串中并暂时保存在那里: 我的问题是这行不通C# 将我的数据从access 2003 db获取到c时出现问题(dbReader.GetString错误),c#,.net,ms-access,oledb,C#,.net,Ms Access,Oledb,[更进一步,所以更新了] 你好,我真的希望你能帮助我 现在,我的代码的第一部分开始工作了,我在我的组合框中得到了我的报告编号,并且我能够将该编号写入lbl。现在,我需要从Access 2003数据库中获取该数字,并将其放入输出中的字符串中。我真的不想在打开程序时将所有数据加载到我的mem中,因此我相信在我选择一个之前,我只会获得[Rapport nr],我会将数据加载到字符串中并暂时保存在那里: 我的问题是这行不通 output = dbReader.GetString(dbReader.Get
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)
{
}
}
}
我发现了这个问题:D在休息后,我回到这个问题上,试着看看是否还有其他方法可以使用,还有:p我发现这个错误与类型有关,所以我没有取出一个并试图解决这个问题,而是通过在dbReader我让它工作起来了,所以我现在可以继续:D给那些可能感兴趣的人!这是我的代码:P它不漂亮但有效:P我allso接受了som try catch,只是为了确保我检查错误并得到友好的响应:
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!";
}
}
}
}
我自己刚刚看到了,所以我更新了我的帖子,并得到了一些进一步的信息:P无论如何,谢谢:POBS:我的错误是,它说我的行或列中没有任何数据。好的:P现在完成了:D谢谢你的帮助:P我想我的问题不在于数据库,尽管我在代码的第一部分得到了我的数字,但不是第二个语句中的其他数据?该语句按应有的方式返回,我只是在access中对其进行了测试。。我将查看是否可以从vs中的调试器获取更多信息:/