Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检索过程中转换为Int的C#SQL Server字符串_C#_Sql Server - Fatal编程技术网

检索过程中转换为Int的C#SQL Server字符串

检索过程中转换为Int的C#SQL Server字符串,c#,sql-server,C#,Sql Server,我使用下面的c代码从SQL Server数据库中检索一组文本字符串。该表包含多个列,其中混合了一个和两个字符长度的代码,例如“AT”、“01”、“BC”。列数据类型为VARCHAR,代码表为Microsoft.Office.Interop.Excel.\u工作表 问题是当我得到“01”作为查询结果时,Excel工作表显示1而不是01。当我对表进行进一步分析时,这会导致问题。我怀疑在检索过程中,01以某种方式被转换为整数而不是字符串。有人能告诉我为什么会这样吗 sqlString = "SELEC

我使用下面的c代码从SQL Server数据库中检索一组文本字符串。该表包含多个列,其中混合了一个和两个字符长度的代码,例如“AT”、“01”、“BC”。列数据类型为VARCHAR,代码表为Microsoft.Office.Interop.Excel.\u工作表

问题是当我得到“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单元格格式化为数字?您需要将单元格格式化为文本。谢谢你们的评论,我从没想过Excel会自动格式化它。我假设如果我存储字符串类型,它将是Excel中的字符串类型。但是我想我错了,哈哈。非常感谢,我从来没有想过这会是Excel的问题。@BrianWang很乐意帮忙!