我有一个表单,我将记录插入数据库。有两个表,table_1称为members
,table_2称为Amount
。
我使用两个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");
}
}
}
答案 0 :(得分: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是不好的,因为您获取了系统资源,并且在应用程序的生命周期内不会丢弃它们。如果未正确处理异常,情况可能会失控。
现在我对你的桌子有些怀疑。字段dID(两个表)和数量都是文本类型(varchar,nvarchar)?.如果它们是数字类型,则必须在将值添加到Parameters集合
之前添加转换答案 1 :(得分: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());