简体   繁体   中英

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.

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
  • 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.

I need this value so I can use it in an IF statement in my view.

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. 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.

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();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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