| | |
Problem in Connecting to a database
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 13
Reputation:
Solved Threads: 0
Hi all,
i am trying some code examples given in a book. I am getting an error while connecting to the database.I have installed sqlexpress edition and trying to connect to system database "master".
Below is the error message:
"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"
And here is the code which i am trying.
pls help me...
Thanks
i am trying some code examples given in a book. I am getting an error while connecting to the database.I have installed sqlexpress edition and trying to connect to system database "master".
Below is the error message:
"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"
And here is the code which i am trying.
C# Syntax (Toggle Plain Text)
class Program { static void Main(string[] args) { // Create a connection string builder SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder(); // Define connection string attributes using three techniques csb.DataSource = "(local)"; csb.Add("Initial Catalog", "master"); csb["Integrated Security"] = true; // Output the connection string from the connection string builder Console.WriteLine("Connection string:\n{0}", csb.ConnectionString); // Create a connection string from the connection string builder SqlConnection connection = new SqlConnection(csb.ConnectionString); // Open and close the connection connection.Open(); Console.WriteLine("\nConnectionState = {0}", connection.State); connection.Close(); Console.WriteLine("ConnectionState = {0}", connection.State); Console.WriteLine("\nPress any key to continue."); Console.ReadKey(); } }
Thanks
Last edited by Ancient Dragon; Jun 24th, 2009 at 9:12 am. Reason: add code tags and removed color tags
Here is an example of connecting to an SQL Database, executing a query, and loading a DataTable:
By default SQL does not enable network protocols which results in the error message you are seeing. Here is the parameters to install SQL 2005 express (the password needs to be changed, i set it as p@ssw0rd):
SQL 2005 Express enable remote connections:
http://support.microsoft.com/kb/914277
SQL 2008 Express enable remote connections:
http://www.linglom.com/2009/03/28/en...-2008-express/
c# Syntax (Toggle Plain Text)
private void button1_Click(object sender, EventArgs e) { const string connStr = "Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;"; const string query = "Select * From Invoice Where InvNumber = @InvNumber"; using (DataTable dt = new DataTable()) { using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); using (SqlCommand cmd = new SqlCommand(query, conn)) { cmd.Parameters.Add(new SqlParameter("@InvNumber", 1100)); using (SqlDataReader dr = cmd.ExecuteReader()) { dt.Load(dr); } } conn.Close(); } System.Diagnostics.Debugger.Break(); } }
By default SQL does not enable network protocols which results in the error message you are seeing. Here is the parameters to install SQL 2005 express (the password needs to be changed, i set it as p@ssw0rd):
C# Syntax (Toggle Plain Text)
"setup.exe" /qb ADDLOCAL=SQL_Engine,SQL_Data_Files,SQL_Replication,Client_Components,Connectivity,SQL_SSMSEE INSTANCENAME=SqlExpress SAVESYSDB=1 SQLBROWSERAUTOSTART=1 SQLAUTOSTART=1 AGTAUTOSTART=1 SECURITYMODE=SQL SAPWD=p@ssw0rd DISABLENETWORKPROTOCOLS=0 ERRORREPORTING=0 SQMREPORTING=0 ADDUSERASADMIN=1 INSTALLSQLDIR="%ProgramFiles%\Microsoft SQL Server\"
SQL 2005 Express enable remote connections:
http://support.microsoft.com/kb/914277
SQL 2008 Express enable remote connections:
http://www.linglom.com/2009/03/28/en...-2008-express/
![]() |
Similar Threads
- Problem connecting to the database after changing the database server (ASP.NET)
- connecting database (PHP)
- Problem in connecting to database MySql (PHP)
- Error connecting to database (ASP.NET)
- Problem With database Connectivity (ASP.NET)
- struggling with connecting database to server (ASP.NET)
Other Threads in the C# Forum
- Previous Thread: returning database back to access.
- Next Thread: How to get a URI of an On memory file.
Views: 372 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development drawing encryption enum event excel file files form format ftp function gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view webbrowser windows winforms wpf xml






