我有一个Excel文件正在连接并用于从中读取数据以完成一个过程
以下是代码:
public void eConnection(string srcString, string id)
{
tmpArry = new ArrayList();
GlobalTestFlow = new Dictionary<string, string>();
StepData = new Dictionary<string, string>();
innerData = new Dictionary<string, string>();
setTestID(id);
conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+srcString+"; Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
connection = new OleDbConnection(conString);
connection.Open();
}
我使用它创建第一个连接,然后使用引用原始连接的另一个项目中的代码读取Excel文件:
public static string testID;
public eDriver driver;
private string conSrc = "'C:Desktop\\Test Data\\OLBBAppTestData.xlsx'";
private string workSheet = "Global";
public void OLBBFrinLnLoanTest()
{
testID = "1"; // Dynamic per test case generated
driver = new eDriver();
driver.eConnection(conSrc, testID); //Connection is made to Excel here
driver.getTestFlow(workSheet); //Grab the worksheet to read data from
foreach (string tmp in driver.GlobalTestFlow.Keys)
{
switch (driver.GlobalTestFlow[tmp].ToString())
{
case "OpenActivateLoan": //Find data that matches "OpenActivateLoan"
cOLBBOpenActivateLoan exeOpenActivateLoan = new cOLBBOpenActivateLoan();
exeOpenActivateLoan.StandAlone(driver, driver.GlobalTestFlow[tmp].ToString()); //Execute this once data matches
break;
default:
break;
}
}
调用exeOpenActivateLoan后,将调用另一个类来继续该过程:
namespace OLBBv2.Components
{
class cOLBBOpenActivateLoan
{
private mOLBBOpenActivateLoan loanModule;
internal void StandAlone(eDriver e, string worksheet)
{
e.getStepData(worksheet); //find step Data to continue workflow
loanModule = new mOLBBOpenActivateLoan();
foreach (string tmp in e.StepData.Keys)
{
switch (tmp)
{
case "i_ClickActionButton":
//calling this function to insert back into Excel file, This is where I would like to close the original connection
loanModule._ClickSubmitButton();
break;
default:
break;
}
}
e.ResetStepData();
}
}
}
我想在调用函数之前关闭连接,因为被调用的函数将更新Excel文件
以下是为更新Excel文件而调用的代码:
public void _InsertLoanActNumSegmentToExcel()
{
eWriter writer = new eWriter();
string loanAct = "applesauce";
writer.insertToDataSheet(loanAct);
}
在eWrirter类中,我建立了一个与Excel文件的新连接以写入它:
public void insertToDataSheet(string loanAct)
{
try
{
string srcExcel = "C:\\Test Data\\OLBBAppTestData.xlsx";
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null;
MyConnection = new OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + srcExcel + "; Extended Properties = 'Excel 8.0;HDR=Yes;'");
MyConnection.Open();
myCommand.Connection = MyConnection;
sql = "UPDATE [Global$] SET o_AccountNumber ='"+loanAct+"' WHERE TestID='"+getTestID()+"'";
myCommand.CommandText = sql;
//Exception gets thrown here with error message "Operation must use an updateable query"
myCommand.ExecuteNonQuery();
MessageBox.Show("Insert Success!");
MyConnection.Close();
}
catch (Exception eInnerData)
{
MessageBox.Show("Method: getInnerData " + eInnerData.ToString());
}
}
尝试执行查询后,我收到错误,因为从我读数据时仍然打开连接,我想关闭连接,然后重新打开它以插回Excel文件
我要做的是从Excel文件中读取,然后在新连接上更新Excel文件