代码之家  ›  专栏  ›  技术社区  ›  user2405778

左键未添加前导零

  •  0
  • user2405778  · 技术社区  · 11 年前

    我添加了 PadLeft(8,'0') 以确保插入到数据库表中的所有帐户至少有8个字符长。代码如下:

    public static string CleanAccount(String strVal)
    {
        string cleanValue;
        string paddedAccount = strVal.PadLeft(8,'0');
        //MessageBox.Show("account: " + paddedAccount);
    
        if (paddedAccount == null)
        {
            throw new System.ArgumentException("Value cannot be null", "original");
        }
        else
        {
            cleanValue = paddedAccount.Replace(" ", "").Replace("$", "").Replace("-", "");
        }
    
        return cleanValue;
    }
    

    Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1);
    Microsoft.Office.Interop.Excel.Range xlRange = worksheet.UsedRange;
    
    long fullRow = worksheet.Rows.Count;
    long lastRow = worksheet.Cells[fullRow, 1].End(Microsoft.Office.Interop.Excel.XlDirection.xlUp).Row;
    int colCount = xlRange.Columns.Count;
    
    for (int i = 2; i <= lastRow; i++)
    {
        lstTran.Add(new LegalTransactionRec()
                        {
                            AccountNumber = Form1.CleanAccount(xlRange.Cells[i, 1].Value2.ToString()),
                            CostAmount = Form1.CleanAmount(Form1.TryToParse(xlRange.Cells[i, 3].Value2.ToString())),
                            SSN = Form1.CleanString(xlRange.Cells[i, 6].Value2.ToString()),
                            TransactionDate = Form1.ConvertToDateTime(xlRange.Cells[i, 2].Value),
                            Description = Form1.CleanDescription(xlRange.Cells[i, 8].Value2.ToString()),
                            TransactionCode = Form1.CleanTranCode(Form1.CleanExtra(xlRange.Cells[i, 4].Value2.ToString()))
                        }
                   );
    }
    

    MessageBox 没有注释掉,我看到该帐户确实被填充了,但在将其添加到数据库表中后,它显示为没有前导零。为什么会这样?

    1 回复  |  直到 11 年前
        1
  •  1
  •   user2405778    11 年前

    这个错误是一个用户错误,我是在汉斯·帕桑(Hans Passant)在开场白的评论中提到它的同时发现的。

    这是我的代码中的更改:

    sql.AppendLine(trans.AccountNumber + ",");
    

    sql.AppendLine("'" + trans.AccountNumber + "',");