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

前端 未结 5 811
眼角桃花
眼角桃花 2020-11-30 01:25

I normally set my column size when creating a parameter in ADO.NET

But what size do I use if the column is VARCHAR(MAX)?

cmd.Parameters.         


        
相关标签:
5条回答
  • 2020-11-30 01:57

    You do not need to pass the size parameter, just declare Varchar already understands that it is MAX like:

    cmd.Parameters.Add("@blah",SqlDbType.VarChar).Value = "some large text";
    
    0 讨论(0)
  • 2020-11-30 02:00

    The maximum SqlDbType.VarChar size is 2147483647.

    If you would use a generic oledb connection instead of sql, I found here there is also a LongVarChar datatype. Its max size is 2147483647.

    cmd.Parameters.Add("@blah", OleDbType.LongVarChar, -1).Value = "very big string";
    
    0 讨论(0)
  • 2020-11-30 02:10

    For those of us who did not see -1 by Michal Chaniewski, the complete line of code:

    cmd.Parameters.Add("@blah",SqlDbType.VarChar,-1).Value = "some large text";
    
    0 讨论(0)
  • 2020-11-30 02:11

    In this case you use -1.

    0 讨论(0)
  • 2020-11-30 02:18

    If you do something like this:

        cmd.Parameters.Add("@blah",SqlDbType.VarChar).Value = "some large text";
    

    size will be taken from "some large text".Length

    This can be problematic when it's an output parameter, you get back no more characters then you put as input.

    0 讨论(0)
提交回复
热议问题