Dataset Connect
Dataset Connect
{
SqlConnection conn = null;
//Constructor to initialize the connection string.
public SQLDemo()
{
conn = new SqlConnection("Data Source=.\\SQLExpress;Initial
Catalog=Northwind;Integrated Security=True");
}
//Method to read data fastest way readonly forward only
public void ReadData()
{
SqlDataReader rdr = null;
SqlCommand com = new SqlCommand("select * from customers", conn);
try
{
conn.Open();
rdr = com.ExecuteReader();
while (rdr.Read())
{
while (rdr.Read())
{
// get the results of each column
string contact = (string)rdr["ContactName"];
string company = (string)rdr["CompanyName"];
string city = (string)rdr["City"];
}
finally
{
if (rdr != null)
rdr.Close();
if (conn != null)
conn.Close();
}
}
//Method to retrieve count of data
public int CountData()
{
int cnt = -1;
SqlCommand com = new SqlCommand();//("select count(1) from customers", conn);
com.CommandText = "select count(1) from customers";
com.Connection = conn;
try
{
conn.Open();
cnt = (int)com.ExecuteScalar();
}
finally
{
if (conn!=null)
{
conn.Close();
}
}
return cnt;
}
da.Fill(ds, "Customers");
}
}
using (SqlCommand comm = new SqlCommand("select * from orders", conn))
{
using (SqlDataAdapter daO = new SqlDataAdapter(comm))
{
daO.Fill(ds, "orders");
}
}
conn.Close();