ADO.NET - The Size property has an invalid size of 0

前端 未结 10 1042
小蘑菇
小蘑菇 2020-12-14 13:41

I\'m trying to get output value from DB via ADO.NET. There\'s a client code:

    using (var connection = new SqlConnection(ConnectionString))
    {
                


        
10条回答
  •  别那么骄傲
    2020-12-14 14:23

    you have to set Size for parameter output

    using (var connection = new SqlConnection(ConnectionString))
    {
        connection.Open();
        SqlCommand command = new SqlCommand("pDoSomethingParamsRes", connection);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@i", 1);
        var outParam = new SqlParameter("@out", SqlDbType.VarChar);
        outParam.Direction = ParameterDirection.Output;
        outParam.Size = 50; // this is example
        command.Parameters.Add(outParam);
        command.ExecuteNonQuery();
        Console.WriteLine(command.Parameters["@out"].Value.ToString());
    }
    

提交回复
热议问题