문제

I'm currently trying to develop an application that allows you to track your spending as part of my class. However I run into the error "Invalid object name 'dbo.AccTransactions"

My Windows Form Code:

    string command = "Insert INTO dbo.AccTransactions (id, transact_id, payee, dateof, amount, category)"
                            + "Values (@id, @transact_id, @payee, @dateof, @amount, @category)";
        SqlCommand cmd = new SqlCommand(command, con);
        cmd.Parameters.AddWithValue("@id", 1);
        cmd.Parameters.AddWithValue("@transact_id", 2);
        cmd.Parameters.AddWithValue("@payee", payeeTextBox.Text);
        cmd.Parameters.AddWithValue("@dateof", DateTime.Today);
        cmd.Parameters.AddWithValue("@amount", Convert.ToDecimal(amountTextBox.Text));
        cmd.Parameters.AddWithValue("@category", categoryTextBox.Text);

        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

My Connection String:

     SqlConnection con = new SqlConnection(@"Data Source=IVY\SQLEXPRESS;Initial Catalog=BudgetTracker;Integrated Security=SSPI;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");

My database has the table "dbo.AccTransactions" and it performs perfectly in SSMS.

Any assistance would be great!

도움이 되었습니까?

해결책

I guess you are missing database name in the connection string.

SqlConnection con = new SqlConnection(@"Data Source=IVY\SQLEXPRESS;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");

try it by putting the name of your database in the following string.

 SqlConnection con = new SqlConnection(@"Data Source=IVY\SQLEXPRESS; Initial Catalog=YOUR_DATABASE_NAME;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");

다른 팁

Please check that user who is running your program has an access to that table. Most probably you need to set your user as dbo since you accessing the table as dbo.AccTransactions.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top