Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi All,
I want to know how to execute queries against SQL Server database by using ADO.NET?
Thanks

[edit]Tidy, and remove unnecessary code block - OriginalGriff[/edit]
Posted
Updated 26-Nov-10 3:43am
v4

All you need to know is here:
Accessing Data with ADO.NET[^]
ADO.NET details[^]
 
Share this answer
 
Comments
Rajesh Anuhya 30-Nov-10 5:01am    
Good links
First, create an SqlConnection object.
Then, create an SqlCommand object.
Run a ExecuteRead on the command.
Process the results.

The complex bit is the connection, and that will depend on your connection string, db type, etc.

C#
sqlConnection sqlConn = new SqlConnection("Server=RIMROCK;Database=Northwind;Integrated Security=SSPI;");
SqlCommand sqlComm = new SqlCommand("SELECT userid,username FROM users ORDER BY username", sqlConn);
SqlDataReader r = sqlComm.ExecuteReader();
while ( r.Read() ) 
   {
   string username = (string)r["username"];
   int userID = (int)r["userid"];
   Debug.WriteLine(username + "(" + userID + ")");
   }
r.Close();
 
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