一个很简单的插入操作 
即 点击按钮 执行插入 insert into tablename(id,name) values('id','name')

解决方案 »

  1.   

    插入的数据就按钮里面 连接数据库 执行sql语句 
    没循环 加断点 也看不到
    但换成存储过程就没出现重复插入了
      

  2.   

    protected void btnAddSupplier_Click(object sender, EventArgs e)
        {
     
      using (SqlConnection con = new SqlConnection(connstring))
                {
                    con.Open();
                    SqlCommand cmd = con.CreateCommand();
                    SqlTransaction transaction;
                    transaction = con.BeginTransaction("CBDTransaction");                cmd.Connection = con;
                    cmd.Transaction = transaction;
                    int k;
                    try
                    {
                        cmd.CommandText = "insert into tableName(id,name) value("+txtId.text+","+txtName.text+")";
                        cmd.CommandType = CommandType.Text;
                        k=(int) cmd.ExecuteNonQuery();
                        transaction.Commit();
                    }
                    catch (SqlException ex)
                    {
                        k = 0;
                        transaction.Rollback();
                        throw new Exception(ex.Message);
                    }
                    finally
                    {
                        con.Close();
                        cmd.Dispose();
                        if (con.State == ConnectionState.Open)
                            con.Close();
                        con.Dispose();                }
                    
                }
    }
      

  3.   

        protected void Button1_Click(object sender, EventArgs e)
        {
    string sqlcon = ConfigurationManager.ConnectionStrings["mycon"].ConnectionString.ToString();
            using (SqlConnection con = new SqlConnection(sqlcon))
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                SqlTransaction transaction;
                transaction = con.BeginTransaction("CBDTransaction");
                cmd.Connection = con;
                cmd.Transaction = transaction;
                int k;
                try
                {
                    cmd.CommandText = "insert into tt values('" + txtid.Text.ToString() + "','" + txtuser.Text.ToString()+ "')";
                    cmd.CommandType = CommandType.Text;
                    k = (int)cmd.ExecuteNonQuery();
                    transaction.Commit();
                }
                catch (SqlException ex)
                {
                    k = 0;
                    transaction.Rollback();
                    throw new Exception(ex.Message);
                }
                finally
                {
                    con.Close();
                    cmd.Dispose();
                    if (con.State == ConnectionState.Open)
                        con.Close();
                    con.Dispose();            }        }    }
    楼主我的,只插入一次啊!!!!!