如何测试sql命令是否执行? [英] How to test whether the sql command executed or not ?

查看:55
本文介绍了如何测试sql命令是否执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我使用了以下代码:-

Hi,
I have used the following code:-

string sql = "insert into reg_table(name, gender, phone, country) values(''" + name + "'', ''" + gender + "'', " + phone + ", ''" + country + "'')";

            SqlCommand MyCmd = new SqlCommand(sql, conn);





现在,我想测试是否将数据插入数据库中.如果插入了数据,则会显示一个消息框,并显示数据已插入"或未插入" ... ??
有人可以帮忙吗?

谢谢与问候
Akiii





Now, i want to test if the data is inserted in the database or not. If the data is inserted then it will show me a messagebox and say "data inserted" or "not inserted"...??
can anybody help?

Thanks and Regards
Akiii

推荐答案

要添加到Sandeep的良好答案中,请将操作包装到try..catch block中.如果出现语法错误,触发引发的错误等情况,您将在客户端遇到异常,并且显然没有插入任何行...
To add to Sandeep''s good answer, wrap the operation into try..catch block. In case of syntax errors, trigger raised errors etc you will have an exception at client side and obviously then no row is inserted...


好吧,请使用ExecuteNonQuery执行命令.
ExecuteNonQuery:对于UPDATE,INSERT和DELETE语句,返回值是受命令影响的行数.
.

此处的详细信息: SqlCommand.ExecuteNonQuery方法 [ ^ ]

因此,如果要插入一行数据,它将返回1,然后根据该值根据您的需要显示消息.
Well, use ExecuteNonQuery to execute your command.
As, ExecuteNonQuery: For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command.

Details here: SqlCommand.ExecuteNonQuery Method[^]

Thus, if one row of data would be inserted it would return 1 and then based on this value show the message as per your need.


这篇关于如何测试sql命令是否执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