| | |
| | | } |
| | | return dataTable; |
| | | } |
| | | |
| | | public DataTable ExecuteStoredProcedure(string procedureName, SqlParameter[] parameters = null) |
| | | { |
| | | try |
| | | { |
| | | using (SqlConnection connection = new SqlConnection(_connectionString)) |
| | | { |
| | | using (SqlCommand command = new SqlCommand(procedureName, connection)) |
| | | { |
| | | command.CommandType = CommandType.StoredProcedure; |
| | | |
| | | // 添加参数(如果存在) |
| | | if (parameters != null && parameters.Length > 0) |
| | | { |
| | | command.Parameters.AddRange(parameters); |
| | | } |
| | | |
| | | connection.Open(); |
| | | |
| | | // 创建DataTable存储结果 |
| | | DataTable resultTable = new DataTable(); |
| | | |
| | | // 使用SqlDataAdapter填充DataTable |
| | | using (SqlDataAdapter adapter = new SqlDataAdapter(command)) |
| | | { |
| | | adapter.Fill(resultTable); |
| | | } |
| | | |
| | | return resultTable; |
| | | } |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | public int ExecuteNonQuery(string sql) |
| | | { |
| | | return this.ExecuteNonQuery(sql, CommandType.Text, null); |