using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace SimpleDataSetExample
{
/// <summary>
/// Example of working with data retrieved through SQL.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
/// <summary>
/// A screen that demonstates a method of binding data through
/// a dataset and another that manually reads the records in a
/// SqlDataReader
/// </summary>
public Form1()
{
// Required for Windows Form Designer support
InitializeComponent();
// Add a button to retrieve SQL and put in a grid
Button btnRetrieveDS = new Button();
btnRetrieveDS.Width = 100;
btnRetrieveDS.Left = 20;
btnRetrieveDS.Text = "Retrieve SQL";
btnRetrieveDS.Click += new EventHandler(btnRetrieveDS_Click);
this.Controls.Add(btnRetrieveDS);
// Add a button to retrieve SQL and interate through records
Button btnReadSQL = new Button();
btnReadSQL.Width = 125;
btnReadSQL.Left = btnRetrieveDS.Left + btnRetrieveDS.Width + 20;
btnReadSQL.Text = "Read SQL Results";
btnReadSQL.Click += new EventHandler(btnReadSQL_Click);
this.Controls.Add(btnReadSQL);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(288, 277);
this.Name = "Form1";
this.Text = "Form1";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnRetrieveDS_Click(object sender, System.EventArgs e)
{
try
{
/* Use the Connection->Data Adapter->DataSet method to get
a resultset and bind it to a control on the form */
SqlConnection conn = new SqlConnection("server=(local);database=Northwind;trusted_connection=yes;");
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Customers", conn);
DataSet ds = new DataSet("Customers");
da.Fill(ds,"Customers");
// Now create a datagrid and bind the results
DataGrid grdResults = new DataGrid();
grdResults.Top = 30;
grdResults.Width = this.Width-20;
grdResults.Height = this.Height - grdResults.Top - 40;
grdResults.Anchor = AnchorStyles.Left |
AnchorStyles.Right |
AnchorStyles.Top |
AnchorStyles.Bottom;
this.Controls.Add(grdResults);
grdResults.BringToFront();
grdResults.DataSource = ds.Tables[0];
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void btnReadSQL_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection("server=(local);database=Northwind;trusted_connection=yes");
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", conn);
SqlDataReader reader = cmd.ExecuteReader();
// iterate through resultset and build a string list of all the customer contacts
string message = "The customers are: \r\n";
while (reader.Read())
{
message += reader["ContactName"] + "\r\n";
}
TextBox txtOutput = new TextBox();
txtOutput.Text = message;
txtOutput.Multiline = true;
txtOutput.Top = 30;
txtOutput.Width = this.Width-20;
txtOutput.Height = this.Height - txtOutput.Top - 40;
txtOutput.Anchor = AnchorStyles.Left |
AnchorStyles.Right |
AnchorStyles.Top |
AnchorStyles.Bottom;
txtOutput.ScrollBars = ScrollBars.Vertical;
this.Controls.Add(txtOutput);
txtOutput.BringToFront();
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}