• What size do you use for varchar(MAX) in your parameter declaration?


    What size do you use for varchar(MAX) in your parameter declaration?

    In this case you use -1.

    See also MSDN docs: msdn.microsoft.com/en-us/library/bb399384.aspx

    Using Large Value Type Parameters

    Large value types can be used in SqlParameter objects the same way you use smaller value types in SqlParameter objects. You can retrieve large value types as SqlParameter values, as shown in the following example. The code assumes that the following GetDocumentSummary stored procedure exists in the AdventureWorks sample database. The stored procedure takes an input parameter named @DocumentID and returns the contents of the DocumentSummary column in the @DocumentSummary output parameter.

     
    CREATE PROCEDURE GetDocumentSummary   
    (  
        @DocumentID int,  
        @DocumentSummary nvarchar(MAX) OUTPUT  
    )  
    AS  
    SET NOCOUNT ON  
    SELECT  @DocumentSummary=Convert(nvarchar(MAX), DocumentSummary)  
    FROM    Production.Document  
    WHERE   DocumentID=@DocumentID  
    

    Example

    The ADO.NET code creates SqlConnection and SqlCommand objects to execute the GetDocumentSummary stored procedure and retrieve the document summary, which is stored as a large value type. The code passes a value for the @DocumentID input parameter, and displays the results passed back in the @DocumentSummary output parameter in the Console window.

    C#
    static private string GetDocumentSummary(int documentID)
    {
        //Assumes GetConnectionString returns a valid connection string.
        using (SqlConnection connection =
                   new SqlConnection(GetConnectionString()))
        {
            connection.Open();
            SqlCommand command = connection.CreateCommand();
            try
            {
                // Setup the command to execute the stored procedure.
                command.CommandText = "GetDocumentSummary";
                command.CommandType = CommandType.StoredProcedure;
    
                // Set up the input parameter for the DocumentID.
                SqlParameter paramID =
                    new SqlParameter("@DocumentID", SqlDbType.Int);
                paramID.Value = documentID;
                command.Parameters.Add(paramID);
    
                // Set up the output parameter to retrieve the summary.
                SqlParameter paramSummary =
                    new SqlParameter("@DocumentSummary",
                    SqlDbType.NVarChar, -1);
                paramSummary.Direction = ParameterDirection.Output;
                command.Parameters.Add(paramSummary);
    
                // Execute the stored procedure.
                command.ExecuteNonQuery();
                Console.WriteLine((String)(paramSummary.Value));
                return (String)(paramSummary.Value);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }
    }
  • 相关阅读:
    列表
    break和continue
    第三天下午
    第二天
    简历,面试
    周三 景安
    应届生求职优势(我的求职心…

    2013年01月02日
    Click and Drag
  • 原文地址:https://www.cnblogs.com/chucklu/p/10821017.html
Copyright © 2020-2023  润新知