繁体   English   中英

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

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

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

存储过程

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
  • 过去的地方
  • 点击日期

我想从我的数据库中的第一行获取“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();
}

使用阅读器:

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

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

使用以下方法管理它

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