C#访问数据库

本文关键字:数据库 访问 | 更新日期: 2023-09-27 17:58:33

我正在尝试连接到我们的数据库,并创建一个从表中读取信息并将信息带到文本框/标签中的程序。我收到一个错误:

int numberOfRowsFeched = oleDbDataAdapter1.Fill(dataSet1, "Böcker");

错误:InvalidOperationException

private void btn1_Click(object sender, EventArgs e)
    {
        {
            string sqlQuery = "SELECT Fält2, Fält3 FROM Böcker WHERE Fält2 = " + "'''" + txt1.Text + "'''";
            oleDbDataAdapter1.SelectCommand.CommandText = sqlQuery;
            dataSet1.Clear();
                oleDbConnection1.Open();
            int numberOfRowsFeched = oleDbDataAdapter1.Fill(dataSet1, "Böcker");
            if (numberOfRowsFeched > 0)
            {
                DataTable dt = dataSet1.Tables["Böcker"];
                lbl1.Text = dt.Rows[0][0].ToString() + " " +
                dt.Rows[0][1].ToString();
                lbl2.Text = dt.Rows[0][2].ToString();
            }
            else
            {
                lbl1.Text = "Boken hittades inte.";
                lbl1.Text = "";
            }
        }
    }
}

我正在使用的新代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.OleDb;
using System.Data.SqlClient;
namespace MySQL
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void btn1_Click(object sender, EventArgs e)
        {
            string ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:''Users''cetin.cigel''databasbibliotek.mdb;User Id=admin;Password=;";
            string SqlString = "SELECT Fält1, Fält2, Fält3 FROM Böcker WHERE Fält2 = ?";
            using (OleDbConnection conn = new OleDbConnection(ConnString))
            {
                using (OleDbDataAdapter da = new OleDbDataAdapter(SqlString, conn))
                {
                    da.SelectCommand.Parameters.AddWithValue("Fält2", txt1.Text);
                    DataTable dt = dataSet1.Tables["Böcker"];
                    da.Fill(dt);
                    if (dt.Rows.Count > 0)
                    {
                        string text = string.Format("{0} {1}",
                            dt.Rows[0].Field<string>(0),
                            dt.Rows[0].Field<string>(1));
                        lbl1.Text = text;
                        lbl2.Text = dt.Rows[0].Field<string>(2);
                    }
                    else
                    {
                        lbl1.Text = "Boken hittades inte.";
                    }
                }
            }
        }
    }
}

C#访问数据库

尝试更改表名,这可能是编码问题。此外,您可能会发现这很有用:http://www.alinq.org/

请发布所有异常,以便人们可以提供帮助:)

我假设您正在尝试打开一个仍然打开的连接。

OleDbConnection.Open方法

InvalidOperationException:连接已打开。

  • 使用using-语句确保所有内容都能尽快得到处理/关闭。您没有关闭连接,这在使用连接池时是个坏主意(默认)
  • 您试图访问三个字段,但在查询中只选择了两列
  • 不要连接sql查询,而是使用参数:

string SqlString = "SELECT Fält1, Fält2, Fält3 FROM Böcker WHERE Fält2 = ?";
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
    using (OleDbDataAdapter da = new OleDbDataAdapter(SqlString, conn))
    {
        da.SelectCommand.Parameters.AddWithValue("Fält2", txt1.Text);
        DataTable dt = dataSet1.Tables["Böcker"];
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            string text = string.Format("{0} {1}",
                dt.Rows[0].Field<string>(0),
                dt.Rows[0].Field<string>(1));
            lbl1.Text = text;
            lbl2.Text = dt.Rows[0].Field<string>(2);
        }
        else
        {
            lbl1.Text = "Boken hittades inte.";
        }
    }
}