Problem while accessing the sqlserver2005 in c#
-
Hi friends I am facing a problem while accessing database sqlserver 2005 from command prompt. Here i am giving my code. I don't have any compilation errors, But runtime errors as follows.... Unhandled Exception: System.Data.SqlClient.SqlException: An error has occurred w hile establishing a connection to the server. When connecting to SQL Server 200 5, this failure may be caused by the fact that under the default settings SQL Se rver does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception my code is : using System; using System.Data; using System.Data.SqlClient; /// <summary> /// Demonstrates how to work with SqlConnection objects /// </summary> class SqlConnectionDemo { static void Main() { // 1. Instantiate the connection string strConnection = "server=localhost; Trusted_Connection=yes; timeout=120"; SqlConnection Conn = new SqlConnection(strConnection); SqlDataReader rdr = null; try { // 2. Open the connection if (Conn.State == ConnectionState.Closed) { Conn.Open(); } // 3. Pass the connection to a command object SqlCommand cmd = new SqlCommand("select * from Customers", Conn); // 4. Use the connection // get query results rdr = cmd.ExecuteReader(); // print the CustomerID of each record while (rdr.Read()) { Console.WriteLine(rdr[0]); } } finally { // close the reader if (rdr != null) { rdr.Close(); } // 5. Close the connection if (Conn != null) { Conn.Close(); } } } } Note: I am executing my program at cmd prompt. Help please...........
-
Hi friends I am facing a problem while accessing database sqlserver 2005 from command prompt. Here i am giving my code. I don't have any compilation errors, But runtime errors as follows.... Unhandled Exception: System.Data.SqlClient.SqlException: An error has occurred w hile establishing a connection to the server. When connecting to SQL Server 200 5, this failure may be caused by the fact that under the default settings SQL Se rver does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception my code is : using System; using System.Data; using System.Data.SqlClient; /// <summary> /// Demonstrates how to work with SqlConnection objects /// </summary> class SqlConnectionDemo { static void Main() { // 1. Instantiate the connection string strConnection = "server=localhost; Trusted_Connection=yes; timeout=120"; SqlConnection Conn = new SqlConnection(strConnection); SqlDataReader rdr = null; try { // 2. Open the connection if (Conn.State == ConnectionState.Closed) { Conn.Open(); } // 3. Pass the connection to a command object SqlCommand cmd = new SqlCommand("select * from Customers", Conn); // 4. Use the connection // get query results rdr = cmd.ExecuteReader(); // print the CustomerID of each record while (rdr.Read()) { Console.WriteLine(rdr[0]); } } finally { // close the reader if (rdr != null) { rdr.Close(); } // 5. Close the connection if (Conn != null) { Conn.Close(); } } } } Note: I am executing my program at cmd prompt. Help please...........
mohanallam wrote:
string strConnection = "server=localhost; Trusted_Connection=yes; timeout=120";
You haven't specified the database you want to connect to in your connection string. Visit ConnectionStrings.com[^] to see how to include this information in your connection string.
Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
-
Hi friends I am facing a problem while accessing database sqlserver 2005 from command prompt. Here i am giving my code. I don't have any compilation errors, But runtime errors as follows.... Unhandled Exception: System.Data.SqlClient.SqlException: An error has occurred w hile establishing a connection to the server. When connecting to SQL Server 200 5, this failure may be caused by the fact that under the default settings SQL Se rver does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception my code is : using System; using System.Data; using System.Data.SqlClient; /// <summary> /// Demonstrates how to work with SqlConnection objects /// </summary> class SqlConnectionDemo { static void Main() { // 1. Instantiate the connection string strConnection = "server=localhost; Trusted_Connection=yes; timeout=120"; SqlConnection Conn = new SqlConnection(strConnection); SqlDataReader rdr = null; try { // 2. Open the connection if (Conn.State == ConnectionState.Closed) { Conn.Open(); } // 3. Pass the connection to a command object SqlCommand cmd = new SqlCommand("select * from Customers", Conn); // 4. Use the connection // get query results rdr = cmd.ExecuteReader(); // print the CustomerID of each record while (rdr.Read()) { Console.WriteLine(rdr[0]); } } finally { // close the reader if (rdr != null) { rdr.Close(); } // 5. Close the connection if (Conn != null) { Conn.Close(); } } } } Note: I am executing my program at cmd prompt. Help please...........
you should specify the database ,user ,password in you contention string
my english is very bad!