2012-12-12 110 views
0

我正在尝试从MS-Access 2010数据库表中填充文本框。桌子上有2个字段,夜景和座位。假设在夜间字段中选择的日期有一个当前值,我想将当前值置于Seats字段中。我现在所拥有的代码是:如何从C#中的Access 2010数据库填充文本框?

 //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

我同意西蒙。这只能从数据库中读取,不会在任何地方移除任何东西。尝试一步一步的调试。 –

+0

好的,我从那里得到了“删除信息”的bug。这段代码没有这样做。当我运行它时,我不知何故将整个程序设置为恢复到旧版数据库。不过,我仍然没有在文本框中找到任何东西。 –

回答

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

现在我放手这在大多数情况下,因此可能无法正常工作开箱即用,但它应该给你一个想法。

+0

我真的需要数据表吗?我在想,因为我只需要一个值(在这种情况下,可用于今晚节目的座位数量),我只需从数据库中获取一个值并将其放入文本框即可。 –

+0

上述方法的优点是它几乎可以接受任何您传入的查询,并且因为它返回一个数据表,您不会将其限制为一列或另一列。这是其中一种“一种方法”。您只需使用返回的数据表与您选择的列名作为索引。 –

相关问题