C# Database Tutorial in C# and ASP.NET

C# Database Tutorial in C# and ASP.NET

With C Sharp(commonly known as C#) we can easily do ADO.NET programming to access a database, insert data into database tables, retrieve data from database tables and update data in the database tables. This is a tutorial suited for anyone who would like to get started working with database in C# by using ADO.NET. In this C# Database Tutorial we will teach you how to insert data into database.

c# Insert Into SQL Application

Our application is simple which insert email address to the database on a button click. Our web page has just 2 controls one textbox for email and other submit button. Our application uses asp.net with C# as our programming language. The database which we are using is SQL Server 2012.

Our HTML

<p>Insert Into Database</p>
<p id="messageParagraph" runat="server"></p>
<div>
    <asp:TextBox ID="emailTextBox" runat="server" placeholder="Enter an email address"></asp:TextBox>	
</div>
<div>
    <asp:Button ID="submitButton" runat="Server" Text="Submit" OnClick="submitButton_Click" />
</div>

Connection String in Web.Config

Add a database connection string in the ‘web.config’ file. It looks similar to –

<connectionStrings>
    <add name="saCS" connectionString="Data Source=yourdatabaseserver;Initial Catalog=yourdatabasename;Persist Security Info=True;User ID=yourdatabaseuserid;Password=yourdatabasepassword" providerName="System.Data.SqlClient"/>
</connectionStrings>

Sql Server Table and Stored Procedure

The Sql table contains only email field of type varchar(100). We have used stored procedure for inserting the emails in this table. The stored procedure is given below

CREATE PROCEDURE [dbo].[sp_InsertEmail]   
    @Email    VARCHAR(100),
AS            
BEGIN
    INSERT INTO Email(Email) VALUES (@Email)
END

C# Button click Function

protected void submitButton_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString =  ConfigurationManager.ConnectionStrings["saCS"].ConnectionString;
    SqlCommand cmd = new SqlCommand("sp_InsertEmail", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@Email", SqlDbType.VarChar, 100);
    cmd.Parameters["@Email"].Value = emailTextBox.Text;
    conn.Open();
    int i = cmd.ExecuteNonQuery();
    conn.Close();
    if (i==-1)
    {
        messageParagraph.innerHtml="Your email is saved successfully.";
    }
}

Explanation

In the button click event we are calling the stored procedure ‘sp_InsertEmail’ and giving its parameter [email protected] value of emailTextBox.Text. We have use ADO.NET classes to execute the stored procedure. The ‘SqlConnection’ class sets the connection string while ‘SqlCommand’ sets the name of the stored procedure and its parameters. In the end we execute the stored procedure using ‘SqlCommand’ class inbuilt function ‘ExecuteNonQuery’. It returns value -1 when stored procedure is executed successfully, which we have checked with if statement and shown the message in ‘messageParagraph’.

Hope you like our C# Database Tutorial and with it you are ready to start your ADO.NET programming in C#.

Share this article -

yogihosting

ABOUT THE AUTHOR

This article has been written by the Technical Staff of YogiHosting. Check out other articles on "WordPress, SEO, jQuery, HTML" and more.