Pages

Monday, 29 January 2018

Simple Example to Start With ADO.NET in Windows C#

Simple Example to Start With ADO.NET in Windows C#
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");
        }  
    }

}