簡體   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