Read excel via ADO.NET with SQL Server commands? - c#

I can read XLS file with this code :
string path =#"c:\r\1.xlsx";
OleDbConnection MyConnection = new OleDbConnection(#"provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + path + #"';HDR=Yes;Jet OLEDB:Engine Type=37");
OleDbDataAdapter MyCommand = new OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
DataSet DtSet = new DataSet();
MyCommand.Fill(DtSet);
...
...
However - when I enhance the query to include some SQL Server commands like
select *,case when 1=1 then 'a' else 'b' end as rr from [Sheet1$]
it goes BANG
I know that OLEDB is using access jet/ace behind the scenes.
How can use pure T-SQL query here?

You have to use IIF in querying excel
select *,
IIF(1 = 1, 'a', 'b') as rr
from [Sheet1$]
And, to create a multiple case statement, just nest them, like this:
select *,
IIF(1 = 1, 'a', IIF( 2 = 2, 'c', 'b')) as rr
from [Sheet1$]
As to whether you can use a pure MSSQL query, I do not believe that any connection that you can use with excel supports the CASE statement. So, you will have to use the above solution

Related

Ado.Net DBase driver truncates cells with tab character

I am trying to access to an old DBase file with Ado.Net C#. I successfully opened file but some string records in table has TAB character like "Some text/TABOther text". Ado.Net driver successfully reads "Some text" and can not read rest of the data. Is there are a way to let Ado.Net read all the content of the cell?
My connection string:
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DATA DIRECTORY;Extended Properties=dBASE IV;User ID=;Password=;";
Select query:
var sql = "select * from " + "MY_TABLE";
OleDbCommand cmd = new OleDbCommand(sql, con);
con.Open();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);
I would use VFPOLEDB driver from Microsoft.Jet driver is good for maybe dBase II but not trustable for the later versions (like dBase IV).
DataTable tbl = new DataTable();
new OleDbAdapter("select * from MY_TABLE",
"Provider=VFPOLEDB;Data Source=DATA DIRECTORY",
).Fill(tbl);
And be sure you only have TAB character in between. While xbase databases can store any character in a string field, in C# (contrary to documentation) strings are ASCIIZ strings like in C. Also if that string has NEWLINE character in it, in DataTable you might not see that.
PS: Try posting to Visual-Foxpro tag next time. That one has more interest.

SQL Bulk Insert in C# not inserting values

