为什么我插入表行的 SQL 查询不能正常工作? [英] Why isn't my SQL query to insert a table row working properly?
问题描述
我对 C# 和 SQL 还很陌生,所以这可能是一个很容易回答的问题.
I am fairly new to C# and SQL, so this may be a very easy question to answer.
我正在尝试通过 C# 代码向表 (EventList
) 添加一行.我打开了我的 SqlConnection
没有任何问题,我知道我连接到正确的数据库,因为一些早期的代码正在查询其中一个表中的行并且它返回正确的键.
I am trying to add a row to a table (EventList
) through C# code. I have opened my SqlConnection
without any issues, and I know I am connected to the correct database as some earlier code is querying for rows in one of the tables and it's returning the correct keys.
将行插入到表中的 SQL 查询是这样的:
The SQL query to insert the row into the table is like this:
sqlCmd.CommandText =
"insert into EventList values ('" +
eventListIdentifier + "','" +
eventId.ToString() + "')";
sqlCmd.ExecuteNonQuery();
我正在使用 SQL Server Management Studio Express 查看我的数据库中的表.运行上述查询后,我右键单击 EventList
表,然后单击 Open Table
.
I am using SQL Server Management Studio Express to view the tables in my database. After running the above query, I right-click on the EventList
table and click Open Table
.
我没有看到根据上述调用添加的新行.任何想法我可能做错了什么?
I am not seeing the new row added based on the above call. Any ideas what I may be doing wrong?
更新 1
我插入的数据类型是:
eventListIdentifier (varchar(100), null)
eventId (varchar(8000), null)
我在 SSMS 中手动创建了相同的查询,如下所示:
I manually created the same query in SSMS like this:
insert into EventList(eventListIdentifier, eventId ) values('test', 'blah')
并说以下内容:
(1 row(s) affected)
然而,当我右键单击它并打开它时,没有向表格中添加任何行.
However no row has been added to the table when I right-click on it and open it.
更新 2
@billinkc 要求的 System.Console.WriteLine(sqlCmd.CommandText);
的输出:
Output of System.Console.WriteLine(sqlCmd.CommandText);
as requested by @billinkc:
insert into EventList(eventListIdentifier, eventId) values ('7/09/2011 10:43:55 AM','7')
推荐答案
由于我在评论中指出的缓存/刷新错误,请不要使用 Open Table.只需在查询窗口中重新运行相同的查询:
Don't use Open Table due to the cache/refresh bug I pointed out in my comment. Just re-run the same query in a query window:
SELECT * FROM dbo.EventList
-- WHERE EventId = <EventId>
;
这篇关于为什么我插入表行的 SQL 查询不能正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!