在sql server数据库中插入记录。每次插入2条记录,我认为事件发射两次。如何在ado.net中停止两次插入记录。
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
CompanyName = txtCompany.Text.Trim();
InsertData();
}
catch (Exception)
{
throw;
}
}
private void InsertData()
{
if (Page.IsPostBack)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["kernelCS"].ConnectionString);
SqlCommand cmd = new SqlCommand(@"INSERT INTO [tbl_Leads]([CompanyName])VALUES(@CompanyName)", con);
cmd.Parameters.AddWithValue("@CompanyName", txtCompany.Text.ToString());
con.Open();
cmd.ExecuteNonQuery();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
...
}
}
答案 0 :(得分:3)
您通过cmd.ExecuteNonQuery()
和da.Fill(ds)
语句在代码中触发了两次查询。
答案 1 :(得分:0)
试试这个:
private void InsertData()
{
SqlConnection con = new SqlConnection(ConfigurationManager.
ConnectionStrings["kernelCS"].ConnectionString);
SqlCommand cmd = new SqlCommand(@"INSERT INTO [tbl_Leads]
([CompanyName]) VALUES (@CompanyName)", con);
cmd.Parameters.AddWithValue("@CompanyName", txtCompany.Text.ToString());
con.Open();
cmd.ExecuteNonQuery();
}`