Click here to Skip to main content
15,904,155 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

My question is how to connect database sql server in asp.net.

Please provide with code easily step by step


Thanks,
Sandeep
Posted

Short answer is: use ADO.NET:
http://en.wikipedia.org/wiki/ADO.NET[^],
http://msdn2.microsoft.com/en-us/library/aa286484.aspx[^].

This clear and simple CodeProject article can help you to get started in no time: Using ADO.NET for beginners[^].

—SA
 
Share this answer
 
For usability, and ease of editing, store your connection settings to DB in WebConfig of the site like such:

XML
<connectionStrings>
<add name="customNameForConnection" connectionString="Data Source=nameOfServer or IP;Initial Catalog=nameOfTable;Persist Security Info=True;User ID=Login;Password=Password" />
</connectionStrings>


C#
using System.Data.SqlClient;
 lang="c#">public partial class ClassFile: System.Web.UI.Page
{
    SqlConnection con =new SqlConnection(ConfigurationManager.ConnectionStrings["customCon"].ConnectionString);
    using(con){
            //using statement means the connection to DB will be closed once finished, meaning doesn't cause memory leaks.
        con.Open();
        string commandText = "SELECT * from DB";
        using (SqlCommand command = new SqlCommand(commandText, con))
        {
            using (SqlDataReader dataReader = command.ExecuteReader())
            {
               if(dataReader.Read())
               {
                  //read data
               }
            }
        }
    }
}


if you have a DB query with conditions, you can use commandParameters to utilise changing variables in the commandString variable like so:

C#
var commandString = "SELECT * FROM DB where b = @id";


and then in then inside the usingSqlCommand brackets you would add the code:
C#
command.Parameters.Add("@id", SqlDbType.Int).Value = nameOfVariable;

Note you can have multiple commandParameters.

hope this gives you help you need in connecting to SQL
 
Share this answer
 
Since this is ASP.NET, you need to mention the framework you're using, if you're using a Web Pages web site then you're going to use the following code to connect to the database.

C#
Database.Open("databaseName"); // connected to databaseName


For more on the above class and the methods, please read this MSDN document[^].

Secondly, if you're using any other method, you can simply just use the SqlClient namespace. That exposes the methods and functions that you can use to connect to the database and perform different operations on it. Since it is a Visual C# code, you can use it in ASP.NET application too. All you need to worry about here is the connectionString for your database, because they need to be precise otherwise the database won't be connected to. For more on connectionStrings, please visit this website. http://connectionstrings.com[^]. The sample code for this one would be,

C#
using (SqlConnection conn = new SqlConnection("{connectionString}")) {
   conn.Open(); // connection opened to the database at the connectionString
   // perform operations here.
}


Read this article[^] for more on connecting to a database through a SqlClient namespace. It would work with any ASP.NET application too.
 
Share this answer
 
Same Question , Answerd by many..
see here..
how to access sql database in asp.net[^]
 
Share this answer
 
using System.Data.SqlClient;
//Namespace

public partial class _Default : System.Web.UI.Page
{
SqlConnection con=new SqlConnection("server=.;uid=sa;pwd=admin;database=test");

// Server uid and pwd need to change according to your sql server login details

Sql Command cmd=new SqlCommand();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900