简体   繁体   中英

SQL table doesn't exist

I have a problem with ADO.NET query. I Create Database successfuly. There is only one table ( RegUsers ) in this database (I am just testing the work with ADO.NET). EDIT: Databese is based on Microsoft Azure

Create of a my table:

CREATE TABLE [dbo].[RegUsers] (
[Id]       INT          NOT NULL,
[Login]    VARCHAR (50) NOT NULL,
[Password]    VARCHAR (50) NOT NULL,
[Name]    VARCHAR (50) NOT NULL,
[Surname] VARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC));

I create SqlConnecion and successfully connect to the database (I hope that successfully if I can Open the connection) and then I'd like to INSERT data into this table with this:

SqlConnectionStringBuilder csBuilder;
csBuilder = new SqlConnectionStringBuilder();
csBuilder.DataSource = "********.database.windows.net";
csBuilder.Encrypt = true;
csBuilder.TrustServerCertificate = false;
csBuilder.UserID = "************".ToString();
csBuilder.Password = "********".ToString();
csBuilder.ConnectTimeout = 30


SqlConnection con = new SqlConnection(csBuilder.ToString());

con.Open();

string PlneniDaty =
@"INSERT INTO [dbo].[RegUsers] (Login,Password,Name,Surname)"+
@" VALUES ('MyLogin','MyPassword','Pavel','Novak')";

SqlCommand NaplDaty = new SqlCommand(PlneniDaty, con);
NaplDaty.ExecuteNonQuery();   

con.Close();

Whenever I execute this command It display Error:

Invalid object name 'dbo.RegUsers'

(and yes the table was successfully created I can see it in SQL Server object Explorer)

Where is the problem?

Your entire code looks good.you can post your connectionString. Following things you need to check.

  1. Connection String ( Data Source Name , Database Name )
  2. Schema of your table.

If I understand correctly, you're executing a insert query in a database dbo.RegUsers and it's giving the error Invalid object name 'dbo.RegUsers'? That simply means that table cannot find an object called "RegUsers ". There are several possible reasons for this:

The object doesn't exist, possibly because the schema and/or database don't exist The object exists, but the database is case-sensitive and some part of the name doesn't match the name in your code

You'll need to investigate more to find out what the cause is in your case, but as a complete guess, your production server has both the RegUsers and databases?

Finally, when posting questions please always include your SQL Server version (2000/2005/2008) and edition (Express, Standard, Enterprise); they can be very important when talking about schemas and permissions, because features and behaviour can be different.

据我在您的创建脚本中看到的那样,表的名称是RegUzivatele,而您试图插入到表名RegUsers时,这当然是不存在的。

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