简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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