使用Windows窗体条目更新SQL表

本文关键字:更新 SQL Windows 窗体 使用 | 更新日期: 2023-09-27 18:21:48

我希望能够用Windows窗体中的条目更新我的SQL数据表,但我已经尝试了所有方法,但都不起作用。我一直有错误,尤其是"无法将字符串转换为十进制"。我有两张表格,一张是全体议员表格,另一张是附日期的经常付款纪录表格。

存款部分应该为每个成员加起来,并链接到成员表,这也是我无法实现的。

//NC-4 Create account.
    private void btnCreateAccount_Click(object sender, EventArgs e)
    {
        //NC-5 Set up and run stored procedure only if Customer Name is present.
        if (isCustomerName())
        {
            //NC-6 Create the connection.
            SqlConnection conn = new SqlConnection(connstr);

            //NC-7 Create a SqlCommand, and identify it as a stored procedure.
            SqlCommand cmdNewCustomer = new SqlCommand("dbo.epaNewCustomer", conn);
            cmdNewCustomer.CommandType = CommandType.StoredProcedure;

            //NC-8 Add input parameter from the stored procedure and specify what to use as its value.
            cmdNewCustomer.Parameters.Add(new SqlParameter("@titheID", SqlDbType.NChar, 10, "titheID"));
            cmdNewCustomer.Parameters["@titheID"].Value = txtTitheID.Text;
            this.txtMemberID.Text = Convert.ToString(parsedMembersID);
            cmdNewCustomer.Parameters.Add(new SqlParameter("@surName", SqlDbType.NVarChar, 50, "surName"));
            cmdNewCustomer.Parameters["@surName"].Value = txtSurname.Text;
            cmdNewCustomer.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar, 50, "Name"));
            cmdNewCustomer.Parameters["@Name"].Value = txtName.Text;
            cmdNewCustomer.Parameters.Add(new SqlParameter("@otherName", SqlDbType.NVarChar, 50, "otherName"));
            cmdNewCustomer.Parameters["@otherName"].Value = txtOthernames.Text;
            cmdNewCustomer.Parameters.Add(new SqlParameter("@Gender", SqlDbType.NChar, 6, "Gender"));
            cmdNewCustomer.Parameters["@Gender"].Value = cmbGender.Text;
            cmdNewCustomer.Parameters.Add(new SqlParameter("@dob", SqlDbType.DateTime, 8, "dob"));
            cmdNewCustomer.Parameters["@dob"].Value = dptDob.Value;
            cmdNewCustomer.Parameters.Add(new SqlParameter("@Age", SqlDbType.Int, 8, "Age"));
            cmdNewCustomer.Parameters["@Age"].Value = txtAge.Value;
            cmdNewCustomer.Parameters.Add(new SqlParameter("@phone", SqlDbType.Decimal, 25, "phone"));
            cmdNewCustomer.Parameters["@phone"].Value = txtPhone.Text;

            cmdNewCustomer.Parameters.Add(new SqlParameter("@email", SqlDbType.NVarChar, 50, "email"));
            cmdNewCustomer.Parameters["@phone"].Value = txtEmail.Text;
            cmdNewCustomer.Parameters.Add(new SqlParameter("@addressHouse", SqlDbType.NVarChar, 1000, "addressHouse"));
            cmdNewCustomer.Parameters["@addressHouse"].Value = txtAddress.Text;
            cmdNewCustomer.Parameters.Add(new SqlParameter("@stateOrigin", SqlDbType.NVarChar, 50, "stateOrigin"));
            cmdNewCustomer.Parameters["@stateOrigin"].Value = txtStateOrigin.Text;
            cmdNewCustomer.Parameters.Add(new SqlParameter("@country", SqlDbType.NVarChar, 50, "country"));
            cmdNewCustomer.Parameters["@country"].Value = txtCountry.Text;
            cmdNewCustomer.Parameters.Add(new SqlParameter("@profession", SqlDbType.NVarChar, 50, "profession"));
            cmdNewCustomer.Parameters["@profession"].Value = txtProfession.Text;
            cmdNewCustomer.Parameters.Add(new SqlParameter("@department", SqlDbType.NVarChar, 15, "department"));
            cmdNewCustomer.Parameters["@department"].Value = cmbDepartment.Text;
            cmdNewCustomer.Parameters.Add(new SqlParameter("@Amount", SqlDbType.Money, 8, "Amount"));
            amount = txtDeposit.Value;
            string Amount = String.Format("Amount: {0:C}", amount);
            cmdNewCustomer.Parameters["@Amount"].Value = Amount;

            //NC-9 Add output parameter.

            cmdNewCustomer.Parameters.Add(new SqlParameter("@titheID", SqlDbType.Int));
            cmdNewCustomer.Parameters.Add(new SqlParameter("@titheID", SqlDbType.NChar, 10, "titheID"));
            cmdNewCustomer.Parameters["@titheID"].Value = this.parsedMembersID;
            cmdNewCustomer.Parameters["@titheID"].Direction = ParameterDirection.Output;
            bool isDecimal = decimal.TryParse(txtPhone.Text.Trim(new[] { ''"' }), out phone);
            if (isDecimal)
                cmdNewCustomer.Parameters["@phone"].Value = phone;

            //NC-10 try-catch-finally
            try
            {
                //NC-11 Open the connection.
                conn.Open();
                //NC-12 Run the stored procedure.
                 cmdNewCustomer.ExecuteNonQuery();

                 MessageBox.Show(" Congratulations!. Account" + " for " + txtName.Text + " with TitheID " + txtTitheID.Text + " has been created successfully.");
            }
            catch (SqlException se)
            {
                //NC-14 A simple catch.
                MessageBox.Show("Sorry, Error in Transaction. Account" + " for " + txtName.Text + " with TitheID " + txtTitheID.Text + "  could not be created.");
                MessageBox.Show(se.Message);
            }
            finally
            {

                //NC-15 Close the connection.
                conn.Close();
            }
        }
    }

使用Windows窗体条目更新SQL表

只是猜测,但您从文本字段和电子邮件字段分配电话:

cmdNewCustomer.Parameters["@phone"].Value = txtPhone.Text;
cmdNewCustomer.Parameters["@phone"].Value = txtEmail.Text;

然后你还有这个:

cmdNewCustomer.Parameters["@phone"].Value = phone;

至少我会试着去掉前两个。

如果这没有帮助,请使用探查器查看实际发送到数据库的值。