I have to replace a VB6 function that allows to export a SQL Server table in a xls file using DAO.
In order to use Excel 2007-2010... files (xlsx), I tried to use this function to do that but it doesn't work.
Here is the DAO functionnality :
//Opening the SQLServer database with DAO
Set oBase= DBEngine.OpenDatabase(DSNName, dbDriverNoPrompt, False, "ODBC,PROVIDER=Microsoft.JET.OLEDB.3.51;DSN=DSNName;UID=user;PWD=pwd;DATABASE=SQLServerBDDName;Server=SQLServerName;")
//Query to export data
oBase.Execute("SELECT * INTO [SheetName] IN 'xlsFileName' 'Microsoft Excel 8.0;' FROM [SQLServerTableName]")
This function works perfectly with 97-2003 Excel files in xls but not with xlsx files.
Is there a possibility to make the same thing with ADO but without OPENROWSET statements or stored procedures ?
Thanks for your help
I finally found how to make in VB6 SQL Server export to Excel 2007 file with ADO and WITHOUT Office installed.
First, I use ADO to create xlsx file :
Dim l_oBase As New ADODB.connection
l_oBase.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myExcelFile.xlsx;Extended Properties=""Excel 12.0 Xml"""
Call l_oBase.Open
Then I execute a query to export a table in a sheet by using ODBC connection and DSN to connect to SQL Server database :
l_sReq = "SELECT * INTO [mySheet] FROM [ODBC;DSN=MyDSN;UID=LoginSQL;PWD=pwdSQL;Server=MySQLServer;Database=myDatabase].[TableName];"
Call l_oBase.Execute(l_sReq)
Call l_oBase.Close
When I execute the query in debug mode, I obtain an error on execute but xlsx file is correctly created and populated with data formats.
Related
I have created an Excel workbook with SQL server connection and loaded data using SQL commands. I want to use the same excel file in a different machine, there I want to change the database credentials. In our excel file, there are more than 10 SQL server connections and sheets, So recreate the file is very difficult. Is there any way to reuse the file with a different database?
I have copied the excel file with all .odc files, But I couldn't find an option to reset the connection to the new database.
I see two possibilities:
Edit the ODC files manually with a text editor
In Excel, use "Data" -> "Connections" to edit the connections
SSMS and SSRS to Excel enable for more than 255 columns when copy-pasting.
SSIS does not allow for more than 255 columns to be exported to Excel 2007.
Is there a way to override this?
Problem
There are a lot of Limitations when exporting to an Excel Files using Sql server data tools
Workarounds
You can do some workaround to achieve this:
Create a dataflowtask that export your data into a FlatFile (csv)
Store your Destination FileName in a Variable
Create another Dataflowtask that convert your csv file to an Excel File using a script task with a similar Function
Note: you have to add Microsoft.Office.Interop.Excel.dll file to the following directories (.Net Framework dll directory) C:\Windows\Microsoft.NET\Framework\v2.0.50727 and (sql server data tools dll directory) C:\Program Files\Microsoft SQL Server\100\DTS\Binn (using vs 2005 and sql 2008) and then add this dll as a reference in your script task
Imports Microsoft.Office.Interop
Public Sub ConvertCSVToExcel(Fromcsv As String, Toxlsx As String)
Dim Exl As New Excel.Application()
Try
Dim wb1 As Excel.Workbook = Exl.Workbooks.Open(Fromcsv, Format:=4)
wb1.SaveAs(Toxlsx, FileFormat:=XlFileFormat.xlOpenXMLWorkbook)
wb1.Close()
Exl.Quit()
Catch ex As Exception
Exl.DisplayAlerts = False
Exl.Quit()
End Try
End Sub
Third party components
Or you have to use a third party components like cozyRoc SSIS+
Side Note
if you are looking to Import data from excel with more than 255 columns you can follow this Link
References
Third party components
SSIS: Export more than 255 columns from SQL table to Exce
Cozyroc website
Workaround
convert csv to xlsx
trying to use custom assembly with script task in SSIS 2008 - can't find correct version of GACUtil
Refer the link. Best would be to create an script in SSIS to copy the content as csv format. You can use c# or VB.Net.
just for anyone come to this page and use SQL Server Import and Export Wizard to export to excel
in tab Review Data Type Mapping
section Data Type Mapping: ==> double click your column that has length more than 255 char open its Column Conversion Details Dialog then close it
then next to end...Lool
I tried to export data from SQL Server to excel, but it does not work.
My code is
INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0 Xml;HDR=YES;
Database=D:\FATXL.xlsx;',
'SELECT * FROM [Sheet1$]')
SELECT * FROM tabel1
Error
The 32-bit OLE DB provider "Microsoft.ACE.OLEDB.12.0" cannot be
loaded in-process on a 64-bit SQL Server.
I need to know if its possible to export data from 64 bit SQL Server to 32 bit MS Excel and how to do it?
Should we have same bit of SQL server and excel for this to work?
Here's are some alternative solutions which never fail.
Alternative solution 1
Caveat: You'd need to run this query manually in the SSMS.
Use Export to save you results in a CSV file which can be opened in Excel and saves as XLS.
Alternative solution 2
Caveat: You'd need to run this query manually in the SSMS.
You can change the Query results display options from grid to file which can be a CSV file and also check the SET NO COUNT option like below
Alternative solution 3
Caveat: Once again need SSMS
Go to Database name and right click to select Tasks> Export from menu which should open up SQL import and Export Wizard. You can save it as package and deploy it under the SSIS catalog and run as scheduled job too.
Alternative solution 4
Write an SSIS package to get data from SQL server and put into Excel.
I am working on a Windows Server 2012 64-bit. I want to be able to import data from a .dbf file into a SQL Server table. I used the import wizard and it worked correctly. However, I have SQL Server Express and can't schedule this insertion.
Is there another way to schedule the insertion of the .dbf data to the SQL Server tables, without the use of the SSIS package loader?
Update
I ended up using Python and writing a script to import from XML. However, I believe the answer by #Oleg was the most accurate, given the circumstances.
Thank you all!
You can also use DBF Commander Pro for this task:
Create command line for your insertion - choose 'File -> Export to DBMS'. Specify transfer options in the window appears, then copy the command line from the bottom of the window:
Create text .BAT file and insert the copied command line, e.g.:
"c:\Program Files\DBFCommander\DBFCommander.exe" -edb "D:\Data\customer.dbf" customer_table "Provider=SQLOLEDB.1;User ID=user1;Initial Catalog=test_db;Data Source=test_server"
Make a schedule using Windows Scheduler that will execute this .BAT file.
Additional info that may be useful for you:
Using DBF in batch mode
Export DBF file to SQL database
I suggest you the next approach:
Create C# script which will use the OleDbConnection (to fetch) and SqlConnection (to upload) objects to import data from the .DBF file to SQL Server database table.
By using LinqPad, LinqPad command-line utility (lprun.exe) and windows Scheduled Task service automate the execution of the mentioned script file
Useful links:
How to get data from DBF file using C#
How to load data into datadase using C#
About LINQPad command-line utility
Another way is create a SQL linked server an ODBC that is pointing at the DBF. Use Windows scheduler to call SQLCMD.EXE to run some SQL to copy the data in.
I have a CSV file with 10 columns. I want to import to SQL Server table with only 5 columns using powershell. Can anyone help in this ?
I suggest that Change your excel file to 5 sight column, and then using SQL Server Import\Export wizard or SSIS (if your convert is each day periodic) or PowerShell to convert your data.
You can you following reference in order to use PowerShell to convert your file:
SQL SERVER – Powershell – Importing CSV File Into Database