Text-based controls are the prime candidate for showing the value of a column of a table. Probably the simplest way to do this is by assigning a value gotten from a SQL data reader. Here is an example: using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;
public class Exercise : System.Windows.Forms.Form
{
Label lblTitle;
Button btnBinder;
Button btnCreateDatabase;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
btnCreateDatabase = new Button();
btnCreateDatabase.Text = "Create Database";
btnCreateDatabase.Location = new Point(12, 12);
btnCreateDatabase.Width = 120;
btnCreateDatabase.Click += new EventHandler(btnCreateDatabaseClick);
btnBinder = new Button();
btnBinder.Text = "Bind";
btnBinder.Location = new Point(140, 12);
btnBinder.Click += new EventHandler(btnBinderClick);
lblTitle = new Label();
lblTitle.Text = "Title";
lblTitle.Location = new Point(12, 44);
Text = "Database Exercise";
Controls.Add(lblTitle);
Controls.Add(btnBinder);
Controls.Add(btnCreateDatabase);
StartPosition = FormStartPosition.CenterScreen;
}
void btnCreateDatabaseClick(object sender, EventArgs e)
{
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local); " +
"Integrated Security='SSPI';"))
{
SqlCommand cmdExercise =
new SqlCommand("IF EXISTS (SELECT name " +
"FROM sys.databases WHERE name = N'Exercsie1' " +
") " +
"DROP DATABASE Exercsie1; " +
"CREATE DATABASE Exercsie1", cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
}
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local); " +
"Database='Exercsie1'; " +
"Integrated Security='SSPI';"))
{
SqlCommand cmdExercise =
new SqlCommand("CREATE TABLE Employees(EmployeeNumber nvarchar(8), " +
"FirstName nvarchar(24), LastName nvarchar(24), " +
"Title nvarchar(50));", cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
}
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local); Database='Exercsie1'; " +
"Integrated Security='SSPI';"))
{
SqlCommand cmdExercise =
new SqlCommand("INSERT INTO Employees " +
"VALUES(N'927049', 'Aaron', 'Swanson', 'General Owner')," +
" (N'804070', 'Justine', 'Aronson', 'Accountant')," +
" (N'284825', 'Paul', 'DaCosta', 'Webmaster')," +
" (N'380408', 'Desmond', 'Perez', 'Account Associate');",
cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
}
}
private void btnBinderClick(object sender, EventArgs e)
{
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local);" +
"Database='Exercsie1';" +
"Integrated Security=SSPI;"))
{
SqlCommand cmdExercise = new SqlCommand("SELECT ALL * FROM Employees;", cntExercise);
cntExercise.Open();
SqlDataReader rdrExercise = cmdExercise.ExecuteReader();
while (rdrExercise.Read())
lblTitle.Text = rdrExercise[3].ToString();
}
}
}
public class Program
{
[STAThread]
static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}
An alternative is to use the Binding class. In this case, you must access the DataBindings property of the label and call the Add() method of the property. In the parentheses, use the primary constructor of the class to pass the Text property as a string, the index of the first table of the data set, and the name of the column of the table. This can be done as follows: private void btnBinderClick(object sender, EventArgs e)
{
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local);" +
"Database='Exercsie1';" +
"Integrated Security=SSPI;"))
{
SqlCommand cmdExercise = new SqlCommand("SELECT EmployeeNumber, " +
"FirstName, LastName, " +
"Title FROM Employees;",
cntExercise);
SqlDataAdapter sdaExercise = new SqlDataAdapter(cmdExercise);
DataSet dsEmployees = new DataSet("EmployeesSet");
cntExercise.Open();
sdaExercise.Fill(dsEmployees);
lblTitle.DataBindings.Add(new Binding("Text", dsEmployees.Tables[0], "Title"));
}
}
|