-1

I downloaded SQL Server 2012 Express. Using http://www.mssqltips.com/sqlservertip/2694/getting-started-with-sql-server-2012-express-localdb/. I created a local database. Now when I tried to connect to that database using Visual Studio 2010 it throws me error 26

Here is my code:

using(SqlConnection connectionString = new SqlConnection(@"Server=(localdb)\SQLEXPRESS;database=master;Trusted_Connection=True;")                          
{
    connectionString.Open(); //error 26
    sql = "insert into Main ([Cable Length1], [Cable Length2]) values(@3',@5')";

    using (SqlCommand cmd = new SqlCommand(sql, connectionString))
    {
        cmd.Parameters.AddWithValue("@3'", 1);
        cmd.Parameters.AddWithValue("@5'", 2);
        cmd.ExecuteNonQuery();
              }
}

Also, I am able to login through SQL Server Management Studio 2012. Can anyone tell me what am I missing?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Cook
  • 71
  • 1
  • 8
  • 2
    Is this a windows app or a web app? What is the error _message_ you get? – D Stanley Feb 05 '13 at 00:00
  • @Cook - What is the extra 't' for in this line: `sql = "insert into Main ([Cable Length1], [Cable Length2]) values(@3',@5't)";`? You have that in your query but not in your: `cmd.Parameters.AddWithValue("@5'", 2);` Remove the 't' and try again. – Brian Feb 05 '13 at 00:09
  • Guys this is windows app @Brian: Sorry about 't', but it throws me an error for open itself. – Cook Feb 05 '13 at 00:10
  • 1
    Also, you can declare your connection string like this (to make it less verbose): `using(SqlConnection connectionString = new SqlConnection(@"Server=.\SQLEXPRESS;database=master;Trusted_Connection=True;")`. – Brian Feb 05 '13 at 00:11
  • @Cook - See my comment above about how you are declaring your `SQL` `connection string`. – Brian Feb 05 '13 at 00:13
  • 1
    `MessageBox.Show("Row inserted !! ");` you're assuming that your code will work successfully ..? wrap that code around a `Try {}catch{}` inside your using never assume.. do proper error trapping / exception handling.. – MethodMan Feb 05 '13 at 00:26
  • thanks for the response. I created a local database using SQL server Management Studio. My server name is MyInstance and Database is LocalDBTest but I am still getting error 26 here is my updated code: – Cook Feb 05 '13 at 02:17
  • DataTable customerTable = new DataTable("Top5Customers"); DataColumn column = new DataColumn(); column.ColumnName = "id"; customerTable.Columns.Add(column); DataView view = new DataView(customerTable); DataRow row = customerTable.NewRow(); row["id"] = 2; customerTable.Rows.Add(row); SqlDataAdapter _dap = new SqlDataAdapter(_cmd); _con.Open(); – Cook Feb 05 '13 at 02:18
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Feb 05 '13 at 02:21
  • Please **do not** put code samples or sample data into comments - since you cannot format it, it's **extremely hard** to read it.... Instead: **update** your question by editing it to provide that additional information! Thank you. – marc_s Feb 05 '13 at 06:03

2 Answers2

0

Ignoring all the other problems1, you should make up your mind as to whether you're using SQL Server LocalDB or not. I am highly doubtful you've made a private LocalDB instance called SQLEXPRESS.

You likely want something along the lines of:

new SqlConnection(@"Server=(localdb)\v11.0;Trusted_Connection=True;")

Or

new SqlConnection(@"Server=.\SQLEXPRESS;Trusted_Connection=True;")

1 being the following:

  • Your SqlConnection is called connectionString.
  • Parameters with quotes in their names.
  • Using master and then trying to insert into a table called Main. System databases are for the system. You are not the system.

I'd expect to see something along the lines of:

using (var connection = new SqlConnection(@"Server=(localdb)\v11.0;Database=MyDatabaseName;Integrated Security=true;")
{
    connection.Open();

    using (var command = connection.CreateCommand())
    {
            command.CommandText = "INSERT INTO [Main] ([Cable Length1], [Cable Length2]) VALUES(@3, @5)";

            command.Parameters.AddWithValue("@3", 1);
            command.Parameters.AddWithValue("@5", 2);

            command.ExecuteNonQuery();
    }
}
ta.speot.is
  • 26,914
  • 8
  • 68
  • 96
0

I got the solution: Make sure you are connected to Database and when you launch sql management studio under server name-->select browse for more-->select database engine and then select your local database. You should see the Green Icon and then you do get connected. Thanks for the help guys!

Cook
  • 71
  • 1
  • 8