C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Query a SQL Server Database in C# with ADO.NET


Posted Date: 06 Apr 2008    Resource Type: Code Snippets    Category: ADO.NET

Posted By: Raju.M       Member Level: Gold
Rating:     Points: 3



The code sample illustrates how to establish a connection with a database from .NET programs, and query the database.

The SqlConnection takes the ConnectionString as a parameter. Once the connection object is created we need to open it. The connection object can be used by a SqlCommand object that takes the query and connection as it's parameters for object creation.

The command can only be executed succesfully if both the connection is perfectly set up and query is valid.



SqlConnection con = new SqlConnection("Data Source=SERVER; Integrated Security=SSPI;persist security info=True;Initial Catalog=DBNAME;");
string empId=Request.QueryString["eid"].ToString();
SqlCommand cmd=new SqlCommand(string.Format("SELECT EmpName FROM empTable WHERE empId='{0}'",empId),con);
con.Open();
string empName = Convert.ToString(cmd.ExecuteNonQuery());
con.Close();




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Simple Query with ADO.NET  .  Connect to SQL Server Database with ADO.NET  .  Connect to Database in C#  .  ADO.NET with C#  .  Accessing SQL Server with ADO.NET  .  Accessing Databases with ADO.NET  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: A Class for Database Access in C#
Previous Resource: Connect a new Database
Return to Discussion Resource Index
Post New Resource
Category: ADO.NET


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

SPOC

Contact Us    Privacy Policy    Terms Of Use