【发布时间】:2014-01-30 15:04:49
【问题描述】:
我正在尝试访问Class 中的List,以便可以在另一个Insert 语句中使用这些值。我的想法是,我使用 excel 电子表格中的值(在 LegalTransactionList Class 中进行)使用 for loop 填充 List。然后,我将把这些值和insert 放入数据库表中(发生在 Helpers Class 中)。我在 lstOfTran.AccountNumber 上收到一条错误消息,指出 LegalTransactionRec 不包含 AccountNumber 的定义。任何帮助将不胜感激。
class LegalTransactionList
{
public static List<LegalTransactionRec> setTransactions()
{
Form1 form = new Form1();
Workbook workbook = form.excelApp.Workbooks.Open(form.txtbxFilename.Text);
List<LegalTransactionRec> lstTran = new List<LegalTransactionRec>();
try
{
//workbook = excelApp.Workbooks.Open(txtbxFilename.Text); View above comment
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1);
Microsoft.Office.Interop.Excel.Range xlRange = worksheet.UsedRange;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
for (int i = 1; i <= rowCount; i++)
{
for (int j = 1; j <= colCount; j++)
{
lstTran.Add(new LegalTransactionRec()
{
AccountNumber = Form1.CleanString(xlRange.Cells[i, 1].Value2.ToString()),
CostAmount = Form1.TryToParse(Form1.CleanAmount(xlRange.Cells[i, 3].Value2.ToString())),
SSN = Form1.CleanString(xlRange.Cells[i, 6].Value2.ToString()),
TransactionDate = Form1.CheckDate(xlRange.Cells[i, 2].Value2.ToDate()),
Description = xlRange.Cells[i, 8].Value2.ToString(),
TransactionCode = Form1.CheckNull(xlRange.Cells[i, 4].Value2.ToInt())
});
}
}
if (form.validateHeader(worksheet))
{
}
}
catch (Exception ex)
{
}
return lstTran;
}
}
class Helpers
{
public void insertRecords()
{
StringBuilder sql = new StringBuilder();
var lstOfTran = LegalTransactionList.setTransactions();
using (DataTable dt = DataManager.GetData(sql))
{
foreach (DataRow dr in dt.Rows)
{
try
{
sql.AppendLine("INSERT INTO LEGAL_TRANSCATIONS (BATCH_ID, ACCOUNT, ATTORNEY_ID, ORG_ID, TRANSACTION_DATE, DATE_INSERTED, TRANSACTION_CODE, AMOUNT, DESCRIPTION, DEBTOR_SSN");
sql.AppendLine("VALUES ( (select max(batch_id) from legal_transaction_batch_info)," + lstOfTran.AccountNumber + );
}
catch (Exception ex)
{ }
}
}
}
}
public class LegalTransactionRec
{
public string AccountNumber { get; set; }
public string CostAmount { get; set; }
public string SSN { get; set; }
public int BatchID { get; set; }
public Attorney Attorney { get; set; }
public DateTime TransactionDate { get; set; }
public string Description { get; set; }
public int TransactionCode { get; set; }
}
public int CheckNull(int intVal)
{
int cleanValue;
if (intVal == null)
{
throw new System.ArgumentException("Value cannot be null", "original");
}
else
{
cleanValue = intVal;
}
return cleanValue;
}
public string CleanString(String strVal)
{
string cleanValue;
if (strVal == null)
{
throw new System.ArgumentException("Value cannot be null", "original");
}
else
{
cleanValue = Regex.Replace(strVal, " ", "").Replace("$", "").Replace("-", "");
}
return cleanValue;
}
public string CleanAmount(String amt)
{
string cleanAmt;
if (amt == null)
{
throw new System.ArgumentException("Value cannot be null", "original");
}
else
{
cleanAmt = Regex.Replace(amt, "(", "-").Replace(")", "").Replace("$", "").Replace("[^0-9]+", "").Replace(" ", "");
}
return cleanAmt;
}
public static void TryToParse(string strAmt)
{
decimal decAmt;
bool result = Decimal.TryParse(strAmt, out decAmt);
}
public DateTime CheckDate(DateTime tranDate)
{
DateTime date;
if (tranDate == null)
{
throw new System.ArgumentException("Value cannot be null", "original");
}
else
{
date = tranDate;
}
return date;
}
【问题讨论】:
-
是否
LegalTransactionRec包含AccountNumber属性? -
另外,不要吞下异常 - 取出空的
catch{}块并处理错误或让它冒泡。 -
在 OP 中添加
LegalTransactionRec类。 -
@DStanley 这是一个非常粗略的草稿,我希望在添加此类内容之前先让程序的骨架工作。我知道这可能不是做事的最佳方式,但因为我正在学习 C#,所以这就是我喜欢做事的方式。不过谢谢。
-
那么一开始就不要添加try/catch,让任何错误自然冒出来。您将花费更多时间试图找出发生错误的哪里,因为它们被catch 块抛出。添加 try/catch 当您准备好处理异常时。