如何从C#中的Access 2010数据库填充文本框?

如何从C#中的Access 2010数据库填充文本框?,c#,ms-access,textbox,C#,Ms Access,Textbox,我正在尝试从MS Access 2010数据库表填充文本框。这张桌子有两块地,夜和座位。我想将Seats字段中的当前值放入文本框中,假设在Night字段中选择的日期有一个当前值。我现在掌握的代码是: //connect to the database to get how many seats are available System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnect

我正在尝试从MS Access 2010数据库表填充文本框。这张桌子有两块地,夜和座位。我想将Seats字段中的当前值放入文本框中,假设在Night字段中选择的日期有一个当前值。我现在掌握的代码是:

       //connect to the database to get how many seats are available
       System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection();
       con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Duncan\Documents\Visual Studio 2012\Projects\TheatreUI\TheatreUI\bin\Debug\PlayHouse.accdb";
       OleDbCommand cmd = con.CreateCommand();

        //open the connection
        con.Open();

        // read from the Nights Table in the database
        cmd.CommandText = "SELECT Seats FROM Nights WHERE Night = '" + System.DateTime.Now.ToShortDateString() + "';";
        MessageBox.Show(System.DateTime.Now.ToShortDateString());
        OleDbDataReader reader = cmd.ExecuteReader();
        MessageBox.Show(reader["Seats"].ToString());
        SeatsText.Text = reader["Seats"].ToString();

        //close the connection
        con.Close();
这段代码不仅没有正确地(或根本没有)填充文本字段,而且似乎完全从数据库中删除了今天日期的记录。第一个消息框显示正确的日期,但第二个消息框显示空白。 如何修复此代码,使其填充文本框而不删除数据库中的条目

public List<DataTable> GetWithQuery(string query)
{
    DataTable dataTable = new DataTable();

    using (OleDbConnection source = new
        OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
        Source=C:\Users\Duncan\Documents\Visual Studio
        2012\Projects\TheatreUI\TheatreUI\bin\Debug\PlayHouse.accdb"))
    {
        using (OleDbCommand sourceCommand = new OleDbCommand(query, source))
        {
            source.Open();

            using (OleDbDataReader dr = sourceCommand.ExecuteReader())
            {
                try
                {
                    dataTable.Load(dr);
                }
                catch (Exception)
                {
                    //Do nothing
                }
                finally
                {
                    source.Close();
                }
            }
        }
    }

    return dataTable;
}

现在,我大部分时间都是免费的,所以它可能不会开箱即用,但它应该会给你一个想法。

如果你必须使用OleDb,以下几点就可以了。我自己也测试过代码

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


            string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
                Source=C:\Users\James\Desktop\Programming\2004RAW.accdb";

                string query = "SELECT FIRST_NAME FROM 2004RAW";

            GetQuery(query, connectionString);
        }

        public void GetQuery(string query, string connectionString)
        {
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                OleDbCommand command = new OleDbCommand(query, connection);
                connection.Open();
                OleDbDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    Console.WriteLine(reader["FIRST_NAME"]);
                }
                reader.Close();
            }
        }
    }
}

还有别的事情。这段代码在任何时候都不会从数据库中删除..我同意Simon的观点。它只从数据库中读取数据,从不在任何地方删除任何内容。试着一步一步地调试。好的,我得到了“删除信息”的错误。这段代码没有起作用。在运行数据库时,我不知何故将整个程序设置为还原到数据库的旧版本。但是,当我运行这个程序时,文本框中仍然没有任何内容。我真的需要一个数据表吗?我在想,因为我只需要一个值(在本例中是今晚节目的可用座位数),所以我可以从数据库中获取一个值并将其放入文本框中。上述方法的优点是它可以接受您传入的几乎任何查询,因为它返回一个datatable,您没有将其限制在某一列或另一列。这是一种“一种方法万能”的方法。您只需使用返回的datatable,并将您选择的列名用作索引。
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.Data;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


            string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
                Source=C:\Users\James\Desktop\Programming\2004RAW.accdb";

                string query = "SELECT FIRST_NAME FROM 2004RAW";

            GetQuery(query, connectionString);
        }

        public void GetQuery(string query, string connectionString)
        {
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                OleDbCommand command = new OleDbCommand(query, connection);
                connection.Open();
                OleDbDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    Console.WriteLine(reader["FIRST_NAME"]);
                }
                reader.Close();
            }
        }
    }
}