单个命令中的多个插入语句

时间:2018-05-25 13:53:13

标签: c# sql-server-2012

我正在努力处理我的代码。当我尝试一次执行insert语句和存储过程时,存储过程的文本框值在存储到数据库时显示为空。但是当我只执行存储过程时,textbox值有一个数据并存储到数据库。

我错过了什么?这是我的代码:

private void recorduserlog()
        {           
            sqlcon.Open();
            cmd = new SqlCommand();
            cmd.CommandText = "userlogs";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = sqlcon;
            cmd.Parameters.Add(new SqlParameter("@id", Main.userid));
            cmd.Parameters.Add(new SqlParameter("@fullname", Main.passname));
            cmd.Parameters.Add(new SqlParameter("@activitydetails", txtfullname.Text));
            cmd.Parameters.Add(new SqlParameter("@userform", 1));
            cmd.Parameters.Add(new SqlParameter("@datelog", DateTime.Now.ToString("M/d/yyyy hh:mm:ss")));
            cmd.ExecuteNonQuery();
            sqlcon.Close();
        }

private void btnsave_Click(object sender, EventArgs e)
    {
       if (txtusername.Text != "" && txtaccesscode.Text != "" && txtfullname.Text != "" && cmbaccessleve.Text != "")  //validating the fields whether the fields or empty or not  
            {
                if (txtaccesscode.Text.ToString().Trim().ToLower() == txtconfirm.Text.ToString().Trim().ToLower()) //validating Password textbox and confirm password textbox is match or unmatch    
                {
                    string UserName = txtusername.Text;
                    string Password = adduser.Encrypt(txtaccesscode.Text.ToString());   // Passing the Password to Encrypt method and the method will return encrypted string and stored in Password variable.  
                    SqlConnection conn = new SqlConnection(Properties.Settings.Default.myconnectionstring);
                    sqlcon.Open();
                    SqlCommand cmd2 = new SqlCommand("insert into endusers(usern,passw,fullname,accesslevel,stats)values('" + UserName + "','" + Password + "','" + txtfullname.Text + "','" + ssfapclass + "',1)", conn);
                    cmd2.ExecuteNonQuery();
                    sqlcon.Close();
                    sqlcon.Dispose();
                    MessageBox.Show("Successfully Created a User Account for '" + txtfullname.Text + "'.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                    recorduserlog();        
                }
                else
                {
                    MessageBox.Show("Access Code and Confirmation Code doesn't match!.. Please Check..", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);  //showing the error message if password and confirm password doesn't match  
                }
            }
            else
            {
                MessageBox.Show("Please fill all the fields!..", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);  //showing the error message if any fields is empty  
            }
        }
    }

另外,两个SQL查询使用相同的TEXTBOX值作为参数。

2 个答案:

答案 0 :(得分:0)

我认为这会关闭应用程序

this.Close();

如评论中所述,您未使用

SqlConnection conn = new SqlConnection(Properties.Settings.Default.myconnectionstring);

您应该使用using

答案 1 :(得分:0)

由于您要求澄清如何处理SqlConnection,我将尝试为您分解。

SqlConnection使用非托管资源(打开到数据库服务器的套接字连接)。垃圾收集器只释放内存,而不是非托管资源。因此,当垃圾收集器从内存中删除SqlConnection时,它可以保留悬空连接。随着时间的推移,这会耗尽与数据库服务器的连接数量,并导致难以诊断问题。

在.NET中,我们有一种处理非托管资源的模式。处理非托管资源的对象将实现IDisposable接口。任何IDisposable对象都应该仔细处理。 大部分时间(有一些例外)IDisposable对象应该在Message<?>块内处理,或在finally语句中创建。这些模式中的任何一个都将确保非托管资源得到妥善处理。

SqlConnection实现了IDisposable(因为它继承自System.Data.Common.DbConnection)。由于有一个专门的类来处理数据库交互是个好主意,这通常会给我们留下如下代码:

using
相关问题