我正在尝试访问List
中的Class
,因此我可以在另一个Insert
语句中使用这些值。我的想法是,我使用List
使用excel电子表格中的值填充for loop
(发生在LegalTransactionList Class
中)。然后我将把这些值和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;
}
答案 0 :(得分:3)
您正尝试访问lstOfTran
类型的LegalTransactionRec
列表中的属性,该列表不需要AccountNumber
属性。我想你要尝试的是lstOfTran[i].AccountNumber
。
答案 1 :(得分:2)
编辑:
这行甚至没有编译:
sql.AppendLine("VALUES ( (select max(batch_id) from legal_transaction_batch_info)," + lstOfTran.AccountNumber + );
+
之后发生了什么?
深入研究:
AccountNumber = Form1.CleanString(xlRange.Cells[i, 1].Value2.ToString()),
看起来CleanString()
会返回意外的内容。你能展示实施吗?
答案 2 :(得分:2)
您正在寻找AccountNumber
上的lstOfTran
:
var lstOfTran = LegalTransactionList.setTransactions();
...
sql.AppendLine("..." + lstOfTran.AccountNumber);
但LegalTransactionList.setTransactions()
会返回LegalTransactionRec
的列表。 列表没有该属性,列表中的项也有。
如果您打算遍历事务列表,那么它将类似于:
StringBuilder sql = new StringBuilder();
var lstOfTran = LegalTransactionList.setTransactions();
foreach (LegalTransactionRec trans in lstOfTran )
{
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),"
+ trans.AccountNumber + );
// ^-- use loop variable here
}
其他注意事项:
xlRange.Cells[i, j]
调用是COM调用,非常昂贵。如果首先将所有数据拉入数组,您会惊讶地发现它运行得多快多少。答案 3 :(得分:2)
setTransactions()
正在返回List<LegalTransactionRec>
并且您将var lstOfTran
设置为从
LegalTransactionList.setTransactions();
您需要在列表中指定您尝试访问的项目。
尝试改为:
lstOfTran[0].AccountNumber
您可能还需要更改
sql.AppendLine("VALUES ( (select max(batch_id) from legal_transaction_batch_info)," + lstOfTran.AccountNumber + );
到
sql.AppendLine("VALUES ( (select max(batch_id) from legal_transaction_batch_info)," + lstOfTran.AccountNumber );
注意我在+
lstOfTran.AccountNumber