如何在消息框中显示sql查询打印的值
我想打印 SQL Server 返回的值。
If NOT Exists(SELECT * FROM ItemList WHERE ItemName='txtItemNama')
BEGIN
INSERT INTO ItemList (ItemName) VALUES('txtItemNamea')
END
ELSE
BEGIN
Print 'Duplicate'
END
此查询将返回受影响的行数或 Duplicate
我想在 C# 的 MessageBox.Show()
中使用此 Duplicate
string query1 = "If NOT Exists(SELECT * FROM ItemList WHERE ItemName='txtItemName') BEGIN INSERT INTO ItemList (ItemName) VALUES('txtItemName') END ELSE BEGIN Print 'Duplicate' END";
SqlCommand cmd = new SqlCommand(query1, conn);
SqlDataReader dr;
conn.Open();
dr=cmd.ExecuteReader();
conn.Close();
MessageBox.Show(dr);
我不知道如何使用 < code>dr 来执行此操作。请帮我在这里打印副本
MessageBox.Show(dr);
我需要在这里做什么?
I want to print a value that is returned by SQL Server.
If NOT Exists(SELECT * FROM ItemList WHERE ItemName='txtItemNama')
BEGIN
INSERT INTO ItemList (ItemName) VALUES('txtItemNamea')
END
ELSE
BEGIN
Print 'Duplicate'
END
This query will either return me either number of rows affected or Duplicate
I want to use this Duplicate in C# in MessageBox.Show()
string query1 = "If NOT Exists(SELECT * FROM ItemList WHERE ItemName='txtItemName') BEGIN INSERT INTO ItemList (ItemName) VALUES('txtItemName') END ELSE BEGIN Print 'Duplicate' END";
SqlCommand cmd = new SqlCommand(query1, conn);
SqlDataReader dr;
conn.Open();
dr=cmd.ExecuteReader();
conn.Close();
MessageBox.Show(dr);
I don't know how to use dr
to do this. Please help me out to print Duplicate here
MessageBox.Show(dr);
What do I need to do here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
晚了几年,但您应该能够通过将事件处理程序附加到 SqlConnection 对象上的 InfoMessage 事件 -
但只有在无法(例如出于某种原因) 使用此线程中提到的替代方案。
a few years late but you should be able to retrieve print/info text (like originally asked) by attaching an eventhandler to the InfoMessage event on the SqlConnection object -
But only do this if you can't (for some reason) use the alternatives mentioned in this thread.
使用 ADO.NET,有四个选项可用于从 SQL 查询返回信息:
这些都是从命令对象调用的方法。
给猫剥皮有很多不同的方法,您可以使用输出参数,可以使用 ExecuteScalar,可以使用返回值,也可以使用虚拟记录集。
您应该能够在 C# 中执行类似以下操作来从查询中获取返回值。
上面的代码捕获您可以根据需要设置的返回值,可能使用 0 表示存在,使用 1 表示不存在。
然而,上述建议有一点需要注意 - 我通常要么使用像 Linq-to-SQL 或 NHibernate 这样的 ORM,要么使用存储过程。我发现内联 SQL 相当麻烦。因此,虽然我的答案总体上应该是合理的,但您可能需要处理一些细节才能使其完全按照您的需要工作。
Using ADO.NET there are four options for returning information from an SQL query:
These are all methods called from your command object.
There are lots of different ways to skin a cat, you can use output parameters, you can use ExecuteScalar, you can use return values or you can use dummy recordsets.
You should be able to something like the following in your C# to get the return value from a query
The code above the captures the return value that you can set as you need, perhaps with a 0 for exists and a 1 for not exists.
There is a bit caveat with the above advice however - I usually either use an ORM like Linq-to-SQL or NHibernate, or I use Stored Procedures. I find inline SQL quite cumbersome. So, while my answer should be sound in general, you will probably need to work through some details to get it working exactly as you need.