C#SQL Server字符串在检索过程中铸造为INT



i具有以下C#代码,可以从SQL Server数据库中检索一堆文字字符串。该表包含几列,其中包括一个和两个字符的代码长度的混合物,例如" at"," 01"," BC"。列数据类型为varchar,codeSheets是microsoft.office.interop.excel._worksheet。

问题是,当我获得" 01"作为查询结果时,Excel表显示1而不是01 。当我在表上进行进一步分析时,这会导致问题。我怀疑在检索期间,01以某种方式将其施放为整数而不是字符串。有人可以指向我为什么会发生这种情况吗?

sqlString = "SELECT DISTINCT(BUSCODES) AS COLCODES FROM ANALYSISTABLE";
SqlCommand codeRetrieval = new SqlCommand(sqlString, dbConn);
codeRetrieval.CommandTimeout = 0;
SqlDataReader codeReader = codeRetrieval.ExecuteReader();

while (codeReader.Read())
{
    if (codeReader["COLCODE"] == DBNull.Value)
        codeSheets.Cells[1, colIndex] = "NULL";
    else if (string.Compare(codeReader["COLCODE"].ToString(), "") == 0)
        codeSheets.Cells[1, colIndex] = "Empty String";
    else if (string.Compare(codeReader["COLCODE"].ToString(), " ") == 0)
        codeSheets.Cells[1, colIndex] = "Single Space";
    else if (string.Compare(codeReader["COLCODE"].ToString(), "  ") == 0)
        codeSheets.Cells[1, colIndex] = "Double Space";
    else
        codeSheets.Cells[1, colIndex] = codeReader["COLCODE"].ToString();
    colIndex++;
}

添加此行:

    codeSheets.Cells[1, colIndex].NumberFormat = "@"; // cell as a text

或在此之前将列格式为文本,如下所示:格式化excel列(或单元格)为c#?

中的文本

最新更新