简体   繁体   中英

Invalid object name “CAccounts”

I keep getting this error:

Invalid object name "CAccounts".

and the code I have is:

System.Threading.Thread thread = new System.Threading.Thread(() =>
{
    // Set ConnectionString.
    String sConSg =
    "CONNSTRING HERE";

    using (SqlConnection connection = new SqlConnection(sConSg))
    {
        try
        {
            connection.Open();
        }
        catch (Exception exception)
        {
            MessageBox.Show(stat.Text = exception.Message);
        }

        try
        {
            SqlDataReader slrr = null;
            SqlCommand command = new SqlCommand("SELECT ActivationCode FROM CAccounts WHERE ActivationCode = " +
                "'" + _activationcode.Text + "'", connection);
            slrr = command.ExecuteReader();

            while (slrr.Read())
            {
                if (slrr["ActivationCode"].ToString() == _activationcode.Text)
                {
                    MessageBox.Show(slrr["ActivationCode"].ToString(), "AutoOptimise");

                }
                else
                {
                }
            }
        }
        catch (Exception exception)
        {
            MessageBox.Show(stat.Text = exception.Message);
        }
    }

});
thread.Start();

Can somebody please shed some light?

The table CAccounts you're referencing in your select clause probably doesn't exist in the database. Check that.

See a list of possibilities here:

http://web.archive.org/web/20150519073601/http://sqlserver2000.databases.aspfaq.com:80/why-do-i-get-object-could-not-be-found-or-invalid-object-name.html

I'd suggest these things:

  • check which schema the object CAccounts is under. Is it dbo or other? Does the user have permissions on that schema?

  • login to SQL Server via Management Studio. Use the credentials in the connection string that the code is using. Paste & run the SQL statement above.

  • use SQL Profiler to capture/verify ensure the SQL statement as it crosses to your SQL Server. Run THAT as an adhoc query against that SQL Server.

  • are there any funny DNS issues? Hosts files? Does this happen during debugging or on the app server?

  • is the database server name correct? ie localhost versus a named server. Try addressing by the IP address that you expect it to be run at, just for fun, both in your connection string, and via SSMS.

Instead of CAccounts, I had to label it DATABASENAME.dbo.CAccounts.

So it would be something like:

"SELECT * FROM DATABASENAME.db.CAccounts"

This might work:

SqlConnection con = new SqlConnection("Data Source=192.168.1.12;Initial Catalog=Ibrahim;User ID=sa;Password=1412;");
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into Ibrahim.EVC_Activation_Val (Date,Dealer,Transaction_ID,Invoice_ID,Mobile_Num,Quantity_LE) values('" + DateTimePicker1.Value.ToString("yyyy/mm/dd") + "','" + Txtbx1.Text + "','" + Txtbx2.Text + "','" + Txtbx3.Text + "','" + Txtbx4.Text + "','" + Txtbx5.Text  + "')",con);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Saved");
            con.Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM