SQL Server:查询执行中的对象名称无效 [英] SQL Server: invalid object name in query execution
问题描述
我正在尝试执行Insert语句,但始终收到无效的对象名称
错误.
I'm trying to execute an Insert statement, but keep getting a Invalid object name
error.
这是我的代码:
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();
}
这是我的错误消息:
System.Data.SqlClient.SqlException:无效的对象名称'dbo.nokernok_kommentarer'.在System.Data.SqlClient.SqlConnection.OnError(SqlException例外,布尔值breakConnection)位于System.Data.SqlClient.SqlInternalConnection.OnError(SqlException例外,布尔值breakConnection)位于System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObjectstateObj)在System.Data.SqlClient.TdsParser.Run(RunBehaviorrunBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSetbulkCopyHandler,TdsParserStateObjectstateObj)在System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(字符串methodName,布尔异步),位于System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult结果,字符串methodName,布尔值sendToPipe)System.Data.SqlClient.SqlCommand.ExecuteNonQuery()在development.DAL.nokernokDAL.addNewComment(Int32userID,Int32 pageID,字符串标题,字符串注释)中C:\ Inetpub \ wwwroot \ naaf \ DAL \ nokernok.cs:line49
System.Data.SqlClient.SqlException: Invalid object name '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
有人可以帮我弄清楚为什么我会收到此错误吗?
Can anyone help me figure out why I get this error?
更新
我应该使用正确的数据库,因为以下查询有效:
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;
}
我的连接字符串如下:
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
^^^^^^^^^^
这篇关于SQL Server:查询执行中的对象名称无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!