Simple Example to Start With ADO.NET in Windows C#
In previous posts we have seen connecting with sql server using ADO.NET, Simple C# program with ADO.NET .
In this post we will see a sample windows application with ADO.NET . I am designing the form as shown in the image below.
In this post we will see a sample windows application with ADO.NET . I am designing the form as shown in the image below.
create a table in the database. Here we are using sql server.
1 |
CREATE TABLE EMPTABLE(EMPNAME VARCHAR(25),SALARY DECIMAL) |
The code in the code behind file is given below :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
using System; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; namespace AdoDemo1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } SqlConnection con = new SqlConnection("data source=localhost;initial catalog=sameer;Integrated security=true"); SqlCommand cmd; private void btnsubmit_Click(object sender, EventArgs e) { cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandType = CommandType.Text; cmd.CommandText="insert into emptable(empname,salary) values(@empname,@salary)"; cmd.Parameters.AddWithValue("@empname", txtname.Text); cmd.Parameters.AddWithValue("@salary", decimal.Parse(txtsalary.Text)); con.Open(); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("record inserted successfully"); } } } |
To have a look at output see the image below :