Error casting CHAR(1) SQL column as “char” in code

前端 未结 3 1468
小蘑菇
小蘑菇 2021-01-26 16:31

I\'m selecting from a SQL Server 2000 box CHAR(1) column called Combo_Label - it will always have a single A..Z character in

相关标签:
3条回答
  • 2021-01-26 16:39

    The database and the Db access APIs have no concept of char. Your CHAR(1) is mapped to string.

    probably the most sensible option:

      string comboLabel = (string)formCombo.Rows[j]["Combo_Label"];
    

    but if you really want a char :

      char comboLabel = ((string)formCombo.Rows[j]["Combo_Label"])[0];
    
    0 讨论(0)
  • 2021-01-26 16:44

    The field is a string , you can do something like that but also need to consider if hte field is null or an emmpty string

    char comboLabel = ((string)formCombo.Rows[j]["Combo_Label"])[0];

    0 讨论(0)
  • 2021-01-26 16:56

    first convert that to a string, then get first character...

    char comboLabel = formCombo.Rows[j]["Combo_Label"].ToString()[0];
    
    0 讨论(0)
提交回复
热议问题