3 Replies - 1773 Views - Last Post: 06 May 2009 - 01:48 AM Rate Topic: -----

#1 J0K3R  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 298
  • Joined: 19-December 08

DB Connection problem

Posted 05 May 2009 - 07:23 AM

Hi.

I am used to working with classic asp. Now I want to set my db connection in a class and call a seperate class to get the records from the db.

I get the following msg:

Exception Details: System.Data.SqlClient.SqlException: 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) :crazy:

Below is my code.

in my DbConnect.cs is

 
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DbConnect
/// </summary>
public class DbConnect
{
	SqlConnection myConnection;

	public DbConnect()
	{
		myConnection = new SqlConnection("Server=(local);" +
										"Database=database;" +
										"Trusted_Connection=True;" +
										"connection timeout=30");
	}

	public void Open()
	{
			myConnection.Open();
	}

	public SqlDataReader Select(string sqlQuery)
	{
		SqlCommand myCommand = new SqlCommand("Command String", myConnection);
		SqlDataReader myReader = myCommand.ExecuteReader();

		return myReader;
	}

	public void Close()
	{
		try
		{
			myConnection.Close();
		}
		catch (Exception e)
		{
			Console.WriteLine(e.ToString());
		}
	}
}



and then in my code is:

DbConnect myCon = new DbConnect();

		myCon.Open();

		SqlDataReader myReader = myCon.Select("Select * from tblUser");

		while (myReader.Read())
		{
			Console.WriteLine(myReader["pageTitle"].ToString());
			Console.WriteLine(myReader["pageContent"].ToString());
		}

		myCon.Close();



Is This A Good Question/Topic? 0
  • +

Replies To: DB Connection problem

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: DB Connection problem

Posted 05 May 2009 - 08:15 AM

That's a known issue, mostly on fresh installs of SQL Server. Take a look at this blog entry on this topic. Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#3 JhoeOendus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 27-April 09

Re: DB Connection problem

Posted 05 May 2009 - 08:37 AM

By default network connections are not allowed by SQL. You would need to enable Named Pipes I believe and make sure you allow for 'both local and network connections.' I just got done setting one of these guys up and had the same problem.
Was This Post Helpful? 0
  • +
  • -

#4 newProgram  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 160
  • Joined: 28-October 08

Re: DB Connection problem

Posted 06 May 2009 - 01:48 AM

View PostJ0K3R, on 5 May, 2009 - 06:23 AM, said:

Hi.

I am used to working with classic asp. Now I want to set my db connection in a class and call a seperate class to get the records from the db.

I get the following msg:

Exception Details: System.Data.SqlClient.SqlException: 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) :crazy:

Below is my code.

in my DbConnect.cs is

 
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DbConnect
/// </summary>
public class DbConnect
{
	SqlConnection myConnection;

	public DbConnect()
	{
		myConnection = new SqlConnection("Server=(local);" +
										"Database=database;" +
										"Trusted_Connection=True;" +
										"connection timeout=30");
	}

	public void Open()
	{
			myConnection.Open();
	}

	public SqlDataReader Select(string sqlQuery)
	{
		SqlCommand myCommand = new SqlCommand("Command String", myConnection);
		SqlDataReader myReader = myCommand.ExecuteReader();

		return myReader;
	}

	public void Close()
	{
		try
		{
			myConnection.Close();
		}
		catch (Exception e)
		{
			Console.WriteLine(e.ToString());
		}
	}
}



and then in my code is:

DbConnect myCon = new DbConnect();

		myCon.Open();

		SqlDataReader myReader = myCon.Select("Select * from tblUser");

		while (myReader.Read())
		{
			Console.WriteLine(myReader["pageTitle"].ToString());
			Console.WriteLine(myReader["pageContent"].ToString());
		}

		myCon.Close();


Try using this one:
Password=<password>;Persist Security Info=True;User ID=<username>;Initial Catalog=<database_name>;Data Souce=<server_name>" 


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1