【发布时间】:2011-03-19 11:16:17
【问题描述】:
我正在尝试执行插入语句,但不断收到 Invalid object name 错误。
这是我的代码:
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(TdsParserStateObject stateObj) 在 System.Data.SqlClient.TdsParser.Run(运行行为 runBehavior,SqlCommand cmdHandler, SqlDataReader 数据流, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 在 System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(字符串 方法名,布尔异步)在 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult 结果,字符串方法名,布尔值 sendToPipe) 在 System.Data.SqlClient.SqlCommand.ExecuteNonQuery() 在 development.DAL.nokernokDAL.addNewComment(Int32 userID,Int32 pageID,字符串标题, 字符串注释)在 C:\Inetpub\wwwroot\naaf\DAL\nokernok.cs:line 49
谁能帮我弄清楚为什么会出现这个错误?
更新
我应该使用正确的数据库,因为以下查询有效:
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();
}
【问题讨论】:
标签: c# sql-server sql-server-2005