ADO.
NET
ADO.NET is a data access technology from Microsoft .Net
Framework , which provides communication between relational and
non-relational systems through a common set of
components .ADO.NET consist of a set of Objects that expose data
access services to the .NET environment. ADO.NET is designed to be
easy to use, and Visual Studio provides several wizards and other
features that you can use to generate ADO.NET data access code.
Data Providers and DataSet
The two key components of ADO.NET are Data
Providers andDataSet . The .Net Framework includes mainly three
Data Providers for ADO.NET. They are the Microsoft SQL Server
Data Provider , OLEDB Data Provider and ODBC Data
Provider . SQL Server uses the SqlConnection object , OLEDB uses
the OleDbConnection Object and ODBC uses OdbcConnection Object
respectively.
Steps to create a database using c#:
Step1:
Start a new project, give <name>.
Step2:
Right click the project in solution explorer and click on Add.
The following window pops up.
Select data on left side and select service-based database.
Give name of the database and select add.
The next screen looks like this:
Select dataset and select next. The following screen appears.
Just click finish.
The solution explorer window will now display the database name.
Double click the database name, left side sever explorer will show
the database and its entities.
Data in database is stored in Tables in the form of rows and
coloumns. To define or design a table in the database, right click on
the tables option and select add a new table
Now the fields can be specified under the name and datatype.
Once the design part is complete click on update . The above window is seen and
click update database.
Right click on the table name in server explorer and select show table data to
enter records into the table. It looks like this:
Now save this and close the application and open once again to design a form and
view the data.
Once the application is opened, go to the properties window in solution explorer
The properties window will open and select add resource in that add existing file
The window will then look like this:
Now select settings option, and type the second line.
Save and go to solution explorer and add a new class and name it as connection
class. We will now establish connection in this class.
This code is for connection class:
namespace createdatabase
{
class Connectionclass
{
private string sql_string;
private string strcon;
System.Data.SqlClient.SqlDataAdapter da1;
public string sql
{
set
{ sql_string = value; }
}
public string connection_string
{
set
{ strcon = value; }
}
public System.Data.DataSet getconnection
{
get
{ return Mydataset(); }
}
private System.Data.DataSet Mydataset()
{
System.Data.SqlClient.SqlConnection con = new
System.Data.SqlClient.SqlConnection(strcon);
con.Open();
da1 = new System.Data.SqlClient.SqlDataAdapter(sql_string, con);
System.Data.DataSet dat_set = new System.Data.DataSet();
da1.Fill(dat_set, "Table1");
con.Close();
return dat_set;
}
}
}
Now design your form and and put the following code:
namespace createdatabase
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Connectionclass objconnect;
string constring;
DataSet ds;
DataRow drow;
int maxrows;
int i = 0;
private void Form1_Load(object sender, EventArgs e)
{
try
{
objconnect = new Connectionclass();
constring = Properties.Settings.Default.EmployeedatabaseConnectionString;
objconnect.connection_string = constring;
objconnect.sql = Properties.Settings.Default.SQL;
ds = objconnect.getconnection;
maxrows = ds.Tables[0].Rows.Count;
navigaterecords();
}
catch (Exception er)
{
MessageBox.Show(er.Message);
}
}
private void navigaterecords()
{
drow = ds.Tables[0].Rows[i];
txtsno.Text = drow.ItemArray.GetValue(0).ToString();
txtname.Text = drow.ItemArray.GetValue(1).ToString();
txtsal.Text = drow.ItemArray.GetValue(2).ToString();
txtdoj.Text = drow.ItemArray.GetValue(3).ToString();
}
private void btnnext_Click(object sender, EventArgs e)
{
if (i != maxrows - 1)
{
i++;
navigaterecords();
}
else
{
MessageBox.Show("No More Records");
}
}
private void btnback_Click(object sender, EventArgs e)
{
if (i > 0)
{
i--;
navigaterecords();
}
else
{
MessageBox.Show("First Record");
}
}
}
}
The above program is for creating and navigating through the database tables.