Database connectivity in asp.net using ms sql server 2008 (insert command)
1) A) सबसे पहले हम Database and Table Create करने के लिए निम्न Step को करते है -
- सबसे पहले हम Start Button पर Click करते है
- इसके बाद हम All Programs पर Click करते है
- इसके बाद Microsoft SQL Server 2008 पर Click कर SQL Server Management Studio पर Click करने पर SQL Server Management Studio Window Display होती है।
6. Database Name अंतर्गत Table पर Mouse का Right Button Click कर New Table Option पर Click करते हैैै।
7- इसके बाद Table के Column Name, Data Type , Allow Nulls Fields को अपनी आवश्यकतानुसार निर्धारित करते है तथा Table को StudentTable Name से Save करते है।
Identity Specification : Yes
(Is Identify) : Yes
Identity Increment : 1
Identity Seed : 1
1) B) इसके बाद हम Form Design करते है।
C) Database Connection Create करने के लिए Server Explorer Icon पर Click करते है तो Add Connection Dailog Display होता है। इसमें Data Source के अंतर्गत Change Button पर Click कर Data Source को Change करते है। Microsoft Sql Sever पर Click कर Ok Button पर Click करते है।
इसके बाद Server Name Type करते है
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient; //
Add Namespace
using System.Configuration;
using System.Data; // Add
Namespace
/// <summary>
/// Summary description for
DatabaseConnectivityClass
/// </summary>
public class DatabaseConnectivityClass
{
SqlConnection
con = new SqlConnection(ConfigurationManager.ConnectionStrings["MSQL"].ToString());
public
int ForQuery(String
sqlcmd)
{
con.Open();
SqlCommand
cmd = new SqlCommand(sqlcmd,
con);
int
x = cmd.ExecuteNonQuery();
con.Close();
return
x;
}
}
<?xml version="1.0"?>
<!--
For more
information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<!--Add connectionStrings-->
<connectionStrings>
<add name="MSQL" connectionString="Data
Source=CSLODHI-PC\SQLEXPRESS;Initial Catalog=student_info;Integrated
Security=True"/>
<!--Add here connection string -->
</connectionStrings>
</configuration>
J) Save Data:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default :
System.Web.UI.Page
{
protected
void Button1_Click(object
sender, EventArgs e)
{
string
insertdata = "Insert into
StudentTable(NameofStudent,CourseName,Address,ContactNumber) values('"
+ TextBox1.Text + "','" +
TextBox2.Text + "','" +
TextBox3.Text + "','" +
TextBox4.Text + "')";
Response.Write("<font color=green>"+insertdata+ "</font>");
DatabaseConnectivityClass
obj = new DatabaseConnectivityClass();
int
x = obj.ForQuery(insertdata);
Response.Write("</br>Save Data Successfully"+x);
Label1.Text = "Save Data Successfully";
}
K) New Record Button Code:
protected
void New_Record_Click(object
sender, EventArgs e)
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
Label1.Text = "";
}
}



No comments: