0

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

2
Contributors
2
Replies
8
Views
2 Years
Discussion Span
Last Post by Violet_82
0

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.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.