简体   繁体   English

SqlDataReader不从SQL Server存储过程返回任何数据

[英]SqlDataReader not returning any data from SQL Server stored procedure

I have the following stored procedure in my SQL Server database which executes fine: 我的SQL Server数据库中有以下存储过程,该过程可以正常运行:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[LoadStates]
AS
BEGIN
    SELECT stateabbrev 
    FROM states 
    ORDER BY stateabbrev
END
GO

Here is my C# code; 这是我的C#代码; sdrData has been initialized and seems to be correct but the results set is empty. sdrData已初始化,似乎正确,但结果集为空。 Please help. 请帮忙。

using (SqlCommand sqlCmd = new SqlCommand("LoadStates", sqlConn))
{
    sqlCmd.CommandType = CommandType.StoredProcedure;

    // set up the parameters that the Stored Procedure expects
    //sqlCmd.Parameters.Add("@States", SqlDbType.Char, 2).Direction = ParameterDirection.Output;

    using (SqlDataReader sdrData = sqlCmd.ExecuteReader())
    {
        while (sdrData.Read())
        {
            string strDBNme = sdrData.ToString();
            //string strDBNme = (string)sdrData["States"];
            cmbxACState.Items.Add(strDBNme);
        }

        sdrData.Close();
    }
}
string strDBNme = (string)sdrData["stateabbrev"];
       SqlDataReader sdrData  = null;
        // create a connection object
        SqlConnection conn = new SqlConnection(create your sql connection string here );

// create a command object
  SqlCommand cmd  = new SqlCommand("LoadStates", conn);
    sqlCmd.CommandType = CommandType.StoredProcedure;
        try
        {
            // open the connection
            conn.Open();                
            sdrData = cmd.ExecuteReader();
            while (sdrData.Read())
                    {
                        //string strDBNme = sdrData.ToString();
                        string strDBNme = (string)sdrData["States"];
                        cmbxACState.Items.Add(strDBNme);
                    }

        }
        finally
        {
            // 3. close the reader
            if (sdrData != null)
            {
                sdrData.Close();
            }

            // close the connection
            if (conn != null)
            {
                conn.Close();
            }
        }   
string connectionString = ConfigurationManager.ConnectionStrings["StackDemo"]
                 .ConnectionString;
         using (SqlConnection connection = new SqlConnection(connectionString))
         {

             connection.Open();
             using (SqlCommand cmd = new SqlCommand("LoadStates", connection))
             {
                 cmd.CommandType = CommandType.StoredProcedure;


                 using (SqlDataReader sdrData = cmd.ExecuteReader())
                 {
                     while (sdrData.Read())
                     {
                       Console.WriteLine( (string)sdrData["stateabbrev"]);

                     }

                     sdrData.Close();
                 }
             }
         }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM