将SQL Server数据导出到Excel中的特定工作表

时间:2021-04-19 10:20:08

I am trying to replicate a defunct homegrown program that I don't have access to the source code. Basically I need to read in a SQL file (here denoted as querySqlAddresses[i]), execute it and dump the result into a specific sheet in a file that I have open.

我试图复制一个我无法访问源代码的已经解散的本地程序。基本上我需要读取一个SQL文件(此处表示为querySqlAddresses [i]),执行它并将结果转储到我打开的文件中的特定工作表中。

I'm finding a lot of dead end things, but I think there may be promise in this, I am just not sure HOW to drop the "results" or even what the "results" variable is so I can target it. Does this even make sense?

我发现了许多死胡同,但我认为这可能有希望,我只是不确定如何放弃“结果”甚至“结果”变量是什么,所以我可以针对它。这甚至有意义吗?

string sqlConnectionString = "Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True";
FileInfo file = new FileInfo(querySqlAddresses[i]);
string script = file.OpenText().ReadToEnd();

SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script); 

1 个解决方案

#1


1  

I actually found another way of doing it using OleDB connections and passing the User Name, Password and Table space into the connection string.

我实际上找到了另一种使用OleDB连接并将用户名,密码和表空间传递到连接字符串的方法。

        string connectionString = "Provider=OraOLEDB.Oracle;Data Source=" + tableSpace + ";User Id=" + userName + ";Password=" + password + ";";
        System.Data.OleDb.OleDbConnection cnn = new System.Data.OleDb.OleDbConnection(connectionString);
        cnn.Open();
        System.Data.OleDb.OleDbDataAdapter Dadpt = new System.Data.OleDb.OleDbDataAdapter(readText, cnn);
        DataSet ds = new DataSet();
        Dadpt.Fill(ds);
        cnn.Close();

#1


1  

I actually found another way of doing it using OleDB connections and passing the User Name, Password and Table space into the connection string.

我实际上找到了另一种使用OleDB连接并将用户名,密码和表空间传递到连接字符串的方法。

        string connectionString = "Provider=OraOLEDB.Oracle;Data Source=" + tableSpace + ";User Id=" + userName + ";Password=" + password + ";";
        System.Data.OleDb.OleDbConnection cnn = new System.Data.OleDb.OleDbConnection(connectionString);
        cnn.Open();
        System.Data.OleDb.OleDbDataAdapter Dadpt = new System.Data.OleDb.OleDbDataAdapter(readText, cnn);
        DataSet ds = new DataSet();
        Dadpt.Fill(ds);
        cnn.Close();