How to create SqlDataAdapter in C#
Today I will explain How to create SqlDataAdapter in C# .The SqlDataAdapter in C# acts as an Bridge between a DataSet and a data source (SQL Server Database) to retrieve data. The SqlDataAdapter is a class that represents a set of SQL commands and a database connection. It can be used to fill the DataSet and update the data source.
This SqlDataAdapter object is used to read the data from database and bind that data to dataset. DataAdapter is a disconnected oriented architecture.
Check below sample code to see how to use DataAdapter in code:
SqlDataAdapter(): Initializes a new instance of the SqlDataAdapter class.
How to create instance of C# SqlDataAdapter class in ADO.NET?
In order to create an instance of the SqlDataAdapter class, we need to specify two things. The sql command that we want to execute and the connection on which we want to execute the command. Following is the syntax to create an instance of the SqlDataAdapter class.
string con="data source=.\\SQLEXPRESS; database=finacial ; integrated security=SSPI";
sqlconnection connection = new sqlconnection(con);
sqlDataAdapter sda= new sqlDataAdapter("select * from recharge",connection);
Related Links:
sqlconnection con=new sqlconnection("Data source="DESKTOP-19Q8E5J\\SQLEXPRESS";Initial catalog=finacial;Integrated security=true;");
>> create a database named as finacial
>> create a table and named as login
create table login (id int primary key identity(1,1) not null,username varchar(50) not null, password varchar(50) not null);
create table and named as recharge
create table recharge(
id int primary key IDENTITY(1,1) NOT NULL,
mobilenetwork varchar(50) NULL,
mobilenumber varchar(50) NULL,
amount varchar(40) NULL,
date varchar(60) NULL,
status varchar(50) NULL);
And then create two empty file named as
1.login.cs
2.rrmain.cs
1.login.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace RRstudio_finacial_application
{
public partial class Login : Form
{
SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=finacial;Integrated Security=True");
public Login()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select Count(*) from login where username='" + textBox1.Text + "' and password='" + textBox2.Text + "'",con);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows[0][0].ToString() == "1")
{
RR_Main rm = new RR_Main();
rm.Show();
this.Hide();
}
else { MessageBox.Show("please check user username and password"); }
con.Close();
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
}
}
}
2.rrmain.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace RRstudio_finacial_application
{
public partial class RR_Main : Form
{
SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=finacial;Integrated Security=True");
public RR_Main()
{
InitializeComponent();
display();
display2();
}
private void btnNew(object sender, EventArgs e)
{
textBox2.Text = "";
textBox2.Clear();
textBox3.Text = "";
textBox3.Clear();
comboBox1.SelectedIndex = -1;
comboBox8.SelectedIndex = -1;
textBox16.Clear();
}
private void btnsave(object sender, EventArgs e)
{
con.Open();
SqlCommand cm = new SqlCommand("insert into recharge (mobilenetwork,mobilenumber,amount,date,status) values ('" + comboBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + dateTimePicker2.Text + "','"+comboBox8.Text+"')", con);
cm.ExecuteNonQuery();
MessageBox.Show("Insert Successfully");
textBox2.Clear();
textBox16.Clear();
textBox3.Clear();
comboBox1.SelectedIndex = -1;
comboBox8.SelectedIndex = -1;
con.Close();
display();
}
void display() {
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from recharge",con);
System.Data.DataTable dt = new System.Data.DataTable();
sda.Fill(dt);
dataGridView1.Rows.Clear();
foreach (DataRow items in dt.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = (n + 1).ToString();
dataGridView1.Rows[n].Cells[1].Value = items[0].ToString();
dataGridView1.Rows[n].Cells[2].Value = items[1].ToString();
dataGridView1.Rows[n].Cells[3].Value = items[2].ToString();
dataGridView1.Rows[n].Cells[4].Value = items[3].ToString();
dataGridView1.Rows[n].Cells[5].Value = items[4].ToString();
dataGridView1.Rows[n].Cells[6].Value = items[5].ToString();
}
con.Close();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (textBox2.TextLength <= 10)
{
}
else { MessageBox.Show("mobile number will be upto 10 number"); }
}
private void RR_Main_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
private void changePasswordToolStripMenuItem_Click(object sender, EventArgs e)
{
Change_Password cp = new Change_Password();
cp.Show();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
In this step we will learn C# SqlDataAdapter it will helps you with your need. The next time I will put some more interesting commands, and continue project thank you this is helpful for you will Like That's all, You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.