DataTable不返回数据

时间:2017-04-10 19:08:26

标签: c# sql

我的方法缺失了什么?它没有返回数据表。

public DataTable GetHotelReportData(int _ratpropid) {

            var _connectionString = _isDevelopment ? CommonTypes.Dev : CommonTypes.Prod;

            DataTable dt = new DataTable();
            dt.Clear();
            SqlConnection conn = new SqlConnection(_connectionString);
            conn.Open();
            SqlCommand cmd = new SqlCommand("ww.HotelRpt_spGenerateData2018", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@RatPropId", _ratpropid);
            SqlDataAdapter da = new SqlDataAdapter();

            try {
                da.SelectCommand = cmd;
                da.Fill(dt);
            }
            catch (Exception _ex) {
                new ErrorLogging().Log(_ex);
            }
            finally {
                conn.Close();
                da.Dispose();
                cmd.Dispose();
            }
            return dt;
        }

1 个答案:

答案 0 :(得分:1)

尝试将数据适配器切断并仅使用该命令。据我所知,不需要适配器:

public DataTable GetHotelReportData(int _ratpropid) {

        var _connectionString = _isDevelopment ? CommonTypes.Dev : CommonTypes.Prod;

        DataTable dt = new DataTable();
        dt.Clear();
        SqlConnection conn = new SqlConnection(_connectionString);
        SqlCommand cmd = new SqlCommand("ww.HotelRpt_spGenerateData2018", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@RatPropId", _ratpropid);


        try {
            conn.Open();
            dt.Load(cmd.ExecuteReader);
            return dt;
        }
        catch (Exception _ex) {
            new ErrorLogging().Log(_ex);
        }
        finally {
            conn.Close();
            cmd.Dispose();
        }

    }

如果这不起作用,请尝试在SSMS(或其他DBMS)中本机执行存储过程,看看是否在那里得到任何结果。