[英]SQL Server: invalid object name in query execution
I'm trying to execute an Insert statement, but keep getting a Invalid object name
error. 我正在尝试执行Insert语句,但不断收到
Invalid object name
错误。
Here's my code: 这是我的代码:
public string addNewComment(int userID, int pageID, string title, string comment)
{
string query = "INSERT INTO dbo.nokernok_kommentarer (userID, pageID, commentTitle, comment) " +
"VALUES ("+ userID +", "+ pageID +", '"+ title +"', '"+ comment +"')";
adapter.InsertCommand = new SqlCommand(query, connection);
//ExecuteNonQuery retuens number of rows affected
int numRows = adapter.InsertCommand.ExecuteNonQuery();
return numRows.ToString();
}
And here is my error message: 这是我的错误信息:
System.Data.SqlClient.SqlException: Invalid object name 'dbo.nokernok_kommentarer'.
System.Data.SqlClient.SqlException:无效的对象名称'dbo.nokernok_kommentarer'。 at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at development.DAL.nokernokDAL.addNewComment(Int32 userID, Int32 pageID, String title, String comment) in C:\\Inetpub\\wwwroot\\naaf\\DAL\\nokernok.cs:line 49
System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection)处于系统的System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)的System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection) System.Data.SqlClient.SqlCommand的System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName,Boolean async)处于.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)。 System.Data.SqlClient.SqlCommand.ExecuteNonQuery()中的InternalExecuteNonQuery(DbAsyncResult结果,String methodName,Boolean sendToPipe)位于C:\\ Inetpub \\中的development.DAL.nokernokDAL.addNewComment(Int32 userID,Int32 pageID,String title,String comment) wwwroot \\ naaf \\ DAL \\ nokernok.cs:第49行
Can anyone help me figure out why I get this error? 任何人都可以帮我弄清楚为什么我会收到这个错误?
UPDATE UPDATE
I should be using the correct database, because the following query works: 我应该使用正确的数据库,因为以下查询有效:
public DataSet getSchools(string countyCode)
{
DataSet ds = new DataSet();
string query = "SELECT * FROM nokernok_skoler WHERE kommunekode LIKE '" + countyCode.Substring(0, 2) + "%' ORDER BY enhetsnavn";
adapter.SelectCommand = new SqlCommand(query, connection);
adapter.Fill(ds);
return ds;
}
My connection string looks like this: 我的连接字符串如下所示:
SqlConnection connection = new SqlConnection();
SqlDataAdapter adapter = new SqlDataAdapter();
// class constructor
public nokernokDAL()
{
connection.ConnectionString = EPiServer.Global.EPConfig["EPsConnection"].ToString();
connection.Open();
}
You're probably in the wrong database. 你可能在错误的数据库中。 Include an initial catalog in your connection string:
在连接字符串中包含初始目录:
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername; ...
^^^^^^^^^^^^^^^^^^^^^^^^^^
Or specify a three part name: 或者指定一个三部分名称:
INSERT INTO myDataBase.dbo.nokernok_kommentarer
^^^^^^^^^^
From the error message, it would appear that the table dbo.nokernok_kommentarer
doesn't exist in your database, or it isn't a table and is thus not updatable. 从错误消息中可以看出,表
dbo.nokernok_kommentarer
在您的数据库中不存在,或者它不是表,因此不可更新。
Have you checked that: 你检查过:
dbo.
dbo.
and not somethingElse.
somethingElse.
? dbo.nokernok_kommentarer
exists? dbo.nokernok_kommentarer
存在吗? If you copy the SQL out from your code and run it in something like SQL Server Management Studio, does it work without error there? 如果从代码中复制出SQL并在SQL Server Management Studio中运行它,那么它是否正常运行?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.