C# 使用存储过程从SQL Server返回数据

C# 使用存储过程从SQL Server返回数据,c#,asp.net,sql-server,stored-procedures,C#,Asp.net,Sql Server,Stored Procedures,我在使用存储过程将数据从sql server数据库返回到aspx页面时遇到问题,希望有人能指出我的错误所在 当我运行时,项目数据被成功地输入到表中,但在下一页(Confirm.aspx)上没有返回任何内容 确认.aspx.cs using Devworks; namespace OSQARv0._1 { public partial class Confirm_Questionnaire : System.Web.UI.Page { OscarSQL b;

我在使用存储过程将数据从sql server数据库返回到aspx页面时遇到问题,希望有人能指出我的错误所在

当我运行时,项目数据被成功地输入到表中,但在下一页(Confirm.aspx)上没有返回任何内容

确认.aspx.cs

using Devworks;

namespace OSQARv0._1
{
    public partial class Confirm_Questionnaire : System.Web.UI.Page
    {
        OscarSQL b;

        protected void Page_Load(object sender, EventArgs e)
        {
            b = new OscarSQL();
            string qstname = b.GetQuestionName();
            ReturnQstID.Text = qstname;          

        }// End Page_Load

    } // Emd Class Confirm_Questionnaire

} // End Namespace
SQL.cs(应用程序代码)

存储过程

public OscarSQL()
        {
            _productConn = new SqlConnection();
            _productConnectionString += "data source=mssql.database.co.uk; Initial Catalog=devworks_oscar;User ID=username;Password=password";
            _productConn.ConnectionString = _productConnectionString;
        }
    public string GetQuestionName()
            {
                SqlCommand myCommand = new SqlCommand("GetQuestion", _productConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                SqlParameter retval = myCommand.Parameters.Add("@QUESTTEXT", SqlDbType.VarChar);
                retval.Direction = ParameterDirection.Output;
                _productConn.Open();
                string returnvalue = (string)myCommand.Parameters["@QUESTTEXT"].Value;
                _productConn.Close();
                return returnvalue;
            }
USE [devworks_oscar]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [hgomez].[GetQuestion]  

AS
    /*SET NOCOUNT ON;*/
    SELECT QuestionText FROM [Questions] WHERE QuestionnaireID = 21
    RETURN

非常感谢您的帮助。

您的存储过程不使用输出参数返回值,因此请使用:


您没有执行该过程。您正在设置它,但不返回它

string returnvalue = (string)myCommand.ExecuteScalar();

返回值是存储过程返回的值,通常为整数值,有时用于驱动业务逻辑或错误条件。在您的情况下,您需要的是返回的数据,而不是返回值。因此,如果查询返回单个值,请使用ExecuteScalar;如果查询返回一个或多个数据记录,请使用ExecuteReader。ExecuteOnQuery通常用于插入/更新和/或删除操作。

在这种情况下,他应该使用ExecuteScalar@stefan,是的,在我发布帖子后马上意识到:)@MarcGravel对我来说像个标量?从[Questions]中选择QuestionText,其中QuestionnaireID=21my抱歉-我匆忙阅读了它,并想象了一个
SELECT@somevar=…
@marcGravel我将来将使用一个变量,例如
@personID
,ExecuteScalar()是否仍然正确?@Rupert,如果您在SQL语句中分配参数(这就是他所想的),那么ExecuteScalar不会返回任何东西,但是如果您只是从现有变量中进行选择,那么您仍然需要ExecuteScalar@Rupert,您应该使用ExecuteScalar,因为该查询的结果正好返回一个值,commandType:commandType.StoredProcedure.Single();-只要说
ExecuteScalar() is a good idea if you know there is only one occurance of that record in the database, ExecuteScalar if I am not mistaken only returns the first record or occurance it finds.. you could use a datareader and do datareader.ExecuteReader() this is forward only but there are ways to make it biDirectional hehe... :) anyway if you want to loop thru the results use ExecuteReader() if you want to execute a StoredProceduere with UDATE,DELETE, or INSERT use reader.ExecuteNonQuery().

Hope this helps.
ExecuteScalar() is a good idea if you know there is only one occurance of that record in the database, ExecuteScalar if I am not mistaken only returns the first record or occurance it finds.. you could use a datareader and do datareader.ExecuteReader() this is forward only but there are ways to make it biDirectional hehe... :) anyway if you want to loop thru the results use ExecuteReader() if you want to execute a StoredProceduere with UDATE,DELETE, or INSERT use reader.ExecuteNonQuery().

Hope this helps.