简体   繁体   English

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

[英]Export SQL Server data into a specific sheet in Excel

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); 

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM