2012-03-06 24 views
0

我已經將訪問數據庫數據源添加到我的C#項目中,現在我希望能夠使用C#訪問數據。如果我右鍵單擊數據源並按「預覽數據」,我可以看到來自數據源的數據,但我不知道使用什麼代碼來訪問此數據。添加數據源後,如何訪問它?

在預覽菜單中,它顯示了兩個方法.Fill和GetData(),但我不知道如何訪問這些方法。

任何幫助將不勝感激,謝謝!

+0

SataSource或DataSet? – 2012-03-06 15:53:14

+0

DataSet ....... – sooprise 2012-03-06 15:55:16

+1

我不知道「預覽菜單」,但Fill和GetData屬於DataDapater。現在你有足夠的關鍵字一起Google教程。 – 2012-03-06 16:00:16

回答

2

聽起來好像您正在查看DataSet表適配器上顯示的填充/獲取數據方法。這裏有很多資源來舉例說明如何綁定數據,包括在這裏所以我建議看一些例子來看看如何去做(下面的例子是針對datagridviews):

C#: Can't populate DataGridView programatically

using(SqlDataAdapter sqlDataAdapter = 
    new SqlDataAdapter("SELECT * FROM Table1", 
     "Server=.\\SQLEXPRESS; Integrated Security=SSPI; Database=SampleDb")) 
{ 
    using (DataTable dataTable = new DataTable()) 
    { 
     sqlDataAdapter.Fill(dataTable); 
     this.dataGridView1.DataSource = dataTable; 
    } 
} 

Dev X文章中VB但它給你的想法:

Dim connStr As String = _ 
"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;" & _ 
"Integrated Security=True" 
Dim sql As String = "SELECT * FROM Customers" 
Dim conn As SqlConnection = New SqlConnection(connStr) 
Dim comm As SqlCommand = New SqlCommand(sql, conn) 
Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(comm) 
Dim ds As DataSet = New DataSet() 
'---open the connection and fill the dataset--- 
conn.Open() 
'---fill the dataset--- 
dataadapter.Fill(ds, "Customers_table") 
'---close the connection--- 
conn.Close() 
'---bind to the DataGridView control--- 
DataGridView1.DataSource = ds 
'---set the table in the dataset to display--- 
DataGridView1.DataMember = "Customers_table" 

MSDN Bind Data to the Windows Forms DataGridView Control

using System; 
using System.Data; 
using System.Data.SqlClient; 
using System.Windows.Forms; 

public class Form1 : System.Windows.Forms.Form 
{ 
    private DataGridView dataGridView1 = new DataGridView(); 
    private BindingSource bindingSource1 = new BindingSource(); 
    private SqlDataAdapter dataAdapter = new SqlDataAdapter(); 
    private Button reloadButton = new Button(); 
    private Button submitButton = new Button(); 

    [STAThreadAttribute()] 
    public static void Main() 
    { 
     Application.Run(new Form1()); 
    } 

    // Initialize the form. 
    public Form1() 
    { 
     dataGridView1.Dock = DockStyle.Fill; 

     reloadButton.Text = "reload"; 
     submitButton.Text = "submit"; 
     reloadButton.Click += new System.EventHandler(reloadButton_Click); 
     submitButton.Click += new System.EventHandler(submitButton_Click); 

     FlowLayoutPanel panel = new FlowLayoutPanel(); 
     panel.Dock = DockStyle.Top; 
     panel.AutoSize = true; 
     panel.Controls.AddRange(new Control[] { reloadButton, submitButton }); 

     this.Controls.AddRange(new Control[] { dataGridView1, panel }); 
     this.Load += new System.EventHandler(Form1_Load); 
     this.Text = "DataGridView databinding and updating demo"; 
    } 

    private void Form1_Load(object sender, System.EventArgs e) 
    { 
     // Bind the DataGridView to the BindingSource 
     // and load the data from the database. 
     dataGridView1.DataSource = bindingSource1; 
     GetData("select * from Customers"); 
    } 

    private void reloadButton_Click(object sender, System.EventArgs e) 
    { 
     // Reload the data from the database. 
     GetData(dataAdapter.SelectCommand.CommandText); 
    } 

    private void submitButton_Click(object sender, System.EventArgs e) 
    { 
     // Update the database with the user's changes. 
     dataAdapter.Update((DataTable)bindingSource1.DataSource); 
    } 

    private void GetData(string selectCommand) 
    { 
     try 
     { 
      // Specify a connection string. Replace the given value with a 
      // valid connection string for a Northwind SQL Server sample 
      // database accessible to your system. 
      String connectionString = 
       "Integrated Security=SSPI;Persist Security Info=False;" + 
       "Initial Catalog=Northwind;Data Source=localhost"; 

      // Create a new data adapter based on the specified query. 
      dataAdapter = new SqlDataAdapter(selectCommand, connectionString); 

      // Create a command builder to generate SQL update, insert, and 
      // delete commands based on selectCommand. These are used to 
      // update the database. 
      SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter); 

      // Populate a new data table and bind it to the BindingSource. 
      DataTable table = new DataTable(); 
      table.Locale = System.Globalization.CultureInfo.InvariantCulture; 
      dataAdapter.Fill(table); 
      bindingSource1.DataSource = table; 

      // Resize the DataGridView columns to fit the newly loaded content. 
      dataGridView1.AutoResizeColumns( 
       DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader); 
     } 
     catch (SqlException) 
     { 
      MessageBox.Show("To run this example, replace the value of the " + 
       "connectionString variable with a connection string that is " + 
       "valid for your system."); 
     } 
    } 

} 
相關問題