我整数cmd.ExecuteNonQuery值会-1 0 [英] cmd.ExecuteNonQuery value of i integer is going -1 from 0

查看:116
本文介绍了我整数cmd.ExecuteNonQuery值会-1 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我在数据库中创建表的动态。
用户输入名字作为他的愿望,选择语言单选按钮。
所以,问题是在执行cmd.ExecuteNonQuery值后的我整型将会-1从0,并显示该表想不出来创建,但是当我去到它的数据库已成功创建。请让我知道我做错了。 thanx提前!

 保护无效btnpaper_Click(对象发件人,EventArgs的发送)
    {
        尝试
        {
                康涅狄格州字符串= ConfigurationManager.ConnectionStrings [sqlconn]的ConnectionString。
                SqlConnection的CON =新的SqlConnection(康涅狄格州);
                con.Open();
                的char [] =改编新的char [] {'N','G','L','我','S','H'};
                字符串str =CREATE TABLE+ Label1.Text.Trim()+
                             txtpaperset.Text.Trim()+ rbtnEng.Text.TrimEnd(ARR)+
                             (+quesNo诠释NOT NULL PRIMARY KEY,+
                             问题VARCHAR(1000)NOT NULL,+
                             安莎VARCHAR(500)NOT NULL,+
                             ANSB VARCHAR(500)NOT NULL,+
                             ANSC VARCHAR(500)NOT NULL,+
                             ANSD VARCHAR(500)NOT NULL,+
                             rightAns VARCHAR(50)NOT NULL+);
                CMD的SqlCommand =新的SqlCommand(STR,CON);
                INT I = cmd.ExecuteNonQuery();
                如果(I 0)
                {
                    lblerrormsg.Visible = TRUE;
                    con.Close();
                }
                其他
                {
                    lblerrormsg.Text =表未创建请其他名称再试!;
                    con.Close();
                }        }
        赶上(System.Exception的excep)
        {
            MessageBox.Show(excep.Message);
        }
    }


解决方案

从MSDN备注注意到关于<一个href=\"http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx\">SqlCommand.ExecuteNonQuery


  

有关UPDATE,INSERT和DELETE语句,返回值是
  该命令影响的行数。当一个触发存在于一
  被插入或更新表,返回值包括数
  受插入或更新操作和数量两排的
  的受触发器或触发器行。对于所有其他类型的
  语句,返回值为-1。如果回退时,返回
  值也是-1。


Here i create table in database dynamically. User enters name as his wish and selects language radiobutton. So problem is after executing cmd.ExecuteNonQuery value of i integer is going -1 from 0. And shows that table couldnt be created but when i go to database its already created successfully. Please let me know where i am doing wrong. Thanx in Advance !!

protected void btnpaper_Click(object sender, EventArgs e)
    {
        try
        {                
                string conn = ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString;
                SqlConnection con = new SqlConnection(conn);
                con.Open();
                char[] arr = new char[] {'n','g','l','i','s','h'};
                string str = "CREATE TABLE " + Label1.Text.Trim() + 
                             txtpaperset.Text.Trim()+ rbtnEng.Text.TrimEnd(arr) +
                             "(" + "quesNo int NOT NULL PRIMARY KEY, " + 
                             "question varchar(1000) NOT NULL," + 
                             "ansA varchar(500) NOT NULL, " + 
                             "ansB varchar(500) NOT NULL, " + 
                             "ansC varchar(500) NOT NULL, " + 
                             "ansD varchar(500) NOT NULL, " + 
                             "rightAns varchar(50) NOT NULL " + ")";                    
                SqlCommand cmd = new SqlCommand(str, con);
                int i = cmd.ExecuteNonQuery();
                if (i > 0)
                {
                    lblerrormsg.Visible = true;
                    con.Close();
                }
                else
                {
                    lblerrormsg.Text = "Table Not Created Please Try with Different Name!";                        
                    con.Close();
                }                

        }
        catch (System.Exception excep)
        {
            MessageBox.Show(excep.Message);
        }      
    }

解决方案

Takes from MSDN Remarks on SqlCommand.ExecuteNonQuery

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.

这篇关于我整数cmd.ExecuteNonQuery值会-1 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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