首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Windows窗体条目更新SQL表

使用Windows窗体条目更新SQL表
EN

Stack Overflow用户
提问于 2015-11-14 08:00:18
回答 1查看 406关注 0票数 0

我希望能够使用Windows窗体中的条目来更新我的SQL数据表,但我已经尝试了所有方法,但它不起作用。我一直有错误,特别是“无法将字符串转换为十进制”。我有两个表,一个是整体成员表,第二个是与日期付款的经常性记录表。

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

代码语言:javascript
复制
//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();

            }
        }

    }
EN

回答 1

Stack Overflow用户

发布于 2015-11-14 15:13:35

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

代码语言:javascript
复制
cmdNewCustomer.Parameters["@phone"].Value = txtPhone.Text;
cmdNewCustomer.Parameters["@phone"].Value = txtEmail.Text;

然后你还有这个:

代码语言:javascript
复制
cmdNewCustomer.Parameters["@phone"].Value = phone;

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

如果这不起作用,可以使用profiler查看您实际发送到数据库的值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33703606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档