MANU BAR

banner image
banner image

Database connectivity in asp.net using ms sql server 2008 (insert command)

 Database connectivity in asp.net using ms sql server 2008 (insert command)

1)    A)  सबसे पहले हम Database and Table Create करने के लिए निम्‍न Step को करते है - 

  1. सबसे पहले हम Start Button पर Click करते है
  2. इसके बाद हम All Programs पर Click करते है 
  3. इसके बाद Microsoft SQL Server 2008 पर Click कर SQL Server Management Studio पर Click करने पर SQL Server Management Studio Window Display होती है।


4. Connect Button पर Click करते है। 




5. Database पर Mouse का Right Button Click करते है इसके बाद New Database पर Click करने पर New Database Window Display होती है। Database Name Type कर Ok Button पर Click करते है। 

 




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 करते है। 



  StudentId Field पर Mouse का Right Button Click कर Set Primary Key पर Click कर StudentId Column की Properties Set करते है जैसे - 
Identity Specification     : Yes
          (Is Identify)     :  Yes
          Identity Increment :  1
          Identity Seed      : 1



Watch this video




Script for Creating Table:- 

USE [student_info]
GO

/****** Object:  Table [dbo].[StudentTable]    Script Date: 10/17/2020 04:41:33 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[StudentTable](
[StudentId] [int] IDENTITY(1,1) NOT NULL,
[NameofStudent] [varchar](50) NOT NULL,
[CourseName] [varchar](50) NOT NULL,
[Address] [varchar](50) NOT NULL,
[ContactNumber] [varchar](50) NOT NULL,
 CONSTRAINT [PK_UserTable] PRIMARY KEY CLUSTERED 
(
[StudentId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

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 करते है 




D) Server Name Type कर Connect to a Database के अंतर्गत Select or enter a database name के Combobox से Table का नाम Select करते है जिस Table से हम Connection Establish(बनाना) करना चाहते है। इसके बाद Test Connection Button पर Click करते है। इसके बाद ok button पर Click करते है। 


E) इसके बाद Server Explorer सेे Create किए गए Connection पर Click करते है ।  






F) Connection String:- प्रत्‍येक मशीन की अलग अलग Connection String होती है  - 

Data Source=CSLODHI-PC\SQLEXPRESS;Initial Catalog=student_info;Integrated Security=True






G) (Adding App_Code Folder and Class ) Visual Studio के Solution Explorer Window में Project पर Right click कर Add ASP.NET Folder के अंतर्गत App_Code Folder पर Click कर Add करते है। 

H) App_Code Folder पर Right Click कर Add New Item पर Click करते है इसके बाद एक Window Display होती है ि‍जिसमें से Class को Select कर Class Name Type कर Add Button पर Click करते है इस प्रकार हम App_Code के अंतर्गत एक Class Add कर सकते है। 


Class Name as DatabaseConnectivityClass.cs (Code):- 

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;

    }

   

}

 



I) Web.config file : (Code) :

<?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 = "";

    }

   }

 




 








Database connectivity in asp.net using ms sql server 2008 (insert command) Database connectivity in asp.net using ms sql server 2008 (insert command) Reviewed by JOB ORIENTED STUDY ACADEMY on October 17, 2020 Rating: 5

No comments:

Powered by Blogger.