Working with dataset class hierarchy
A collection of dataset inter-related classes is called as dataset class hierarchy
Observation on system.data.sqlclient namespace
->this namespace supports 2 types of sql server managed connections
1)untrusted connections
2)trusted connections
-> In untrusted connection, userid and password sqlserver is required.
->In trusted connection, userid and password are not required
Example on navigating through the records with sql server trusted connection
Open windows forms app project
Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new->
project->select visual c# from installed templates->select windows forms application project
place 4 buttons with text first, prev, next and last
place 2 labels and 2 textboxs
using System.Data.sqlclient;
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 WindowsFormsApplication23
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataTable dt = new DataTable();
int n = 0;
public void showrecord()
{
DataRow dr = dt.Rows[n];
textBox1.Text = dr[0].ToString();
textBox2.Text = dr[1].ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("trusted connection=yes;database=northwind");
SqlDataAdapter da = new SqlDataAdapter("select * from products", cn);
DataSet ds = new DataSet();
da.Fill(ds, "d");
dt = ds.Tables["d"];
label1.Text = dt.Columns[0].ColumnName;
label2.Text = ds.Tables["d"].Columns[1].ColumnName;
showrecord();
}
private void button1_Click(object sender, EventArgs e)
{
n = 0;
showrecord();
}
private void button2_Click(object sender, EventArgs e)
{
n = n - 1;
if (n == -1)
{
n = 0;
MessageBox.Show("no prev record");
}
showrecord();
}
private void button3_Click(object sender, EventArgs e)
{
n = n + 1;
if(n>dt.Rows.Count-1)
{
n = dt.Rows.Count - 1;
MessageBox.Show("no next record");
}
showrecord();
}
private void button4_Click(object sender, EventArgs e)
{
n = dt.Rows.Count - 1;
showrecord();
}
}
}