0

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

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

4

2 回答 2

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"]);

现在我大部分时间都放手了,所以它可能无法开箱即用,但它应该给你一个想法。

于 2012-12-12T04:28:56.940 回答
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();
            }
        }
    }
}
于 2012-12-12T05:43:11.237 回答