数据为什么会重复添加1个?

songwin songwin 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
把一个textbox中的数据添加到数据库中,可以每次点击按钮后都回添加两个相同的数据。

后台代码:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace quanliwenCsharp
{
/// <summary>
/// Addtype 的摘要说明。
/// </summary>
public class Addtype : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button BtnAddtype;
protected System.Web.UI.WebControls.TextBox txttype;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面

}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.BtnAddtype.Click += new System.EventHandler(this.BtnAddtype_Click);

}
#endregion

private void BtnAddtype_Click(object sender, System.EventArgs e)
{
//添加到数据库
String strConnection = ConfigurationSettings.AppSettings["connectionString"];
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();

String strsql;
strsql = "insert producetype (ttype) values (@txttype)";

SqlCommand objCommand = new SqlCommand(strsql,objConnection);
objCommand.Parameters.Add("@txttype", SqlDbType.NVarChar, 50);

objCommand.Parameters["@txttype"].Value = txttype.Text;
SqlDataReader objReader = objCommand.ExecuteReader();

objReader.Close();

objConnection.Close();

}



}
}
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
songwin
.NET社区 62,271 成员 668,968 内容