I'm completely new to C#, so I'm sure I'm going to get a lot of comments about how my code is formatted - I welcome them. Please feel free to throw any advice or constructive criticisms you might have along the way.
I'm building a very simple Windows Form App that is eventually supposed to take data from an Excel file of varying size, potentially several times per day, and insert it into a table in SQL Server 2005. Thereafter, a stored procedure within the database takes over to perform various update and insert tasks depending on the values inserted into this table.
For this reason, I've decided to use the SQL Bulk Insert method, since I can't know if the user will only insert 10 rows - or 10,000 - at any given execution.
The function I'm using looks like this:
public void BulkImportFromExcel(string excelFilePath)
{
excelApp = new Excel.Application();
excelBook = excelApp.Workbooks.Open(excelFilePath);
excelSheet = excelBook.Worksheets.get_Item(sheetName);
excelRange = excelSheet.UsedRange;
excelBook.Close(0);
try
{
using (SqlConnection sqlConn = new SqlConnection())
{
sqlConn.ConnectionString =
"Data Source=" + serverName + ";" +
"Initial Catalog=" + dbName + ";" +
"User id=" + dbUserName + ";" +
"Password=" + dbPassword + ";";
using (OleDbConnection excelConn = new OleDbConnection())
{
excelQuery = "SELECT InvLakNo FROM [" + sheetName + "$]";
excelConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFilePath + ";Extended Properties='Excel 8.0;HDR=Yes'";
excelConn.Open();
using (OleDbCommand oleDBCmd = new OleDbCommand(excelQuery, excelConn))
{
OleDbDataReader dataReader = oleDBCmd.ExecuteReader();
using (SqlBulkCopy bulkImport = new SqlBulkCopy(sqlConn.ConnectionString))
{
bulkImport.DestinationTableName = sqlTable;
SqlBulkCopyColumnMapping InvLakNo = new SqlBulkCopyColumnMapping("InvLakNo", "InvLakNo");
bulkImport.ColumnMappings.Add(InvLakNo);
sqlQuery = "IF OBJECT_ID('ImportFromExcel') IS NOT NULL BEGIN SELECT * INTO [" + DateTime.Now.ToString().Replace(" ", "_") + "_ImportFromExcel] FROM ImportFromExcel; DROP TABLE ImportFromExcel; END CREATE TABLE ImportFromExcel (InvLakNo INT);";
using (SqlCommand sqlCmd = new SqlCommand(sqlQuery, sqlConn))
{
sqlConn.Open();
sqlCmd.ExecuteNonQuery();
while (dataReader.Read())
{
bulkImport.WriteToServer(dataReader);
}
}
}
}
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
excelApp.Quit();
}
}
The function runs without errors or warnings, and if I replace the WriteToServer with manual SQL commands, the rows are inserted; but the bulkImport isn't inserting anything.
NOTE: There is only one field in this example, and in the actual function I'm currently running to test; but in the end there will be dozens and dozens of fields being inserted, and I'll be doing a ColumnMapping for all of them.
Also, as stated, I am aware that my code is probably horrible - please feel free to give me any pointers you deem helpful. I'm ready and willing to learn.
Thanks!
I think it would be a very long and messy answer if I commented on your code and also gave pointer sample codes in the same message, so I decided to divide then into two messages. Comments first:
You are using automation to get what? You already have the sheet name as I see it and worse you are doing app.Quit() at the end. Completely remove that automation code.
If you needed some information from excel (like sheet names, column names) then you could use OleDbConnecton's GetOleDbSchemaTable method.
You might do the mapping basically in 2 ways:
Excel column ordinal to SQL table column name
Excel column name to SQL table column name
both would do. In a generic code, assuming you have column names same in both sources, but their ordinal and count may differ, you could get the column names from OleDbConnection schema table and do the mapping in a loop.
You are dropping and creating a table named "ImportFromExcel" for the purpose of temp data insertion, then why not simply create a temp SQL server table by using a # prefix in table name? OTOH that code piece is a little weird, it would do an import from "ImportFromExcel" if it is there, then drop and create a new one and attempt to do bulk import into that new one. In first run, SqlBulkCopy (SBC) would fill ImportFromExcel and on next run it would be copied to a table named (DateTime.Now ...) and then emptied via drop and create again. BTW, naming:
DateTime.Now.ToString().Replace(" ", "_") + "_ImportFromExcel"
doesn't feel right. While it looks tempting, it is not sortable, probably you would want something like this instead:
DateTime.Now.ToString("yyyyMMddHHmmss") + "_ImportFromExcel"
Or better yet:
"ImportFromExcel_" +DateTime.Now.ToString("yyyyMMddHHmmss")
so you would have something that is sorted and selectable for all the imports as a wildcard or looping for some reason.
Then you are writing to server inside a reader.Read() loop. That is not the way WriteToServer works. You wouldn't do reader.Read() but simply:
sbc.WriteToServer(reader);
In my next message e I will give simple schema reading and a simple SBC sample from excel into a temp table, as well as a suggestion how you should do that instead.
Here is the sample for reading schema information from Excel (here we read the tablenames - sheet names with tables in them):
private IEnumerable<string> GetTablesFromExcel(string dataSource)
{
IEnumerable<string> tables;
using (OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" +
string.Format("Data Source={0};", dataSource) +
"Extended Properties=\"Excel 12.0;HDR=Yes\""))
{
con.Open();
var schemaTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
tables = schemaTable.AsEnumerable().Select(t => t.Field<string>("TABLE_NAME"));
con.Close();
}
return tables;
}
And here is a sample that does SBC from excel into a temp table:
void Main()
{
string sqlConnectionString = #"server=.\SQLExpress;Trusted_Connection=yes;Database=Test";
string path = #"C:\Users\Cetin\Documents\ExcelFill.xlsx"; // sample excel sheet
string sheetName = "Sheet1$";
using (OleDbConnection cn = new OleDbConnection(
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+path+
";Extended Properties=\"Excel 8.0;HDR=Yes\""))
using (SqlConnection scn = new SqlConnection( sqlConnectionString ))
{
scn.Open();
// create temp SQL server table
new SqlCommand(#"create table #ExcelData
(
[Id] int,
[Barkod] varchar(20)
)", scn).ExecuteNonQuery();
// get data from Excel and write to server via SBC
OleDbCommand cmd = new OleDbCommand(String.Format("select * from [{0}]",sheetName), cn);
SqlBulkCopy sbc = new SqlBulkCopy(scn);
// Mapping sample using column ordinals
sbc.ColumnMappings.Add(0,"[Id]");
sbc.ColumnMappings.Add(1,"[Barkod]");
cn.Open();
OleDbDataReader rdr = cmd.ExecuteReader();
// SqlBulkCopy properties
sbc.DestinationTableName = "#ExcelData";
// write to server via reader
sbc.WriteToServer(rdr);
if (!rdr.IsClosed) { rdr.Close(); }
cn.Close();
// Excel data is now in SQL server temp table
// It might be used to do any internal insert/update
// i.e.: Select into myTable+DateTime.Now
new SqlCommand(string.Format(#"select * into [{0}]
from [#ExcelData]",
"ImportFromExcel_" +DateTime.Now.ToString("yyyyMMddHHmmss")),scn)
.ExecuteNonQuery();
scn.Close();
}
}
While this would work, thinking in the long run, you need column names, and maybe their types differ, it might be an overkill to do this stuff using SBC and you might instead directly do it from MS SQL server's OpenQuery:
SELECT * into ... from OpenQuery(...)
the WriteToServer(IDataReader) is intended to do internally the IDataReader.Read()operation.
using (SqlCommand sqlCmd = new SqlCommand(sqlQuery, sqlConn))
{
sqlConn.Open();
sqlCmd.ExecuteNonQuery();
bulkImport.WriteToServer(dataReader);
}
You can check the MSDN doc on that function, has a working example: https://msdn.microsoft.com/en-us/library/434atets(v=vs.110).aspx

Copying data from one database to another - Out of Memory Exception

I have a task where i want to copy all data from one database to another database & skipping 2 tables. There are more than 200 tables.
I have table structure ready for my 2nd databas.
So as a solution i created a page & on a button click i have below code :-
DataSet ds = new DataSet();
string connectionString = "Data Source=COMP112\\MSSQLSERVER2014;Initial Catalog=HCMBL;Integrated Security=True;Persist Security Info=True";
SqlConnection con = new SqlConnection(connectionString);
//render table name from database
string sqlTable = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' and TABLE_Schema='" + Session["SchemaName"].ToString() + "' and TABLE_NAME!='ENTRY' and TABLE_NAME!='OT' and TABLE_NAME!='BL_ENTRY' and TABLE_NAME!='BL_OT'";
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand(sqlTable, con);
cmd.CommandType = CommandType.Text;
da.SelectCommand = cmd;
da.Fill(ds);
con.Close();
//render connection string from WebConfig file
string strcon = ConfigurationManager.ConnectionStrings["SPSchema"].ConnectionString;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (!(ds.Tables[0].Rows[i]["TABLE_NAME"].ToString().Contains("Asp")))
{
string deleteQuery = "Truncate table " + Session["SchemaName"].ToString() + "." + ds.Tables[0].Rows[i]["TABLE_NAME"];
con.Open();
SqlCommand cmdDelete = new SqlCommand(deleteQuery, con);
cmdDelete.ExecuteNonQuery();
con.Close();
DataSet dataSet = new DataSet();
SqlConnection conn = new SqlConnection(strcon);
conn.Open();
string selectData = "select * from " + Session["SchemaName"].ToString() + "." + ds.Tables[0].Rows[i]["TABLE_NAME"];
SqlCommand command = new SqlCommand(selectData, conn);
DataTable dataTable = new DataTable();
SqlDataAdapter dataAdapter = new SqlDataAdapter(selectData, conn);
dataAdapter.FillSchema(dataSet, SchemaType.Mapped);
dataAdapter.Fill(dataSet);
dataTable = dataSet.Tables[0];
conn.Close();
if (dataSet.Tables[0].Rows.Count > 0)
{
//Connect to second Database and Insert row/rows.
SqlConnection conn2 = new SqlConnection(connectionString);
conn2.Open();
SqlBulkCopy bulkCopy = new SqlBulkCopy(conn2);
bulkCopy.DestinationTableName = Session["SchemaName"].ToString() + "." + ds.Tables[0].Rows[i]["TABLE_NAME"].ToString();
bulkCopy.WriteToServer(dataTable);
conn2.Close();
}
}
}
As i run the above code after inserting data in less than 10 tables, it gives out of memory exception & program crashes.
How to handle this? I tried increasing the memory capacity of SQL Server but still same error.
Is there any other way to achieve the task?
What you are doing is very far from the best solution. You are using an ASP.NET MVC process to get all data of your entire database into memory, and then outputting it to another database. If your database is anything more than small and trivial, that will most definitely fill your process's alotted memory.
This type of task should never be done through the memory of a process, but rather using some form of Backup/Restore pattern.
You should look into SSIS projects and create an extract, transfer, and load (ETL) solution, which can be triggered from your ASP.NET MVC solution asynchronously.
An SSIS solution can be triggered from C# code in this way:
var app = new Application();
var package = app.LoadPackage("compiled-package.dtsx", null);
var results = package.Execute();
See this question for a little more information (not specifically about duplicating databases, but has information about triggering SSIS packages from code): How to execute an SSIS package from .NET?
Alternatively
You also have the option of running a query against both databases at once, however this requires some additional plumbing to be done. The user account of your ASP.NET MVC solution needs to have access to both databases. If your databases are hosted on different servers, you also need to link one server to the other: Create linked servers
To perform an insert directly from the output of a select, consider this:
string source = "NAME_OF_SOURCE_DATABASE";
string target = "NAME_OF_TARGET_DATABASE";
string schema = Session["SchemaName"].ToString();
string table = ds.Tables[0].Rows[i]["TABLE_NAME"];
// Uncomment this if you need to deal with autoincrement columns
/*string idInsQuery = $"SET IDENTITY_INSERT {target}.{schema}.{table} ON";
var idInsCommand = new SqlCommand(idInsQuery, conn);
idInsCommand.ExecuteNonQuery();*/
string insQuery = $"INSERT INTO {target}.{schema}.{table} SELECT * FROM {source}.{schema}.{table}";
var insCommand = new SqlCommand(insQuery, conn);
insCommand.ExecuteNonQuery();
// Uncomment this if you need to deal with autoincrement columns
/*string idInsQuery2 = $"SET IDENTITY_INSERT {target}.{schema}.{table} OFF";
var idInsCommand2 = new SqlCommand(idInsQuery2, conn);
idInsCommand2.ExecuteNonQuery();*/
This will only work if the table structures are identical. There might be problems with autoincrement ids or columns with default values, too.
This will copy data from a table in database 1 to a table in database 2
Insert into db2.dbo.table2 (col1,col2)
Select col1,col2 from db1.dbo.table1
Run this sql statement and the data will be copied without a round trip to your app.
Let me know if you find my approach is useful.
First of all, why you want to write down one whole application to do this job while SQL Server have inherited property to do it.
My approach would be configure an Linked Server and configure it which tables you want to copy and which one not.
https://learn.microsoft.com/en-us/sql/relational-databases/linked-servers/create-linked-servers-sql-server-database-engine
Secondly, You can just write down simple stored procedure and schedule that in your sql server to push into another server database as per your schedule. In this way you can control it in N number of ways. I mean about controlling any dependencies(Table level or Business level).
To do this in t-sql, you can use the following system stored procedures to schedule a daily job. This example schedules daily at 1:00 AM. See Microsoft help for details on syntax of the individual stored procedures and valid range of parameters.
DECLARE #job_name NVARCHAR(128), #description NVARCHAR(512), #owner_login_name NVARCHAR(128), #database_name NVARCHAR(128);
SET #job_name = N'Some Title';
SET #description = N'Periodically do something';
SET #owner_login_name = N'login';
SET #database_name = N'Database_Name';
-- Delete job if it already exists:
IF EXISTS(SELECT job_id FROM msdb.dbo.sysjobs WHERE (name = #job_name))
BEGIN
EXEC msdb.dbo.sp_delete_job
#job_name = #job_name;
END
-- Create the job:
EXEC msdb.dbo.sp_add_job
#job_name=#job_name,
#enabled=1,
#notify_level_eventlog=0,
#notify_level_email=2,
#notify_level_netsend=2,
#notify_level_page=2,
#delete_level=0,
#description=#description,
#category_name=N'[Uncategorized (Local)]',
#owner_login_name=#owner_login_name;
-- Add server:
EXEC msdb.dbo.sp_add_jobserver #job_name=#job_name;
-- Add step to execute SQL:
EXEC msdb.dbo.sp_add_jobstep
#job_name=#job_name,
#step_name=N'Execute SQL',
#step_id=1,
#cmdexec_success_code=0,
#on_success_action=1,
#on_fail_action=2,
#retry_attempts=0,
#retry_interval=0,
#os_run_priority=0,
#subsystem=N'TSQL',
#command=N'EXEC my_stored_procedure; -- OR ANY SQL STATEMENT',
#database_name=#database_name,
#flags=0;
-- Update job to set start step:
EXEC msdb.dbo.sp_update_job
#job_name=#job_name,
#enabled=1,
#start_step_id=1,
#notify_level_eventlog=0,
#notify_level_email=2,
#notify_level_netsend=2,
#notify_level_page=2,
#delete_level=0,
#description=#description,
#category_name=N'[Uncategorized (Local)]',
#owner_login_name=#owner_login_name,
#notify_email_operator_name=N'',
#notify_netsend_operator_name=N'',
#notify_page_operator_name=N'';
-- Schedule job:
EXEC msdb.dbo.sp_add_jobschedule
#job_name=#job_name,
#name=N'Daily',
#enabled=1,
#freq_type=4,
#freq_interval=1,
#freq_subday_type=1,
#freq_subday_interval=0,
#freq_relative_interval=0,
#freq_recurrence_factor=1,
#active_start_date=20170101, --YYYYMMDD
#active_end_date=99991231, --YYYYMMDD (this represents no end date)
#active_start_time=010000, --HHMMSS
#active_end_time=235959; --HHMMSS
Let me know in case you need more details on this.
Thanks,
Ayan

Using WHERE clause with csv files ADO.NET / C#

i have a little problem. I'm using a csv file as database and i'm asking it with ADO.NET's OLEDB funcions.
I want to select only rows where column "DATA" is included between two datas, like this:
String conn = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\;
;Extended Properties='text;HDR=Yes;Format=Delimited(;)';";
OleDbConnection cn = new OleDbConnection(conn);
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [mydb]", cn);
OleDbDataAdapter daAd = new OleDbDataAdapter();
daAd.SelectCommand= cmd;
cn.Open();
DataTable dt = new DataTable();
daAd.Fill(dt);
DateTime mydata= Convert.ToDateTime("01/01/1990");
DateTime mydata2= Convert.ToDateTime("01/01/2000");
Nothing wrong until now, but when i change
"SELECT * FROM [mydb]"
With
"SELECT * FROM [mydb] WHERE DATA>= '"+ mydata.Date +"' AND DATA<='"+ mydata2.Date +"'
I have an error saying "Syntax error (missing operator) in query 'WHERE DATA=> mydata AND DATA<= mydata2'.
I really don't know how to solve it.
More info: .CSV file is formatted like this:
DATA;INFO1;INFO2;INFO3
01/01/1990;1;2;3`
And into schema.ini is this :
[mydb.csv]
Format=Delimited(;)
ColNameHeader=True
DateTimeFormat=dd-MM-yyyy
Col1=DATA DateTime
Col2=info1 Long Width 3
Col3=info2 Integer
Col4=info3 Integer
EDIT :
I'm running a x86 Seven, i read that for solve JET driver's incompatibility is sufficient the schema.ini file, hope i'm right.
Well, this:
SELECT * FROM [mydb] WHERE DATA=> mydata AND DATA<= mydata2
is clearly invalid SQL. Perhaps you meant:
SELECT * FROM [mydb] WHERE DATA>= mydata AND DATA<= mydata2
(You used => for "greater than" rather than >=.)
I think you mean this:
SELECT * FROM [mydb] WHERE DATA >= mydata AND DATA <= mydata2
You just got the => the wrong way round!
UPDATE AFTER OP's CORRECTION
Is it due to your schema.ini? You seem to have the wrong DateTimeFormat:
DateTimeFormat=dd-MM-yyyy
Shouldn't it be:
DateTimeFormat=dd/MM/yyyy
SECOND UPDATE
Change you mydata to the following:
"SELECT * FROM [mydb] WHERE DATA >= '" +
mydata.ToString("dd/MM/yyyy") +
"' AND DATA <= '" + mydata2.ToString("dd/MM/yyyy") + "'"
Try
SELECT * FROM [mydb] WHERE DATA >= mydata AND DATA <= mydata2
Note the order of "<" and "=" in the first comparison.
Because of i'm quering a csv I can't use sql's functions(errors says they don't exist) not even compare my DATA with string. If i put in the query an unquoted date it works, but i don't respect the WHERE clause, selecting all records.
Query seems to doesn't work only for DateTime format, so i'll put my DATA in a yyyyMMdd Integer format.It's the only alternative I've in mind, still don't know why can't search with Datetime fields.

How to implement "Right Outer Join" query in access database using C#?

I want to write RightOuterJoin query to retrieve data from access database.. How can i implement this query in c#?
I tried Like this
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=Sample1.accdb; Jet OLEDB:Engine Type=5";
string sql = "SELECT t1.mobileno,t1.RetailerNo,t1.custcode,t2.RET NO FROM [C:\\Sample1.accdb].[Table1] as t1 RIGHT OUTER JOIN [C:\\Sample1.accdb].[Table2] as t2 ON t1.RetailerNo = t2.RET NO";
database = new OleDbConnection(connectionString);
database.Open();
OleDbCommand cmd1 = new OleDbCommand(sql, database);
OleDbDataAdapter da = new OleDbDataAdapter(cmd1);
//database.Open();
//conn.Open();
cmd1.ExecuteNonQuery();
System.Data.DataTable dt = new System.Data.DataTable();
da.Fill(dt);
I think you may have a problem with your connection string. It uses the Jet provider, but the database type is the newer ACCDB format. Jet does not work with ACCDB.
For ACCDB, you need the ACE provider. If you don't have Office 2007 or 2010 installed, you can download and install the Access Database Engine Redistributable.
Here is the provider section from a working connection string:
Provider=Microsoft.ACE.OLEDB.12.0
See Connection strings for Access 2007 for more details.
Once you have a working connection to Sample1.accdb, revise your SQL statement to this:
SELECT t1.mobileno,t1.RetailerNo,t1.custcode,t2.[RET NO]
FROM
Table1 as t1
RIGHT JOIN Table2 as t2
ON t1.RetailerNo = t2.[RET NO]
Notes:
Since you're already connected to Sample1.accdb, you don't need to prefix the table names with the path to the db file.
For a field name which includes a space (RET NO), enclose the field name in square brackets.
Try the following code
select * from tblemp right join tblDept on tblemp.DeptId=tblDept.pkDeptId
Its is the same as you use right join in SQL SERVER.

Categories