This website completely moved to new platform. For latest content, visit www.programmingposts.com

Search this Site

5 Dec 2012

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.

ADO.NET SAMPLE APPLICATION_FORM


create a table in the databse. Here we are using sql server.

CREATE TABLE EMPTABLE(EMPNAME VARCHAR(25),SALARY DECIMAL)

 The code in the code behind file is given below:



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 ouput see the image below:

ADO.NET SAMPLE APPLICATION_OUTPUT

Download Source Code :
Ado.Net Sample Application.rar    .


Support blog with your valuable comments..

No comments:

Post a Comment

Thanks for your comments.
-Sameer