如何在Excel中使用c#改变odc连接文件path

我有一个Excel文件(ac#项目中的embedded式资源)与多个工作表,从SQL Server使用odc文件的连接信息获取他们的数据。 我想更新excel文件,在我指定的特定文件夹中查找这些odc文件,因此需要相应地更新excel odc连接文件path。 我怎么能通过C#做到这一点,例如,如果我输出的odc文件“d:\ odcFiles \ abc.odc”,那么我想更新Excel连接path为“d \ odcFiles \ abc.odc”。 同样对于所有其他的odc文件。 任何帮助将非常感激。

突出显示的部分是我所指的(odc文件的位置),我想通过c#在飞行中更改:

在这里输入图像说明

一种方法可能是最简单的方法,就是直接编辑registry。

[更新]我不知道ODBC。 但是我用这个代码来dynamic访问excel文件。 这是一个asp.net应用程序,但重要的数据库的东西在那里。

// using System.Data.OleDb OleDbConnection ExcelConection = null; OleDbCommand ExcelCommand = null; OleDbDataReader ExcelReader = null; OleDbConnectionStringBuilder OleStringBuilder = null; try { OleStringBuilder = new OleDbConnectionStringBuilder(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';"); OleStringBuilder.DataSource = MapPath(@"~\App_Datav\MyExcelWorksheet.xls"); ExcelConection = new OleDbConnection(); ExcelConection.ConnectionString = OleStringBuilder.ConnectionString; ExcelCommand = new OleDbCommand(); ExcelCommand.Connection = ExcelConection; ExcelCommand.CommandText = "Select * From [Sheet1$]"; ExcelConection.Open(); ExcelReader = ExcelCommand.ExecuteReader(); GridView1.DataSource = ExcelReader; GridView1.DataBind(); } catch (Exception Args) { LabelErrorMsg.Text = "Could not open Excel file: " + Args.Message; } finally { if (ExcelCommand != null) ExcelCommand.Dispose(); if (ExcelReader != null) ExcelReader.Dispose(); if (ExcelConection != null) ExcelConection.Dispose(); }