插入到MS SQL Server数据库 [英] Insert to MS SQL server database

查看:85
本文介绍了插入到MS SQL Server数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,这里有一个逻辑错误.想要将信息添加到数据库,它捕获重复的主键记录,但仍然仍然添加记录

以下是将信息添加到数据库的事件

Hi guys am having a logical error here. Want to add the informaion to database,It catches the duplicate primary key record but still it adds the record anyway

Below is the event for adding the information to database

private void btnInsert_Click(object sender, EventArgs e)
      {

          char placeholder = '-';
          string textStrng;
          string var = txtFName.Text;

          if (txtFName.Text=="")
          {
              MessageBox.Show("Please enter your name.",
             "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
              txtFName.Focus();
              txtFName.BackColor = Color.Moccasin;
          }
          else if (txtPassword.Text != txtRepassword.Text)
          {
              MessageBox.Show("Password missmatch",
              "Error Password", MessageBoxButtons.OK, MessageBoxIcon.Stop);
              txtPassword.Focus();
              txtPassword.BackColor = Color.Moccasin;
              txtRepassword.BackColor = Color.Moccasin;
          }
          else
          {

                   int sequence = 0;


                  try
                  {

                      newVar = var.Remove(3);
                      textStrng = newVar.ToUpper().ToString() + placeholder + 0 + sequence+1;
                      txtUserID.Text = textStrng.ToString();

                      users = new ClsUsers(Convert.ToString(txtFName.Text), Convert.ToString(txtLName.Text), Convert.ToString(txtUserID.Text),
                      Convert.ToString(txtEmail.Text), Convert.ToString(txtUsername.Text), Convert.ToString(txtPassword.Text),
                      Convert.ToString(txtRightsName.Text), Convert.ToString(txtDescription.Text));

                      bl.InsertUser(users);
                      sequence++;
                  }
                  catch (Exception exp)
                  {
                      MessageBox.Show(exp.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                      txtFName.Focus();
                      txtFName.BackColor = Color.Moccasin;
                  }


              MessageBox.Show("Added succsessfuly.\n\n" + "UserID: " + txtUserID.Text + "\nName: " + txtFName.Text + "\nSurname: " + txtLName.Text + "\nEmail: " + txtEmail.Text,
                  "Insert", MessageBoxButtons.OK, MessageBoxIcon.Information);

              clearTextBoxes();
              txtFName.Focus();

          }


      }



以下是插入方法



Below is the method to insert

#region Insert

       public void InsertUser(ClsUsers users)
       {
           using (SqlConnection conn = new SqlConnection(ConnString))
           {
               SqlCommand cmd = new SqlCommand("procInsertUserInfo", conn);
               cmd.CommandType = CommandType.StoredProcedure;

               cmd.Parameters.Add(new SqlParameter("@fName", SqlDbType.NVarChar, 20));
               cmd.Parameters["@fName"].Value = users.FName;

               cmd.Parameters.Add(new SqlParameter("@lname", SqlDbType.NVarChar, 50));
               cmd.Parameters["@lname"].Value = users.SName;

               cmd.Parameters.Add(new SqlParameter("@userID", SqlDbType.NVarChar, 7));
               cmd.Parameters["@userID"].Value = users.UserID;

               cmd.Parameters.Add(new SqlParameter("@email", SqlDbType.NVarChar, 50));
               cmd.Parameters["@email"].Value = users.Email;

               cmd.Parameters.Add(new SqlParameter("@username", SqlDbType.NVarChar, 20));
               cmd.Parameters["@username"].Value = users.Username;

               cmd.Parameters.Add(new SqlParameter("@password", SqlDbType.NVarChar, 25));
               cmd.Parameters["@password"].Value = users.Password;

               cmd.Parameters.Add(new SqlParameter("@name", SqlDbType.NVarChar, 20));
               cmd.Parameters["@name"].Value = users.RightName;

               cmd.Parameters.Add(new SqlParameter("@desc", SqlDbType.NVarChar, 100));
               cmd.Parameters["@desc"].Value = users.Desc;


               try
               {

                   conn.Open();
                   cmd.ExecuteNonQuery();
               }
               catch (Exception exp)
               {
                   throw new ApplicationException(exp.Message);
               }
           }//End using
       }//End Method

推荐答案

我不认为您的错误将是重复的主键错误-如果发生该错误,则无法将记录插入SQL Server数据库中.
使用调试点运行代码,也许您会找到错误的根本原因.
I dont think your error would be a duplicate primary key error - if that error occurs, there is no way a record could be inserted into an SQL Server database.

Run through your code using debug points and perhaps you will get to the root cause of the error.


private void button1_Click(object sender, EventArgs e)
        {


                        if (txtuname.Text.Equals(""))
            {
                MessageBox.Show("Please Enter User Name", "User Creation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtuname.Focus();
                return;
            }
            if (txtpword.Text.Equals(""))
            {
                MessageBox.Show("Please Enter Password", "User Creation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtpword.Focus();
                return;
            }
            if (txtcpassword.Text.Equals(""))
            {
                MessageBox.Show("Please Enter Confirm Password", "User Creation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtcpassword.Focus();
                return;
            }
            if (txtpword.Text != txtcpassword.Text)
            {
                MessageBox.Show("Please Check Confirm Password", "User Creation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtcpassword.Focus();
                return;
            }
            //if (cbentry.Checked == false && cbview.Checked == false && cbsearch.Checked == false && cbreport.Checked == false && cbadmin.Checked == false)
            //{
            //    MessageBox.Show("Please Set User Privilage", "User Creation", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    cbentry.Focus();
            //    return;
            //}
            sq = "select uname from admin where uname='" + txtuname.Text + "' ";
            cmd = new SqlCommand(sq, con);
            rd = cmd.ExecuteReader();
            if (rd.Read() == true)
            {
                rd.Close();
                MessageBox.Show("Already Exist This User", "User creation", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (rd.Read() != true)
            {
                rd.Close();
                sq = "insert into admin values('" + txtuname.Text + "','" + txtpword.Text + "')";
                cmd = new SqlCommand(sq, con);
                cmd.ExecuteNonQuery();
                MessageBox.Show("User Saved", "User craetion", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtuname.Focus();
                txtcpassword.Text = "";
                txtpword.Text = "";
                txtuname.Text = "";
            }

        }


这篇关于插入到MS SQL Server数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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