Click here to Skip to main content
15,885,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how to connect with database in asp.net...my sqlserver name is Zenith\sqlexpress i put this name for connection but show error always that line...please tell me solution for that problem or give me coding for that open the connection.....
Posted
Updated 16-May-11 5:51am
v2

Open Visual Studio.
From the menu, select "View...Server Explorer"
In the "Server Explorer" pane, browse the Data Connections.
Find your database.
Right click your database, and select "Properties"
In the "Properties" pane, find the line "Connection string"
Copy that into your program as the Connection string - you will probably want to store in a file later rather than hard coding, but get it working first.
 
Share this answer
 
Comments
ambarishtv 17-May-11 4:17am    
my 5
CS2011 18-May-11 5:59am    
Seeing the question I think this solution is best suited for him. 5+
To connect to SQL Server from C#.NET, you need to create a connection string such as below:

private SqlConnection connection;
private string connectionString =
@"Server=(local);Database=Embedding_SQL_Test;User ID=sa;Password=123";
connection = new SqlConnection( connectionString );

Next, you use the SqlConnection object created above to create a 'SqlCommand', as shown below:
SqlCommand cmd = new SqlCommand( "select * from Customer where CustomerID = @Cid", connection);

The SQL query shown here can be replaced by a SELECT, INSERT, UPDATE queries etc.

Next to execute the SQL queries in the database, you use the following methods:
ExecuteReader - to execute SELECT queries
ExecuteNonQuery - to execute INSERT, DELETE, UPDATE, and SET statements.

This is a very short description of how to connect to SQL Server database from C# and execute SQL queries in the database.
For details about the connection string, the methods and their parameters check the following link: ( http://www.shahriarnk.com/Shahriar-N-K-Research-Embedding-SQL-in-C-Sharp-Java.html[^] )
Here you will also find details about how to pass parameters to the SQL queries as well as calling stored procedures and much more.
 
Share this answer
 
Hi Idhaya,

Please Visit the below URL.
http://msdn.microsoft.com/en-us/library/ms178371.aspx[^]


Regards
Chaithu
 
Share this answer
 
v2
sqlconnection cn=new sqlconnection("server=yourcomputername;uid=yoursqlserveruserid;pwd=your password;database=dname");
 
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