Add和AddRange区别 Add和AddRange Add:将指定的对象添加到……中 AddRange:向……末尾,添加数组 - 在群体操作时,使用AddRange取代Add 用AddRange可以让我们要加入的东西一次性加入,而不要每次都加一次,这样显然可以加快速度。几乎所有的windows control都支持Add和AddRange两种方法。 Add: For i = 0 To param.Length - 1 comm.Parameters.Add(param(i)) Next i AddRange: Comm.Parameters.AddRange(Param) - 支持Add和AddRange的类: StringCollection, TraceCollection, SqlParameter, etc. HttpWebRequest UserControl ColumnHeader -
原代码: view plain 1.''' ExecuteQuery,查询数据操作 2.''' </summary> 3.''' <param name="strSql">sql语句</param> 4.''' <param name="param">参数集合</param> 5.''' <param name="ct">命令类型</param> 6.''' <returns>DataTable类型,数据表</returns> 7.''' <remarks></remarks> 8.Public Function ExecuteQuery(ByVal strSql As String, ByVal param() As SqlParameter, ByVal ct As CommandType) As DataTable 9. Dim dt As New DataTable 10. 11. comm = New SqlCommand(strSql, Me.GetConn) 12. comm.CommandType = ct 13. 14. If param IsNot Nothing Then 15. For i = 0 To param.Length - 1 16. comm.Parameters.Add(param(i)) 17. Next i 18. End If 19. 20. Try 21. dr = comm.ExecuteReader() 22. dt.Load(dr) 23. Catch ex As Exception 24. Throw ex 25. Finally 26. dr.Close() 27. conn.Close() 28. End Try 29. 30. Return dt 31.End Function 修改后: view plain 1.''' <summary> 2.''' 查询方法 3.''' </summary> 4.''' <param name="myCommandType">sql语句类型:区别于存储过程</param> 5.''' <param name="Param">参数数组</param> 6.''' <param name="strSql">sql语句</param> 7.Public Function ExecuteQuery(ByVal myCommandType As CommandType, ByVal Param() As SqlParameter, ByVal strSql As String) As DataTable 8. Dim dt As New DataTable 9. 10. Comm = New SqlCommand(strSql, Me.GetConn) 11. Comm.CommandType = myCommandType 12. 13. If Param IsNot Nothing Then 14. Comm.Parameters.AddRange(Param) 15. End If 16. 17. Try 18. myDataReader = Comm.ExecuteReader 19. dt.Load(myDataReader) 20. Catch ex As Exception 21. Throw ex 22. Finally 23. myDataReader.Close() 24. Conn.Close() 25. End Try 26. 27. Return dt 28.End Function