Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQL输出存储过程不与ExecuteReader一起使用_C#_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

C# SQL输出存储过程不与ExecuteReader一起使用

C# SQL输出存储过程不与ExecuteReader一起使用,c#,sql,sql-server,sql-server-2008,C#,Sql,Sql Server,Sql Server 2008,在SQL 2008和C 4.0上使用存储过程时,我无法从Select语句检索输出信息和返回信息。我一直在获取未设置为对象实例的对象引用。。当我执行ExecuteScalar时,我得到的是行,而不是数据。找到了一些例子,它们看起来像我正在做的,所以我想我错过了一些简单的东西。谢谢 存储过程 使用[PhoneDb] 去 /******对象:StoredProcedure[dbo]。[TestPagingProcedure]脚本日期:06/16/2011 08:39:03******/ 将ANSI_空

在SQL 2008和C 4.0上使用存储过程时,我无法从Select语句检索输出信息和返回信息。我一直在获取未设置为对象实例的对象引用。。当我执行ExecuteScalar时,我得到的是行,而不是数据。找到了一些例子,它们看起来像我正在做的,所以我想我错过了一些简单的东西。谢谢

存储过程

使用[PhoneDb] 去 /******对象:StoredProcedure[dbo]。[TestPagingProcedure]脚本日期:06/16/2011 08:39:03******/ 将ANSI_空值设置为ON 去 在上设置带引号的\u标识符 去 更改过程[dbo]。[TestPagingProcedure] -在此处添加存储过程的参数 @startRowIndex int, @最大行数, @全行整数输出 像 开始 -已添加SET NOCOUNT以防止额外的结果集丢失 -干扰SELECT语句。 不计数; 声明@first_id UNIQUEIDENTIFIER 声明@startRow int 设置@startRowIndex=@startRowIndex-1*@maximumRows 如果@startRowIndex=0 设置@startRowIndex=1 在startRowIndex设置行数 按ExtensionGUID从ExtItem订单中选择@first\u id=ExtensionGUID 打印@first_id 设置行数@maximumRows 选择 扩展UID、AesExt、AESHAPSHPASSWORD、ToNumber、AgentExt、Name、JpgImageName、BigImageName、WbmpImageName 从ExtItem WHERE ExtensionGUID>=@第一个\u id 按扩展UID排序 将行数设置为0 -获取总行数 从ExtItem中选择@totalRows=CountExtensionUID 终止 C代码


根据,在处理输出参数之前必须关闭datareader。

最好使用SELECTTOP@maximumRows ... 而不是设置行数。通过这种方式,查询优化器知道您只需要最上面的行,并且可能会生成一个为此而优化的计划。我把它放在我的循环之后,而不是在结束之后。谢谢你,先生!!我会记住这一点,我知道答案就在这里的某个地方。我也有同样的问题。
 public bool GetPagedResults(string startRowIndex, string maxRows, ref double totalRowsReturned)
    {
        bool IsSuccess = false;
        string clearPassword = "";
        Log.WriteLine("GetExtList : ENTERED GETEXTITEM: ", Log.DEBUG_LEVEL.VERBOSE);
        SqlConnection MyConnection = null;
        EnDecrypt hasher = null;

        try
        {
            if (SQLLookup.DatabaseString == "")
            {
                Log.WriteLine("GetPagedResults : SQLLookup.DatabaseString is empty:", Log.DEBUG_LEVEL.VERBOSE);
                SQLLookup.SQLFinder();
                Log.WriteLine("GetPagedResults : SQL FINDER RUN: SQLLookup.DatabaseString:'" + SQLLookup.DatabaseString + "'", Log.DEBUG_LEVEL.VERBOSE);
            }

            Log.WriteLine("GetPagedResults: SQL Server '" + SQLLookup.DatabaseString + "'", Log.DEBUG_LEVEL.VERBOSE);

            _extItemList.Clear();  // Keep new records from just being appended to existing list.

            hasher = new EnDecrypt("SetMyKey", "SaltGenerator");

            // Create a Connection to SQL Server
            MyConnection = new SqlConnection(@"Data Source= " + SQLLookup.DatabaseString + @"; Initial Catalog=PhoneDb;Integrated Security=True");

            SqlCommand myCommand = new SqlCommand("TestPagingProcedure", MyConnection);
            myCommand.CommandType = CommandType.StoredProcedure;

            /* ASSIGN PARAMETERS */
            myCommand.Parameters.Add(new SqlParameter("@startRowIndex", startRowIndex));
            myCommand.Parameters.Add(new SqlParameter("@maximumRows", maxRows));
            myCommand.Parameters.Add("@totalRows", SqlDbType.Int, 4);
            myCommand.Parameters["@totalRows"].Direction = ParameterDirection.Output;


            Log.WriteLine("GetPagedResults:3 After try ", Log.DEBUG_LEVEL.VERBOSE);
            Log.WriteLine("GetPagedResults:3 startRowIndex = " + startRowIndex + "  maxRows = " + maxRows, Log.DEBUG_LEVEL.VERBOSE);
            MyConnection.Open();
            SqlDataReader Reader = myCommand.ExecuteReader();

            Log.WriteLine("GetPagedResults  BEFORE WHILE LOOP", Log.DEBUG_LEVEL.VERBOSE);
            while (Reader.Read())
            {
                /* BUILD EXT ITEM*/
                ExtItem extItem = new ExtItem();
                if (Reader.IsDBNull(0) || Reader.GetGuid(0) == Guid.Empty)
                    extItem.ExtensionGUID = Guid.Empty;
                else
                    extItem.ExtensionGUID = Reader.GetGuid(0);

                if (Reader.IsDBNull(1) || Reader.GetString(1) == "")
                    extItem.AesExt = "No value";
                else
                    extItem.AesExt = Reader.GetString(1);


                /* ADD ITEM TO LIST */
                AddItem(extItem);

                //Log.WriteLine("GetExtList extItem: " + extItem.ToString(), Log.DEBUG_LEVEL.VERBOSE);
            }

            // get the total rows 
            Log.WriteLine("GetPagedResults: New Total number of pages: " + (int)myCommand.Parameters[2].Value, Log.DEBUG_LEVEL.TERSE);
            // totalRowsReturned = myCommand.Parameters["@totalRows"];

            IsSuccess = true;

            MyConnection.Close();
            Log.WriteLine("GetPagedResults: RETURNING:", Log.DEBUG_LEVEL.VERBOSE);
        }

        catch (Exception ex)
        {
            Log.WriteLine("GetPagedResults: Unable to retrieve Extension list. Caught Exception " + ex.Message,
                Log.DEBUG_LEVEL.TERSE);
            IsSuccess = false;
        }

        MyConnection.Close();

        return IsSuccess;
    }