![]() |
| Fig1: Scr Shot of sample table in SQL DB |
![]() |
| Fig2:Scr shot of data being displayed from DB using a windows form |
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.SqlClient;
namespace WinConnectedApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnGetData_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
// Create a connection object
con.ConnectionString = @"Data source=USERS35\SQLExpress;initial catalog = training1;integrated security =true";
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select empid,empfname,emplname,emploc,empsal from employee";
//0 1 2 3 4
try
{
con.Open();//establishes phy link between App and DB
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();//Reads one row from employee
txtEmpId.Text = dr["empid"].ToString();
txtFirstName.Text = dr["empfname"].ToString();
txtLastName.Text = dr["emplname"].ToString();
txtloc.Text = dr["emploc"].ToString();
txtSal.Text = dr["empsal"].ToString();
dr.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
}
}

