1 public int Insert(DataTable dt) 2 { 3 int flag = 0; 4 StringBuilder sb; 5 SqlCommandBase cmd; 6 try 7 { 8 if (dt.Rows.Count>0) 9 { 10 for (int i = 0; i < dt.Rows.Count; i++) 11 { 12 13 sb = new StringBuilder(); 14 sb.Append("INSERT INTO sys_UserOperatingAutyRe"); 15 sb.Append(" ([Guid],[UserId],[GroupId],[OperatingAutyId])"); 16 sb.Append("VALUES (@Guid,@UserId,@GroupId,@OperatingAutyId)"); 17 cmd = SqlBuilder.BuildSqlCommandBase(sb.ToString()); 18 cmd.AddParam("@Guid", SqlDbType.VarChar, dt.Rows[i]["Guid"]); 19 cmd.AddParam("@UserId", SqlDbType.VarChar, dt.Rows[i]["UserId"]); 20 cmd.AddParam("@GroupId", SqlDbType.VarChar, dt.Rows[i]["GroupId"]); 21 cmd.AddParam("@OperatingAutyId", SqlDbType.VarChar, dt.Rows[i]["OperatingAutyId"]); 22 flag = DataProvider.Instance.ExecuteNoQuery(_Loginer.DBName, cmd.SqlCommand); 23 } 24 } 25 return flag; 26 } 27 catch (Exception ex) 28 { 29 30 return 0; 31 } 32 }
这样循环一条插一条是不是不好啊?如果不好怎么改进?
楼上也说了,
使用SQLSERVER 或ORACLE 提供的BulkCopy功能,执行批量插入。