รับค่าพารามิเตอร์เอาต์พุตใน ADO.NET


96

กระบวนงานที่เก็บไว้ของฉันมีพารามิเตอร์เอาต์พุต:

@ID INT OUT

ฉันจะดึงข้อมูลนี้โดยใช้ adm.net ได้อย่างไร?

using (SqlConnection conn = new SqlConnection(...))
{
    SqlCommand cmd = new SqlCommand("sproc", conn);
    cmd.CommandType = CommandType.StoredProcedure;

    // add parameters

    conn.Open();

    // *** read output parameter here, how?
    conn.Close();
}

คำตอบ:


120

แสดงให้เห็นว่าการตอบสนองอื่น ๆ แต่เป็นหลักคุณเพียงแค่ต้องสร้างSqlParameterการตั้งค่าDirectionการOutputและเพิ่มไปยังSqlCommand's Parametersคอลเลกชัน จากนั้นรันโพรซีเดอร์ที่เก็บไว้และรับค่าของพารามิเตอร์

ใช้ตัวอย่างโค้ดของคุณ:

// SqlConnection and SqlCommand are IDisposable, so stack a couple using()'s
using (SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand("sproc", conn))
{
   // Create parameter with Direction as Output (and correct name and type)
   SqlParameter outputIdParam = new SqlParameter("@ID", SqlDbType.Int)
   { 
      Direction = ParameterDirection.Output 
   };

   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Parameters.Add(outputIdParam);

   conn.Open();
   cmd.ExecuteNonQuery();

   // Some various ways to grab the output depending on how you would like to
   // handle a null value returned from the query (shown in comment for each).

   // Note: You can use either the SqlParameter variable declared
   // above or access it through the Parameters collection by name:
   //   outputIdParam.Value == cmd.Parameters["@ID"].Value

   // Throws FormatException
   int idFromString = int.Parse(outputIdParam.Value.ToString());

   // Throws InvalidCastException
   int idFromCast = (int)outputIdParam.Value; 

   // idAsNullableInt remains null
   int? idAsNullableInt = outputIdParam.Value as int?; 

   // idOrDefaultValue is 0 (or any other value specified to the ?? operator)
   int idOrDefaultValue = outputIdParam.Value as int? ?? default(int); 

   conn.Close();
}

โปรดใช้ความระมัดระวังในการรับParameters[].Valueเนื่องจากประเภทจะต้องถูกโยนจากobjectสิ่งที่คุณประกาศว่าเป็น และSqlDbTypeใช้เมื่อคุณสร้างSqlParameterความต้องการให้ตรงกับประเภทในฐานข้อมูล หากคุณกำลังจะเอาท์พุทไปที่คอนโซลคุณอาจใช้Parameters["@Param"].Value.ToString()( โดยชัดแจ้งหรือโดยปริยายผ่านการโทรConsole.Write()หรือการString.Format()โทร)

แก้ไข: กว่า 3.5 ปีและมีการดูเกือบ 20k ครั้งและไม่มีใครใส่ใจที่จะพูดถึงว่ามันไม่ได้รวบรวมด้วยเหตุผลที่ระบุไว้ในความคิดเห็น "ระวัง" ของฉันในโพสต์ต้นฉบับ ดี. แก้ไขตามความคิดเห็นที่ดีจาก @Walter Stabosz และ @Stephen Kennedy และเพื่อให้ตรงกับการแก้ไขรหัสอัปเดตในคำถามจาก @abatishchev


8
คุณไม่จำเป็นต้องconn.Close()อยู่ในusingบล็อก
Marcus

1
ฉันคิดว่าการใช้ int.MaxValue ของคุณเป็นคุณสมบัติ Size ไม่ถูกต้อง int.MaxValue เป็นค่าคงที่ที่มีค่า 2,147,483,647 msdn.microsoft.com/en-us/library/… . ข้อผิดพลาดนี้ไม่เป็นอันตรายในตัวอย่างนี้เนื่องจากประเภทข้อมูลคือ Int และ "สำหรับชนิดข้อมูลที่มีความยาวคงที่ค่าของ Size จะถูกละเว้น" แต่ค่าศูนย์จะเพียงพอ
Walter Stabosz

ค่าเป็นวัตถุประเภทดังนั้นการกำหนดให้กับ int โดยตรงโดยไม่ต้องแคสต์จะไม่ได้ผล
Stephen Kennedy

1
สำหรับผู้ที่ใช้ DataReader คุณต้องปิดหรืออ่านจนจบข้อมูลก่อนจึงจะสามารถดูพารามิเตอร์เอาต์พุตได้
Garry English

57

สำหรับใครก็ตามที่ต้องการทำสิ่งที่คล้ายกันโดยใช้เครื่องอ่านที่มีขั้นตอนการจัดเก็บโปรดทราบว่าต้องปิดเครื่องอ่านเพื่อดึงค่าเอาต์พุต

using (SqlConnection conn = new SqlConnection())
{
    SqlCommand cmd = new SqlCommand("sproc", conn);
    cmd.CommandType = CommandType.StoredProcedure;

    // add parameters
    SqlParameter outputParam = cmd.Parameters.Add("@ID", SqlDbType.Int);
    outputParam.Direction = ParameterDirection.Output;

    conn.Open();

    using(IDataReader reader = cmd.ExecuteReader())
    {
        while(reader.Read())
        {
            //read in data
        }
    }
    // reader is closed/disposed after exiting the using statement
    int id = outputParam.Value;
}

4
ฉันพลาดความจริงที่ว่าต้องปิดเครื่องอ่านก่อนอ่านพารามิเตอร์เอาต์พุต ขอบคุณที่ชี้ให้ดู!
Nicklas Møller Jepsen

