小编Rob*_*Rob的帖子

坚持使用SqlDataReader.GetValues方法

我正在调用一个SQL存储过程,它返回一个用户及其老板的小表.我想要做的是将此信息与我的应用程序的不同部分中的变量进行比较,以启用/取消编辑GridView行.为了实现这一点,似乎该GetValues方法将返回我需要的东西,即存储过程返回的整个数据集.我的想法是,一旦我在我的应用程序中拥有该数据,我就可以将该数据集加载到一个数组中,并循环遍历它以完成我需要做的事情.

问题是这个.在我下面的代码中,我收到错误

无法将类型'int'隐式转换为'object'

当我查看有关此方法的文档时,返回值为int.

我的问题是,既然存储过程包含字符串数据(用户名和老板名),为什么GetValuesmehtod会返回int?我的实际数据如何表示为数字?如何将数据转换为基于字符串的数组?

我一直在寻找互联网上的许多例子和信息,这似乎是新人们常见的问题.我只是不理解或得到它,我没有取得任何进展.任何帮助是极大的赞赏!

    public Object[] GetDeptAppData()
    {
        Object[] myObject;
        string sp_deptapp, sp_status, sp_supervisor;

        //execute stored procedure to get data from database
        SqlConnection sqlConnection = new SqlConnection("Data Source=MyServer;Initial Catalog=MyCatalog;Persist Security Info=True;User ID=MyUser;Password=MyPassword");
        SqlCommand cmd = new SqlCommand();
        SqlDataReader reader;

        cmd.CommandText = "SP_Admin_DeptApp_Load";
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Connection = sqlConnection;
        sqlConnection.Open();
        reader = cmd.ExecuteReader();

        if (reader.HasRows == true)
        {
            //sp_deptapp = reader.GetValue(0).ToString();
            //sp_status = reader.GetValue(1).ToString();
            //sp_supervisor = reader.GetValue(2).ToString();

            myObject = …
Run Code Online (Sandbox Code Playgroud)

c# sql asp.net datareader

2
推荐指数
2
解决办法
2万
查看次数

标签 统计

asp.net ×1

c# ×1

datareader ×1

sql ×1