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

时间:2015-10-27 12:38:03

标签: c# sql asp.net ado.net sqldatareader

我有一个存储过程,如下所示,它从我的数据库中检索前三行。

存储过程

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

我的数据库包含以下列:

  • ID
  • Past_Place
  • Click_Date

我想要从我的数据库的顶行获取'Months'值,但我不知道如何在我的代码后面写这个,因为所有解决方案我找到填充gridview或者有参数在其中min并不需要它们。

我需要这个值,所以我可以在我的视图中的IF语句中使用它。

只是你知道我必须遵循使用另一个存储过程更新表的代码。

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

过去4小时我一直坚持这个问题,真的需要一些帮助。

3 个答案:

答案 0 :(得分:1)

使用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();

答案 1 :(得分:1)

您的问题是您使用的ExecuteNonQuery()本质上忽略了您可能会收到的任何数据。您需要将输出参​​数添加到您的过程中,或使用SqlCommand.ExecuteReader来获取数据读取器,然后从中读取信息。

答案 2 :(得分:0)

使用以下

管理它
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();
        }
相关问题