没有显示成功进入其他部分 [英] not showing successfully going to the else part

查看:77
本文介绍了没有显示成功进入其他部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

try
            {
                SqlConnection con = new SqlConnection(@"Data Source=LENOVO-41B3F2CE\SQLEXPRESS;Initial Catalog=IMS;Integrated Security=True;Pooling=False");
                string query = "SELECT [user].* FROM [user] where u_name='" + txtusername.Text + "' and pwd='" + txtpassword.Text + "'";
                SqlCommand com = new SqlCommand(query, con);
               
               con.Open();
                if (com.ExecuteNonQuery() == 1)
                {
                    //Response.Redirect("WebForm1.aspx");
                    Label3.Text = "successfully";
                    
                }
                else
                {
                    Label3.Text = "not successfully";
                }
                con.Close();
            }
            catch (Exception ex)
            {
                Console.Write("" + ex.Message);
            }

推荐答案

在您的情况下,ExecuteNonQuery将始终返回-1。



来自 MSDN [ ^ ]:



对于UPDATE,INSERT和DELETE语句,返回值是受命令影响的行数。当插入或更新的表上存在触发器时,返回值包括插入或更新操作影响的行数以及受触发器或触发器影响的行数。 对于所有其他类型的语句,返回值为-1。如果发生回滚,则返回值也为-1。



您应该使用ExecuteScalar,并让它返回行计数。 ExecuteNonQuery通常用于你不太关心returnvalue的查询(除了上面文本中可能受影响的行数。
ExecuteNonQuery will always return -1 in your case.

From MSDN[^]:

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

You should use ExecuteScalar instead and have it return the row count. ExecuteNonQuery is normally used for queries where you don''t care much about the returnvalue (except maybe the number of affected rows as per above text.






您正在使用ExecuteNonQuery,请查看 http://blogs.msdn.com/b/spike/archive/2009/01/27/sqlcommand-executenonquery-returns-1-when- doing-insert-update-delete.aspx [ ^ ]



如果你在算一下用户符合您的条件然后您必须从查询中返回计数并使用ExecuteScaler来获得结果计数。



希望此信息可以帮助您。



祝你好运
Hi,

You are using ExecuteNonQuery incorrectly, please have a look at http://blogs.msdn.com/b/spike/archive/2009/01/27/sqlcommand-executenonquery-returns-1-when-doing-insert-update-delete.aspx[^]

If you are counting user that match your criteria then you have to return count from the query and use ExecuteScaler to get your result count.

hope this information helps you.

best luck


这篇关于没有显示成功进入其他部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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