gpt4 book ai didi

c# - Web 表单在数据库中两次上传相同的数据

转载 作者:太空狗 更新时间:2023-10-29 22:19:10 26 4
gpt4 key购买 nike

我正在将数据上传到数据库,但是我的表单首先在数据库中上传了相同的数据两次我上传数据时没有检查是否插入了天气它工作正常它正在上传数据一次现在我已经检查了如果data inserted than show the message data inserted successful but this is upload data twice.

这是我的代码:

SqlConnection conn1 = new SqlConnection("Data Source=ZAZIKHAN\\SQLEXPRESS;Initial Catalog=resume;Integrated Security=True");
conn1.Open();

SqlCommand cmd3 = new SqlCommand("insert into Profile(Id,Name,JobTitle,Phone,Email,Address,Website,Facebook,Twitter,GooglePlus,Skype,Picture,WhyMeText) values('"+ID.Text+"','" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox9.Text + "','" + TextBox10.Text + "','" + uploadFolderPath + "','" + TextArea1.InnerText + "')", conn1);

cmd3.ExecuteNonQuery();
if (cmd3.ExecuteNonQuery() == 1)
{
Response.Write("<script LANGUAGE='JavaScript' >alert('information saved Successful')</script>");
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
TextBox6.Text = "";
TextBox7.Text = "";
TextBox8.Text = "";
TextBox9.Text = "";
TextBox10.Text = "";
TextArea1.InnerText = "";
}
else { Response.Write("<script LANGUAGE='JavaScript' >alert('sorry try again')</script>"); }

conn1.Close();

最佳答案

因为您执行了 SqlCommand 两次。

一个是用

cmd3.ExecuteNonQuery();

还有一个是with;

if (cmd3.ExecuteNonQuery() == 1)

来自 MSDN ;

Executes a Transact-SQL statement against the connection and returns the number of rows affected.

请始终使用 parameterized queries .这种字符串连接对于 SQL Injection 是开放的攻击。

也可以使用 using处理你的 SqlConnection 的语句;

using(SqlConnection conn1 = new SqlConnection("Data Source=ZAZIKHAN\\SQLEXPRESS;Initial Catalog=resume;Integrated Security=True"))
{
//Write here your command with parameterized way..
conn1.Open();
if (cmd3.ExecuteNonQuery() == 1)
{
//....
}
}

关于c# - Web 表单在数据库中两次上传相同的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20566151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com