Jericho Javier
Jericho Javier

Reputation: 1

ASP to SQL Server connection basics?

I apologize in advance if the question is too noobish. I am new to the ASP and SQL server world (i've been using PHP and MySQL up to this point) (I've read other topics here, but none seemed to give me a clear answer)

I want to connect my ASP website to my SQL database (using sql server 2005 currently), how would i do that? I've been trying to use numerous connection strings, but everything seems confusing to me right now (too many varieties)

Also, how do i execute queries after making a succesful connection?

I believe an answer to those two would get me started on, i hope i'm not asking for much or something. Thanks in advance!

Upvotes: 0

Views: 127

Answers (2)

Rosemol Francis
Rosemol Francis

Reputation: 25

To add sql connection to a asp.net webpage, First we have to get the connection string. For that open server explorer ->Data connections ->add connection. Give the servername and database name in the given popup box. After adding the connection take the property window of added connection, From there we will get the connection string. After that write the following code:

     using System.Data;
     using System.Data.SqlClient;



     public void dbconnection
     {
     SqlConnection con;
     con = new SqlConnection("connectionstring");
     con.Open();
     SqlCommand cmd=new SqlCommand("Your sql query",con);
     cmd.ExecuteNonQuery();
     con.close();
     }

for insert,update and delete queries we use ExecuteNonQuery(). for select queries we use

  SqlDataReader dr = cmd.ExecuteReader();

Upvotes: 0

ChaCha
ChaCha

Reputation: 31

Dim objDbCon
Dim dataCount
Dim sqlQuery

Set objDbCon = Server.CreateObject("ADODB.Connection")

'Change the parameters with your own environment'
objDbCon.ConnectionString = "Provider=SQLOLEDB; Data Source=120.120.120.120; Initial Catalog=Database name; User Id=user1; Password=1234;"

objDbCon.Open

'Put sql script which you want to get result set'
sqlQuery = "SELECT COUNT(*) AS CNT FROM TABLE_NAME"

'This is how you execute sql script and bind the result set to dataset object'
Set Rs = objDbCon.Execute(sqlQuery)

dataCount = Rs("CNT")

Rs.Close

Upvotes: 3

Related Questions