28

ไม่ใช่รหัสของฉัน แต่เป็นตัวอย่างที่ดีที่ฉันคิด

แหล่งที่มา: http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=624

using System; 
using System.Data; 
using System.Data.SqlClient; 


class OutputParams 
{ 
    [STAThread] 
    static void Main(string[] args) 
    { 

    using( SqlConnection cn = new SqlConnection("server=(local);Database=Northwind;user id=sa;password=;")) 
    { 
        SqlCommand cmd = new SqlCommand("CustOrderOne", cn); 
        cmd.CommandType=CommandType.StoredProcedure ; 

        SqlParameter parm= new SqlParameter("@CustomerID",SqlDbType.NChar) ; 
        parm.Value="ALFKI"; 
        parm.Direction =ParameterDirection.Input ; 
        cmd.Parameters.Add(parm); 

        SqlParameter parm2= new SqlParameter("@ProductName",SqlDbType.VarChar); 
        parm2.Size=50; 
        parm2.Direction=ParameterDirection.Output; 
        cmd.Parameters.Add(parm2); 

        SqlParameter parm3=new SqlParameter("@Quantity",SqlDbType.Int); 
        parm3.Direction=ParameterDirection.Output; 
        cmd.Parameters.Add(parm3);

        cn.Open(); 
        cmd.ExecuteNonQuery(); 
        cn.Close(); 

        Console.WriteLine(cmd.Parameters["@ProductName"].Value); 
        Console.WriteLine(cmd.Parameters["@Quantity"].Value.ToString());
        Console.ReadLine(); 
    } 
} 

2
ใช่นี่ถูกต้องแล้ว เพียงตั้งค่าคุณสมบัติ ParameterDirection ของพารามิเตอร์ คุณไม่จำเป็นต้องใช้บรรทัด cn.Close () - บล็อกที่ใช้ {} จะดูแลสิ่งนั้น
MusiGenesis

6
string ConnectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(ConnectionString))
{
//Create the SqlCommand object
SqlCommand cmd = new SqlCommand(“spAddEmployee”, con);

//Specify that the SqlCommand is a stored procedure
cmd.CommandType = System.Data.CommandType.StoredProcedure;

//Add the input parameters to the command object
cmd.Parameters.AddWithValue(“@Name”, txtEmployeeName.Text);
cmd.Parameters.AddWithValue(“@Gender”, ddlGender.SelectedValue);
cmd.Parameters.AddWithValue(“@Salary”, txtSalary.Text);

//Add the output parameter to the command object
SqlParameter outPutParameter = new SqlParameter();
outPutParameter.ParameterName = “@EmployeeId”;
outPutParameter.SqlDbType = System.Data.SqlDbType.Int;
outPutParameter.Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.Add(outPutParameter);

//Open the connection and execute the query
con.Open();
cmd.ExecuteNonQuery();

//Retrieve the value of the output parameter
string EmployeeId = outPutParameter.Value.ToString();
}

แบบอักษรhttp://www.codeproject.com/Articles/748619/ADO-NET-How-to-call-a-stored-procedure-with-output


6
public static class SqlParameterExtensions
{
    public static T GetValueOrDefault<T>(this SqlParameter sqlParameter)
    {
        if (sqlParameter.Value == DBNull.Value 
            || sqlParameter.Value == null)
        {
            if (typeof(T).IsValueType)
                return (T)Activator.CreateInstance(typeof(T));

            return (default(T));
        }

        return (T)sqlParameter.Value;
    }
}


// Usage
using (SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand("storedProcedure", conn))
{
   SqlParameter outputIdParam = new SqlParameter("@ID", SqlDbType.Int)
   { 
      Direction = ParameterDirection.Output 
   };

   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Parameters.Add(outputIdParam);

   conn.Open();
   cmd.ExecuteNonQuery();

   int result = outputIdParam.GetValueOrDefault<int>();
}

3

คุณสามารถรับผลลัพธ์ได้ตามรหัสด้านล่าง ::

using (SqlConnection conn = new SqlConnection(...))
{
    SqlCommand cmd = new SqlCommand("sproc", conn);
    cmd.CommandType = CommandType.StoredProcedure;

    // add other parameters parameters

    //Add the output parameter to the command object
    SqlParameter outPutParameter = new SqlParameter();
    outPutParameter.ParameterName = "@Id";
    outPutParameter.SqlDbType = System.Data.SqlDbType.Int;
    outPutParameter.Direction = System.Data.ParameterDirection.Output;
    cmd.Parameters.Add(outPutParameter);

    conn.Open();
    cmd.ExecuteNonQuery();

    //Retrieve the value of the output parameter
    string Id = outPutParameter.Value.ToString();

    // *** read output parameter here, how?
    conn.Close();
}

2

สร้าง SqlParamObject ซึ่งจะให้คุณควบคุมวิธีการเข้าถึงพารามิเตอร์

:

SqlParameter param = SqlParameter ใหม่ ();

ตั้งชื่อสำหรับพารามิเตอร์ของคุณ (ควร b เหมือนกับที่คุณประกาศตัวแปรเพื่อเก็บค่าในฐานข้อมูลของคุณ)

: param.ParameterName = "@yourParamterName";

ล้างตัวยึดค่าเพื่อเก็บข้อมูลเอาต์พุตของคุณ

: param.Value = 0;

กำหนดทิศทางที่คุณเลือก (ในกรณีของคุณควรเป็น Output)

: param.Direction = System.Data.ParameterDirection.Output;


1

นั่นดูชัดเจนกว่าสำหรับฉัน:

int? id = outputIdParam.Value คือ DbNull? ค่าเริ่มต้น (int?): outputIdParam.Value;

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.