0

First I would like to say that I thought that my problem was easily solved but after a lot of trying and searching I am not making any progress (I am also a complete beginner when it comes to databases).

I made a database with Microsoft SQL Server and called it UltimatePokerDB. I added a table MyTable I added two columns sevenKeys and sevenValues

this is visible in Visual Studio Image of my Server Explorer in visual studio

and makes this SQL code

CREATE TABLE MyTable
(
    [sevenKeys] BINARY(56) NOT NULL PRIMARY KEY, 
    [SevenValues] BINARY(48) NOT NULL
)

I have the following code written in my Main method

            string provider = "System.Data.SqlClient";
            string connectionstring = "Server=localhost;Database=master;Trusted_Connection=True;";
            byte[] one = new byte[7];
            byte[] two = new byte[6];

            DbProviderFactory factory = DbProviderFactories.GetFactory(provider);
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                string sql = "insert into MyTable (sevenKeys, sevenValues) values(@first,@last)";
                cnn.Open();
                using (SqlCommand cmd = new SqlCommand(sql, cnn))
                {
                    cmd.Parameters.AddWithValue("@first", one);
                    cmd.Parameters.AddWithValue("@last", two);
                    cmd.ExecuteNonQuery();
                    Console.WriteLine("row inserted");
                }
            }

I get an error at cmd.ExecuteNonQuery() and it says: System.Data.SqlClient.SqlException: 'Invalid object name 'MyTable'.'

Basically Im giving it the wrong name but I have no clue what it wants me to do...

1
  • You are not quering the correct database. You are on the master, and the table MyTable doesn't exists there. Commented Mar 1, 2019 at 16:13

1 Answer 1

5

You've specified in your connection string to use the Database master. You should probably be using the database your table is in, which is probably not master. In fact it looks like it should be UltimatePokerDB from your screenshot.

Sign up to request clarification or add additional context in comments.

1 Comment

i changed my connection string and it now indeed does work, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.