Insert Data in Database (mssql)
How TO Insert Data In MSsql DataBase with ASP.NET (C#).
So let's Start Now...
You must do 5 steps for insert, update, and delete (DML)Operations.
Step 1: Add " using System.Data.SqlClient ; ".
Step 2: Create Object of SqlConnection... like this.. ( SqlConnection Objname = new SqlConnection(); )
Step 3: Crate Object of SqlCommand.... like this... ( SqlCommand cmd = new SqlCommannd(); ).
Step 4: After create Objects of sqlconnection and sqlcommand You Open the Connection. Like this (Objname.Open() ;)
Step 5: Execute your Query...
Here Example::::......
-------------------------------------------------------------------------------------------
Code Start
-------------------------------------------------------------------------------------------
using System;
using System.Windows.Forms;
using System.Data.SqlClient; // Step 1
namespace Managment
{
public partial class Form1 : Form
{
public Form1() // Constructor of this class
{
InitializeComponent();
}
private void btnsave_Click(object sender, EventArgs e)
{
SqlConnection objSqlConnection = new SqlConnection("Data Source = SUMITSHEKHAWAT; Initial Catalog = Company; Integrated Security = True"); // Step 2
SqlCommand objSqlCommand = new SqlCommand("Insert into Users values('"+Convert.ToString(txtname.Text.Trim())+"')",objSqlConnection); // Step 3
objSqlConnection.Open(); // Step4
int resultstorer = objSqlCommand.ExecuteNonQuery(); // Step5
if (resultstorer > 0)
{
lblmessage.Text = "Data Save Succesfully";
}
else
{
lblmessage.Text = "Data Not Save Please check your code";
}
}
}
}
Code End
-------------------------------------------------------------------------------------------
Result :

Comments
Post a Comment