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)
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();

New Topic/Question
Reply




MultiQuote





|