hi guys, in one of my .NET applications I need to connect to localDB but the code I wrote originally was for sql express and now it's returning an error understandly. Does anybody know what's the equivalent of this for localDB please?

protected void submitData(object sender, EventArgs e)
    {
        hookUp = new SqlConnection("Server=localhost\\SqlExpress;Database=test4;" + "Integrated Security=True");
        strInsert = "INSERT INTO Overtime(Week,HrsWorked,Overtime,Comment) VALUES (@week,@hrsWrk,@ovt,@cmmt)";
        HrsWorked = Convert.ToDecimal(hrs.Value);
        Overtime = HrsWorked - WRKHRS;
        sqlCmd = new SqlCommand(strInsert, hookUp);
        sqlCmd.Parameters.Add("@week", txtStartDate.Text);
        sqlCmd.Parameters.Add("hrsWrk", HrsWorked);
        sqlCmd.Parameters.Add("@ovt", Overtime);
        sqlCmd.Parameters.Add("@cmmt", TextArea1.Value);
        hookUp.Open();
        sqlCmd.ExecuteNonQuery();
        hookUp.Close();
        txtStartDate.Text = "";
        hrs.Value = "";
        TextArea1.Value = "";
    }

I presume the difference will only be in this line:
hookUp = new SqlConnection("Server=localhost\\SqlExpress;Database=test4;" + "Integrated Security=True");

Assuming the above code worked for your sql express data source, the only thing you would have to change is the connection string. There is no need to change any other code.

The default connection string for local DB (for .Net4.0) would be,..

 "Server=(localdb)\\v11.0;Integrated Security=true;"

But.. It could be different if you created an instance different then the default.

ah OK, I found a few people ont he net saying that there was no needto escape the backslash, I'll update my code thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.