0

在此处输入图像描述我有一个表格,我在其中将记录插入数据库。有两个表,table_1 被调用members,table_2 被调用Amount

我正在使用两个 SQLINSERT语句将记录发送到数据库,因为这是我想出的方式——可能还有其他方式,我不知道。

当我插入记录时,我收到一条消息,它已成功插入,但是当我检查数据库时,插入的记录替换了当前的记录,所以我在数据库中的最后一条记录重复了几次。请协助。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CemiyetAidatSistem
{
    public partial class AddMember : Form
    {
        public AddMember()
        {
            InitializeComponent();
        }
        SqlConnection con = new SqlConnection("Data Source=My-PC\\SQLSERVER;Initial Catalog=FredericiaDernek;Integrated Security=True");

        private void btnInsert_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand();
            string Sql = "INSERT INTO Uyeleri ( dID, FullName, Address, Mobile, Email, Comments ) VALUES ('" + txtdID.Text + "', '" + txtAdiSoyadi.Text + "','" + txtAddress.Text + "','" + txtMobile.Text + "','" + txtEmail.Text + "','" + txtComments.Text + "')";
            cmd.CommandText = Sql;
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            Sql = "INSERT INTO Aidat (dID Year, Amount ) VALUES ('"+ txtdID.Text +"','" + txtYear.Text + "','" + txtAmount.Text + "')";
            cmd.CommandText = Sql;
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            for (int i = 0; i < this.Controls.Count; i++)
            {
                if (this.Controls[i] is TextBox)
                {
                    this.Controls[i].Text = "";
                }
            }
            MessageBox.Show("Data Added Scuessfully");
        }

    }
}
4

2 回答 2

2

我已经重写了您的代码以纠正错误和不良做法

string connString = "Data Source=My-PC\\SQLSERVER;Initial Catalog=FredericiaDernek;Integrated Security=True";

private void btnInsert_Click(object sender, EventArgs e)
{
    using(SqlConnection con = new SqlConnection(connString))
    {
        con.Open();
        string Sql = "INSERT INTO Uyeleri (dID, FullName, Address, Mobile, Email, Comments ) " + 
                     "VALUES (@id, @name, @address, @mobile, @email, @comments");
        using(SqlCommand cmd = new SqlCommand(Sql, con))
        {
            cmd.Parameters.AddWithValue("@id", txtdID.Text);
            cmd.Parameters.AddWithValue("@name", txtAdiSoyadi.Text);
            cmd.Parameters.AddWithValue("@address", txtAddress.Text);
            cmd.Parameters.AddWithValue("@mobile", txtMobile.Text);
            cmd.Parameters.AddWithValue("@email", txtEmail.Text);
            cmd.Parameters.AddWithValue("@comments", txtComments.Text);
            cmd.ExecuteNonQuery();

            Sql = "INSERT INTO Aidat (dID, [Year], Amount ) VALUES " + 
                  "(@id, @year, @amount)";
            cmd.Parameters.Clear();
            cmd.CommandText = Sql;  // <- missing this in the previous version.....
            cmd.Parameters.AddWithValue("@id", txtdID.Text);
            cmd.Parameters.AddWithValue("@name", txtYear.Text);
            cmd.Parameters.AddWithValue("@amount", txtAmount.Text);
            cmd.ExecuteNonQuery();
        }
    }

我改变了什么:

  • 第二个插入语句是错误的。第一列和第二列之间缺少逗号
  • 删除了在全局级别创建 SqlConnection
  • 添加了适当的 using 语句以在出现异常时处理 SqlConnection 和 SqlCommand
  • 两个插入语句使用的参数
  • 在 Year 字段周围添加方括号(Year 是 T-SQL 中的保留关键字)

在全局级别创建 SqlConnection 是不好的,因为您获取系统资源并且您不会在应用程序的生命周期内释放它们。如果异常处理不正确,情况可能会失控。

现在我对你的桌子有些怀疑。字段 dID(两个表)和 Amount 是文本类型(varchar,nvarchar)?。如果它们是数字类型,则需要在将值添加到参数集合之前添加转换

于 2012-12-22T16:15:46.870 回答
1

我还建议更改您的 for 循环以清除控件替换此

for (int i = 0; i < this.Controls.Count; i++)
{
    if (this.Controls[i] is TextBox)
    {
        this.Controls[i].Text = "";
    }
}

使用 linq 使用以下代码。

this.Controls.OfType<TextBox>().ToList().ForEach(textBox => textBox.Clear());

请记住,“this”指的是您的表单名称

所以它会是

(YourWinFormsName).Controls.OfType<TextBox>().ToList().ForEach(textBox => textBox.Clear());
于 2012-12-22T16:23:53.870 回答