简体   繁体   English

从不需要参数的存储过程中获取值

[英]Getting a value from stored procedure which doesn't need parameters

I have a stored procedure as shown below which retrieves the top 3 rows from my database.我有一个如下所示的存储过程,它从我的数据库中检索前 3 行。

Stored Procedure存储过程

USE [PaydayLunch]
GO
/****** Object:  StoredProcedure [dbo].[GetPastPlaces]    Script Date: 27/10/15 12:28:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetPastPlaces]
AS
SELECT TOP 3* From Past_Places
  order by id desc

My DB has the following columns:我的数据库有以下列:

  • ID ID
  • Past_Place过去的地方
  • Month
  • Click_Date点击日期

I want need to get the 'Months' value from my top row in my database but I don't know how to write this in my code behind as all solutions I find either populate a gridview or have parameters in which min doesn't and doesn't require them.我想从我的数据库中的第一行获取“Months”值,但我不知道如何在我的代码后面写这个值,因为我发现所有解决方案要么填充gridview要么有参数,其中 min 没有和不需要它们。

I need this value so I can use it in an IF statement in my view.我需要这个值,以便我可以在IF语句中使用它。

Just so you know that I do have to following code which uses another stored procedure to update table.只是让您知道我必须遵循使用另一个存储过程来更新表的代码。

protected void BtnDecideForMe_Click(object sender, EventArgs e)
{
    string connection = ConfigurationManager.ConnectionStrings["PaydayLunchConnectionString1"].ConnectionString;
    SqlConnection conn = new SqlConnection(connection);

    using (SqlCommand cmd = new SqlCommand("dbo.GetRandomPlace", conn))
    {
        cmd.CommandType = CommandType.StoredProcedure;

        // Sets up the parameter
        cmd.Parameters.Add("@OutputVar", SqlDbType.VarChar, 25).Direction = ParameterDirection.Output;

        // Opens the SQL connection & execute's the 'GetRandomPlace' sproc
        conn.Open();
        cmd.ExecuteNonQuery();

        // Reads the output value from @OutputVar in the sproc
        string place = Convert.ToString(cmd.Parameters["@OutputVar"].Value);
        conn.Close();

        // Populates the input field
        txtDestination.Text = place;
    }

    // Generates & updates past places table
    SqlPastPlaces.InsertParameters["Past_Place"].DefaultValue = ((TextBox)txtDestination).Text;
    SqlPastPlaces.InsertParameters["Month"].DefaultValue = DateTime.Now.ToString("MMMM");
    SqlPastPlaces.InsertParameters["Click_Date"].DefaultValue = DateTime.Now.ToString();

    SqlPastPlaces.Insert();
}

Using Reader:使用阅读器:

SqlConnection connection = new SqlConnection(ConnectionString);

command = new SqlCommand("TestProcedure", connection);
command.CommandType = System.Data.CommandType.StoredProcedure;
connection.Open();
reader = command.ExecuteReader();

List<Test> TestList = new List<Test>();
Test test;

while (reader.Read())
{
    test = new Test();
    test.ID = int.Parse(reader["ID"].ToString());
    test.Name = reader["Name"].ToString();
    TestList.Add(test);
}
connection.Close();

Your problem is that you are using ExecuteNonQuery() which by its nature ignores any data you might get back.您的问题是您使用的是ExecuteNonQuery() ,它本质上会忽略您可能返回的任何数据。 You need to either add output parameters to your procedure or use SqlCommand.ExecuteReader to get a data reader back and then read the information from that.您需要将输出参​​数添加到您的过程或使用SqlCommand.ExecuteReader来获取数据读取器,然后从中读取信息。

Managed it using the following使用以下方法管理它

string connection = ConfigurationManager.ConnectionStrings["PaydayLunchConnectionString1"].ConnectionString;
        SqlConnection conn = new SqlConnection(connection);

        SqlCommand com = new SqlCommand(
            "SELECT        TOP (1) Month " +
            "FROM          Past_Places", conn);

        try
        {
            conn.Open();
            SqlDataReader reader = com.ExecuteReader();
            while (reader.Read())
            {
                PastMonth.Text = reader["Month"].ToString();
            }
            reader.Close();
        }
        finally
        {
            conn.Close();
        }

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

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