user133466
user133466

Reputation: 3415

What is the sql connection string I need to use to access localhost\SQLEXPRESS with Windows Authentication or SQL Authentication?

've installed SQL Express on my PC hoping to do some practice creating tables and then modifying them. I coded a webpage in Visual Studio to, basically, SELECT * from a table in the SQLEXPRESS, but I can never get the connection string to work. Please help

My connection string

"Data Source=localhost\SQLEXPRESS;Initial Catalog=test;User Id=xaa9-PC\xaa9;Password=abcd;"

Error Message:

Query is select * from tblCustomers where username='johndoe' error is Login failed for user 'x309-PC\x309'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Exception: Query is select * from tblCustomers where username='johndoe' error is Login failed for user 'x309-PC\x309'.

Upvotes: 20

Views: 104213

Answers (4)

shanu
shanu

Reputation: 1

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=SHANU-PC\SQLEXPRESS;Initial Catalog=Anusha;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {

        con.Open();
        SqlCommand cmd=new SqlCommand("select * from tbl_state",con);

        SqlDataAdapter da=new SqlDataAdapter(cmd);

        DataTable dt=new DataTable();
        da.Fill(dt);
            DropDownList1.DataSource = dt;
            DropDownList1.DataTextField = "sname";
            DropDownList1.DataValueField = "sid";
            DropDownList1.DataBind();

        con.Close();
        }

Upvotes: 0

Nicholas Murray
Nicholas Murray

Reputation: 13529

If you are placing your data connection string in a web.config file you specify your connection like below:

<connectionStrings>
<add name="NorthwindConnString" 
     connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True" 
     providerName="System.Data.SqlClient"/>
</connectionStrings>

but if you are hard coding within a c# based website you have to escape the '\' back slashes:

"Data Source=.\\\\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"

Even Scott Hanselman can forget this...

Upvotes: 0

Andomar
Andomar

Reputation: 238186

Try using Windows authentication:

Data Source=localhost\SQLEXPRESS;Initial Catalog=test;Integrated Security=SSPI;

Upvotes: 64

Darin Dimitrov
Darin Dimitrov

Reputation: 1039140

Try like this:

string connectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=test;User Id=x309;Password=abcd;";

Also make sure you have enabled SQL authentication.

Upvotes: 7

Related Questions