简体   繁体   English

在SQL Server 2008上运行查询时,asp.net无效的对象名称错误

[英]Asp.net invalid object name error when running query on SQL Server 2008

I am trying to run a query, like the following: 我正在尝试运行查询,如下所示:

SqlCommand putuser = new SqlCommand("INSERT INTO MainDB(NumberofEmployees) VALUES(4)", cn);

putuser.ExecuteNonQuery();

I get an Invalid object name error for the table named MainDB. 我收到名为MainDB的表的无效对象名称错误。 I have checked the following: 我检查了以下内容:

  1. The query works when running it in the Database. 在数据库中运行查询时,该查询有效。
  2. I am connected to the correct database. 我已连接到正确的数据库。
  3. The Database does have a table with the name. 数据库确实有一个带有名称的表。

I am using Forms authentication. 我正在使用表单身份验证。 How do I make it so that anyone who access the application can make this insert to the Database. 我如何做到这一点,以便任何访问该应用程序的人都可以将此插入到数据库中。 The insert is initially occurring after a user is created. 插入最初是在创建用户之后发生的。

Are you using a connection string with Windows authentication or SQL Server authentication? 您是否使用Windows身份验证或SQL Server身份验证的连接字符串?

If you are using SQL Server authentication, use: 如果使用的是SQL Server身份验证,请使用:

<add name="ConnectionString" 
     connectionString="Data Source=127.0.0.1; Initial Catalog=DataBase;Persist Security Info=True;User ID=user; Password=password

then you need to make sure the user has access to the data base and schema that you are using. 那么您需要确保用户有权访问您正在使用的数据库和架构。

If you are using Windows authentication, the use 如果您使用Windows身份验证,则使用

<add name="ConnectionString" 
     connectionString="Data Source=127.0.0.1; Initial Catalog=DataBase;Trusted_Connection=Yes;Persist Security Info=True;"/>

then you need to make sure that the user you are using in your App_Pool (the windows account that runs the application) is added as a login/user on the data base and has the rights to run queries. 那么您需要确保您在App_Pool中使用的用户(运行该应用程序的Windows帐户)被添加为数据库中的登录名/用户,并且具有运行查询的权限。

EDIT 编辑

You can use SQL Server Management Studio to do this, step by step instructions can be found here and here 您可以使用SQL Server Management Studio来执行此操作,可在此处此处找到分步说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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