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

时间:2012-12-12 04:02:09

标签: c# ms-access textbox

我正在尝试从MS-Access 2010数据库表填充文本框。该表有2个字段,夜晚和座位。我想将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();

这段代码不仅没有正确填充文本字段(或根本没有),它似乎完全从数据库中删除了今天日期的记录。第一个消息框显示正确的日期,但第二个消息框显示空白。 如何修复此代码以填充文本框并且不删除数据库中的条目?

2 个答案:

答案 0 :(得分:2)

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;
}

然后你可以简单地调用以下内容:

string query = "SELECT Seats FROM Nights WHERE Night = '" + System.DateTime.Now.ToShortDateString();
DataTable dataTable = GetWithQuery(query);
Console.WriteLine(dataTable["Seats"]);

现在我在大多数情况下都放手了,所以它可能不会开箱即用,但它应该给你一个想法。

答案 1 :(得分:0)

如果您必须使用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();
            }
        }
    }
}