SqlDataAdapter is a part of the ADO.NET Data Provider and it resides in the System.Data.SqlClient namespace. SqlDataAdapter provides the communication between the Dataset and the SQL database. We can use SqlDataAdapter Object in combination with Dataset Object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace killercodes_in.DataAdapter | |
{ | |
public partial class Form1 : Form | |
{ | |
public string _connStr_ = "Data Source=127.0.0.1,1433;Initial Catalog=avyukta;User Id=sa;Password=iamadmin;"; | |
SqlCommand cmd; | |
SqlConnection conn; | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
conn = new SqlConnection(_connStr_); | |
//cmd = new SqlCommand(); | |
SqlDataAdapter adap = new SqlDataAdapter(); | |
DataSet ds = new DataSet(); | |
int i = 0; | |
try | |
{ | |
conn.Open(); | |
cmd = new SqlCommand("select * from grademstr", conn); | |
adap.SelectCommand = cmd; | |
adap.Fill(ds); | |
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) | |
{ | |
MessageBox.Show(ds.Tables[0].Rows[i].ItemArray[0] + "-" + | |
ds.Tables[0].Rows[i].ItemArray[1] + "-" + ds.Tables[0].Rows[i].ItemArray[2] + "-" + ds.Tables[0].Rows[i].ItemArray[3]); | |
} | |
adap.Dispose(); | |
cmd.Dispose(); | |
conn.Close(); | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.Show(ex.Message); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Sql Data Adapter | |
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.Sql; | |
using System.Data.SqlClient; | |
namespace killercodes_in.SqlDataAdapter_ | |
{ | |
public partial class Form1 : Form | |
{ | |
private string _cString1 = "Data Source=(local);Initial Catalog=avyukta;User Id=sa;Password=iamadmin;"; | |
//SqlCommand cmd; | |
SqlConnection con; | |
SqlDataAdapter adp; | |
DataSet ds; | |
SqlCommandBuilder cb; | |
public Form1() | |
{ | |
InitializeComponent(); | |
con = new SqlConnection(_cString1); | |
} | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
//DataGrid dg; | |
try | |
{ | |
con.Open(); | |
con.Close(); | |
} | |
catch (Exception) | |
{ | |
} | |
ds = new DataSet(); | |
adp = new SqlDataAdapter("select * from grademstr", con); | |
cb = new SqlCommandBuilder(adp); | |
adp.Fill(ds, "grademstr"); | |
dataGridView1.DataSource = ds; | |
dataGridView1.DataMember = "grademstr"; | |
} | |
private void button2_Click(object sender, EventArgs e) | |
{ | |
cb.GetUpdateCommand(); | |
adp.Update(ds, "grademstr"); | |
} | |
} | |
} |