Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connecting to SQL Database ???

Status
Not open for further replies.

anthonymel

Technical User
Jan 4, 2005
76
US
I searched the internet far and wide for a sample web page of code that shows how to connect to a SQL server for querying. With php it seems everything is straight forward, but with ASP.NET I have no clue where to begin. Everything is tuned for VB.NET or C#.NET. I'm not building a commplex web app. I just need to query my databse through a web page.

So if anyone knows of a site or could throw out some code here on how to connect, through DSN, to the sample northwidn db in SQL Server 2000 and do a simple query. That would be amazing. Once I see I can do this I will head straight to the book store.

Thanks

Anthony
 
Check out the FAQ's: faq855-5662

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
you have to use System.Data.SqlClient namespalce
under this you have sqlconnection sqlcommand objects
Typical code looks as below
in <appSettings> tag of web.config file
<appSettings>

<!-- Database connection string
<add key="constring" value="Data Source=servername;uid=userid;pwd=
password;Initial Catalog=dbname"/>
</appSettings>

SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["constring"]);
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@param1",SqlDbType.VarChar,20);
cmd.Parameters.Add("@param2",SqlDbType.Int);
cmd.Parameters["@param1"].Value =val1
cmd.Parameters["@param2"].Value = val2;
cmd.CommandText = "spname";
con.Open();
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top