C# 在数据库中重复记录信息

C# 在数据库中重复记录信息,c#,sql,C#,Sql,我有一个表单,我正在将一条记录插入数据库。有两个表,表1称为成员,表2称为金额 我正在使用两个SQLINSERT语句将记录发送到数据库,因为这就是我所发现的方法——可能还有其他方法,我不知道 当我插入记录时,我会收到一条消息,表明它已成功插入,但当我检查数据库时,插入的记录将替换当前的记录,因此数据库中的最后一条记录重复了几次。请帮忙 using System; using System.Collections.Generic; using System.ComponentModel; usin

我有一个表单,我正在将一条记录插入数据库。有两个表,表1称为
成员
,表2称为
金额

我正在使用两个SQL
INSERT
语句将记录发送到数据库,因为这就是我所发现的方法——可能还有其他方法,我不知道

当我插入记录时,我会收到一条消息,表明它已成功插入,但当我检查数据库时,插入的记录将替换当前的记录,因此数据库中的最后一条记录重复了几次。请帮忙

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");
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用System.Data.SqlClient;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
名称空间cemiyetaidstatistem
{
公共部分类AddMember:表单
{
公共AddMember()
{
初始化组件();
}
SqlConnection con=newsqlconnection(“数据源=My PC\\SQLSERVER;初始目录=FredericiaDernek;集成安全性=True”);
私有无效BTN插入\单击(对象发送方,事件参数e)
{
SqlCommand cmd=新的SqlCommand();
string Sql=“插入Uyeleri(dID、全名、地址、手机、电子邮件、注释)值(“+txtdID.Text+”、“+txtAdiSoyadi.Text+”、“+TXTDAddress.Text+”、“+txtMobile.Text+”、“+TXTMail.Text+”、“+TXTDComments.Text+”);
cmd.CommandText=Sql;
cmd.Connection=con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Sql=“插入Aidat(dID年,金额)值(“+txtdID.Text+”、“+TXTDYEAR.Text+”、“+TXTDIMOUNT.Text+”)”;
cmd.CommandText=Sql;
cmd.Connection=con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
for(int i=0;i
我已经重写了您的代码,以纠正错误和不良做法

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();
        }
    }
string connString=“数据源=My PC\\SQLSERVER;初始目录=FredericiaDernek;集成安全性=True”;
私有无效BTN插入\单击(对象发送方,事件参数e)
{
使用(SqlConnection con=newsqlconnection(connString))
{
con.Open();
string Sql=“插入Uyeleri(dID、全名、地址、手机、电子邮件、评论)”+
“值(@id、@name、@address、@mobile、@email、@comments”);
使用(SqlCommand cmd=newsqlcommand(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=“插入Aidat(dID,[年],金额)值”+
“(@id,@year,@amount)”;
cmd.Parameters.Clear();

cmd.CommandText=Sql;//我已经重写了您的代码,以纠正错误和错误做法

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();
        }
    }
string connString=“数据源=My PC\\SQLSERVER;初始目录=FredericiaDernek;集成安全性=True”;
私有无效BTN插入\单击(对象发送方,事件参数e)
{
使用(SqlConnection con=newsqlconnection(connString))
{
con.Open();
string Sql=“插入Uyeleri(dID、全名、地址、手机、电子邮件、评论)”+
“值(@id、@name、@address、@mobile、@email、@comments”);
使用(SqlCommand cmd=newsqlcommand(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=“插入Aidat(dID,[年],金额)值”+
“(@id,@year,@amount)”;
cmd.Parameters.Clear();

cmd.CommandText=Sql;//我还建议更改for循环以清除控件,并替换此

for (int i = 0; i < this.Controls.Count; i++)
{
    if (this.Controls[i] is TextBox)
    {
        this.Controls[i].Text = "";
    }
}
for(int i=0;i
下面的代码使用linq

this.Controls.OfType<TextBox>().ToList().ForEach(textBox => textBox.Clear());
this.Controls.OfType().ToList().ForEach(textBox=>textBox.Clear());
请记住,“this”将引用表单的名称

是的

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

我还建议更改for循环以清除控件并替换此选项

for (int i = 0; i < this.Controls.Count; i++)
{
    if (this.Controls[i] is TextBox)
    {
        this.Controls[i].Text = "";
    }
}
for(int i=0;i
下面的代码使用linq

this.Controls.OfType<TextBox>().ToList().ForEach(textBox => textBox.Clear());
this.Controls.OfType().ToList().ForEach(textBox=>textBox.Clear());
请记住,“this”将引用表单的名称

是的

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

查看您的表设计是否有主键,以及您要插入的ID,更改查询/插入语句以利用SQL参数,您正在自行设置