问题如题:asp.net
插入若干条记录,其中提供的部分数据是主键重复插入,,怎么跳过这些继续插入其它数据
【就是当执行到逐渐重复报错时候不报错跳过这条继续执行下面的】
相当于 for 循环里报错先continue 一下,而不是直接break;谢谢

解决方案 »

  1.   

            public static int AddAuthWin(string UsrGroupId, TreeView tvType)
            {            SqlConnection Conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);
                Conn.Open();
                SqlTransaction SqlTran = Conn.BeginTransaction();            foreach (TreeNode tn in tvType.CheckedNodes)
                {
                    SqlParameter[] parm = new SqlParameter[]{
                        SqlHelper.GetParameter("@usrgrp_id",SqlDbType.VarChar,50,"usrgrp_id",UsrGroupId),
                        SqlHelper.GetParameter("@classId",SqlDbType.Int,4,"classId",Convert.ToInt32(tn.Value))
                    };
                    try
                    {
                        SqlHelper.ExecuteNonQuery(SqlTran, CommandType.StoredProcedure, "usp_AddAuthToGroup", parm);
                    }
                    catch (System.Exception ee)
                    {
                        SqlTran.Rollback();
                        throw ee;
                         
                    }
                };
                SqlTran.Commit();
                //SqlTran.Connection.Close();
                Conn.Close();            return 0;
            }插入重复要能继续执行下面 就达到效果了
    我觉得来回判断浪费资源也不太方便吧
      

  2.   

            public static int AddAuthWin(string UsrGroupId, TreeView tvType)
            {            SqlConnection Conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);
                Conn.Open();
                SqlTransaction SqlTran = Conn.BeginTransaction();            foreach (TreeNode tn in tvType.CheckedNodes)
                {
                    SqlParameter[] parm = new SqlParameter[]{
                        SqlHelper.GetParameter("@usrgrp_id",SqlDbType.VarChar,50,"usrgrp_id",UsrGroupId),
                        SqlHelper.GetParameter("@classId",SqlDbType.Int,4,"classId",Convert.ToInt32(tn.Value))
                    };
                    try
                    {
                        SqlHelper.ExecuteNonQuery(SqlTran, CommandType.StoredProcedure, "usp_AddAuthToGroup", parm);
                    }
                    catch (System.Exception ee)
                    {
                        SqlTran.Rollback();
                        throw ee;
                         
                    }
                };
                SqlTran.Commit();
                //SqlTran.Connection.Close();
                Conn.Close();            return 0;
            }插入重复要能继续执行下面 就达到效果了
    我觉得来回判断浪费资源也不太方便吧