I wonder when you should use a data set and Data adapter and when not to.
Do you have to use data adapter if you want to work disconnected from the database? If you use Data adapter, must you also use dataset?
I understand that dataset is a representation of tables in memory and data adapter is somehow a link between a dataset and the database.
I have this code:
string connectionString = "data source = KONTORET\\sqlexpress; Initial Catalog = testDatabas; Trusted_Connection = True";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
string selectStatement = "select * from Cars";
SqlCommand command = new SqlCommand(selectStatement, connection);
As I understand it you doesn't need data adapter when you work like this. Do you use a dataset even if you don't say so explicitly?
I would also like to know how to show this in a form.
Loading
Bitini LumbetPosted May 20, 2012, 3:03 PM
Lars PerssonPosted May 21, 2012, 5:22 AM
EhteshamPosted May 21, 2012, 4:48 AM
*)SqlDataAdapter can directly update the DB(for instance change in datagridview can directly reflect database table) if used with SqlCommandBuilder
*)SqlDataAdapter can fill DataTable,DataSet
*)Sqlcommand can use Command.Executereader to read the data(which has better performance then Fill method of DataAdapter)
SenthilkumarPosted May 20, 2012, 11:59 PM
The DataAdapter is providing connectivity to the sql server and source of the data needs to be stored.
The sql data adapter accepts the data set or datatable to store the information. If the result returns in the multiple then DataSet can handle internally.
If it returns single table result then DataTable preferred.
Satyapriya NayakPosted May 20, 2012, 11:13 PM
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.OleDb;
namespace Dataset_and_Datatable_windows
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oledbda;
string str;
DataSet ds;
DataTable dt;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Using Dataset
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from employee";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "employee");
dataGridView1.DataMember = "employee";
dataGridView1.DataSource = ds;
con.Close();
//Using Datatable
OleDbConnection con = new OleDbConnection(ConnectionString);
dt = new DataTable();
com = new OleDbCommand();
com.Connection = con;
con.Open();
com.CommandText = "Select * from employee";
oledbda = new OleDbDataAdapter(com);
oledbda.Fill(dt);
dataGridView1.DataMember = "employee";
dataGridView1.DataSource = dt;
con.Close();
}
}
}
Thanks
Lars PerssonPosted May 20, 2012, 2:20 PM
This works fine:
stringconnectionString = "data source = KONTORET\\sqlexpress; Initial Catalog = testDatabas; Trusted_Connection = True";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Bilar", connection);
DataTable table = new DataTable();
adapter.Fill(table);
dataGridView1.DataSource = table;
But this doesn't, why?
Same connection,same adapter and then:
DataSet DS = new DataSet();
adapter.Fill(DS);
dataGridView1.DataSource = DS.Tables;
Edit: This works:
dataGridView.DataSource = DS.Tables[0];
Bitini LumbetPosted May 20, 2012, 1:57 PM