Converting between SQL char and C#

前端 未结 3 1756
青春惊慌失措
青春惊慌失措 2021-01-12 18:47

If I want to insert into a database column of type char(2) from C#. What data type would I use in C#?

If using the following code:

private static Sq         


        
相关标签:
3条回答
  • 2021-01-12 19:05

    string will work fine if it is 2 characters or shorter

    0 讨论(0)
  • 2021-01-12 19:05

    char, varchar, nchar, nvarchar are actually strings

    the size helps to determine how long the string is...

    by the way

    char has a fixed length, so if you want to have "1" in a char(2) the contents will be actual "1 "

    varchar(2) will be "1"

    the n part stands for unicode, so everything inside those fields will be in Unicode.


    normally we use nvarchar to save some space on the data, as if you have a char(250) the database will always save the full length, as an empty varchar(250) will be nothing.

    In our programming language we then use padding to do what char does, for example, in C#

    "1".PadLeft(2);
    "1".PadRight(2);
    

    will output " 1" and "1 " respectively.

    0 讨论(0)
  • 2021-01-12 19:13

    try using AddWithValue method and parce it as string,

    it is only a one line. no need to have a seperate method.

    command.Parameters.AddWithValue(parameterName, parameterValue);
    
    0 讨论(0)
提交回复
热议问题