I'm generating dbf file to get imported to legacy systems that only accepts dBase II OR III. My aplication is .Net 3.5. I initially started working with this component VFPOLEDB.1 but it only generate dbf files in dBase V format which isn't backwards compatibily.
Anyone knows a component or driver to generate de dbf file in dBase II or III
Thanks
Try issuing a call to execute a script that opens the file, then does
COPY TO {some file} type FOX2X
that should get you the output...
There was another post of a similar all being done via C# through the VFPOleDB and I'll try to find it... Yup, and with credit to #DaveB here's a snippet of his post in Create .DBF in C# code that is readable from Excel (VFP or not)
string connectionString = #"Provider=VFPOLEDB.1;Data Source=C:\YourDirectory\";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
using (OleDbCommand scriptCommand = connection.CreateCommand())
{
connection.Open();
string vfpScript = #"USE TestDBF
COPY TO OldDBaseFormatFile TYPE Fox2x
USE";
scriptCommand.CommandType = CommandType.StoredProcedure;
scriptCommand.CommandText = "ExecScript";
scriptCommand.Parameters.Add("myScript", OleDbType.Char).Value = vfpScript;
scriptCommand.ExecuteNonQuery();
}
}
The original post was for someone to be able to open the file in Excel format.
I remember trying to do this very thing several years ago and failing. My solution was to take an existing dBase II file, empty all data, and keep that empty file as a template for when I needed to create a new database.
ESRI's Shapefile format uses dBase III for storing attribute data. There's a decent implementation in the SharpMap project which you should be able to use independently (careful of the license, though: it's LGPL).
http://code.google.com/p/sharpmapv2/source/browse/trunk/SharpMap.Data.Providers/ShapeFileProvider/DbaseFile.cs
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to save an existing Excel file via ssms OR C# into my SQL Server 2016 database.
I want to save each row of my Excel file in a C# object and then save it into my database, or do you have better ideas?
I also thought about saving the Excel file as a *.csv and import this file via ssms in my database.
Which of these two ideas would you recommend or is there any other way to solve this problem?
If you have any questions, I would be pleased to answer them.
I thank you in advance for all the answers and tips!
For your problem you can try below approaches:
1) Using SQLBulkcopy:
SqlBulkCopy class as the name suggests does the bulk insert from one source to another and hence all rows from the Excel sheet can be easily read and inserted using the SqlBulkCopy class.
protected void Upload(object sender, EventArgs e)
{
//Upload and save the file
string excelPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(excelPath);
string conString = string.Empty;
string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
switch (extension)
{
case ".xls": //Excel 97-03
conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx": //Excel 07 or higher
conString = ConfigurationManager.ConnectionStrings["Excel07+ConString"].ConnectionString;
break;
}
conString = string.Format(conString, excelPath);
using (OleDbConnection excel_con = new OleDbConnection(conString))
{
excel_con.Open();
string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();
DataTable dtExcelData = new DataTable();
//[OPTIONAL]: It is recommended as otherwise the data will be considered as String by default.
dtExcelData.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Salary",typeof(decimal)) });
using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + sheet1 + "]", excel_con))
{
oda.Fill(dtExcelData);
}
excel_con.Close();
string consString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(consString))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{
//Set the database table name
sqlBulkCopy.DestinationTableName = "dbo.tblPersons";
//[OPTIONAL]: Map the Excel columns with that of the database table
sqlBulkCopy.ColumnMappings.Add("Id", "PersonId");
sqlBulkCopy.ColumnMappings.Add("Name", "Name");
sqlBulkCopy.ColumnMappings.Add("Salary", "Salary");
con.Open();
sqlBulkCopy.WriteToServer(dtExcelData);
con.Close();
}
}
}
}
Here this code adds an excel sheet with three columns as Id, Name and Salary.
2) Using DTS in SSMS:
You can use the SQL Server Data Transformation Services (DTS) Import Wizard or the SQL Server Import and Export Wizard to import Excel data into SQL Server tables. When you are stepping through the wizard and selecting the Excel source tables, remember that Excel object names that are appended with a dollar sign ($) represent worksheets (for example, Sheet1$), and that plain object names without the dollar sign represent Excel named ranges.
3) Using SSIS package:
You can create SSIS package to import excel file. For this, you can use BIDS in Visual Studio or SQL Server Data tools.
You can give your excel file as excel source and in the target give your SQL server database table.
Perform the necessary mappings and you're good to go.
Now, you must be having a question like When to use which approach?
Use approach 1, Whenever you're providing functionality to import excel file at the user end, i.e. according to application requirement, the user can upload local excel sheet. For this use case, one thing you should look out is, the user must be aware of the template. If you have written code to import excel with 3 columns and the user tries to import with 4 columns, you will have some error in future. So make sure that you provide a template that user should download and fill and upload it.
Use approach 2, whenever you want to load data for only one time, or you can say that you want to perform initial load. You can use this approach as it's most simple and requires less time to do the configuration.
Use approach 3, whenever you have some requirement like to import excel data on the timely basis from some shared location. For ex, you are importing monthly mobile bills to your database provided by some vendor. You can create a package for this functionality and do the SSIS configuration and create a package.
Once the package is created you can create a SQL job and schedule it as per the requirements.
You can use BulkInsert to imports a data file into a database table or view in a user-specified format in SQL Server
As all, it depends on usage, change frequency, who is going to maintain solution etc.
SSIS and CSV import
It is possible to create SSIS package which would be able to import your data automatically when deployed on MSSQL server or manually. This would be simplest/quickest to implement. One of advantages when using Visual Studio tooling for SSIS development you would have visual representation of mappings, flow.
Drawback, even though I have seen automated column mapping updates (C# automatic SSIS package generation), whenever you would need to add, remove, change column, you would need a manual change.
BCP
MS console utility which you can use to define columns in format files and import your CSVs. Drawback is that you there is no graphical user interface, though many would argue that this is an advantage because there is a better overview for changes.
ORM
In object relational mapping solution you would need to translate your Excel file into object oriented programming language classes and save as objects into database table. Drawback is that you need to have some programming knowledge, but would pay off in a longer run because potentially your solution could get the data directly form source for those excel sheets.
This question already has an answer here:
MySQL Load Data Infile
(1 answer)
Closed 6 years ago.
I'm really a newbie one.
I wanted to import data from excel or CSV file to MySQL using C#.
I just need a button and then, viola! It should insert automatically to MySQL database.
I only know how the structure of my excel file and my MySQL database table looks like. Just don't know how to import it.
Please help. As always, thank you!
This should be able to start you off.. but you could've found this if you actually searched Stack Exchange or Google. Also since you commented that you already know how to read an Excel file... here's a start to insert the data.
using MySql.Data.MySqlClient;
cont string connectInfo = "Server=yourServer;Database=yourDatabase;UID=yourUsername;Password=yourPassword";
var query = "insert into yourTable (column, column, column..) Values(#column1, #column2, #column3)";
using (var connect = new MySqlConnection(connectInfo)) {
connect.Open();
using (var cmd = new MySqlCommand(query, connect) {
cmd.Parameters.AddwithValue("#column1", "something");
cmd.Parameters.AddWithValue("#column2", "something");
cmd.Parameters.AddWithValue("#column3", "something");
cmd.ExecuteNonQuery();
}
}
I cannot to connect to a DBF file type Visual dBase level 7 format with C#.
I can read a DBF file type dBase III and dBase IV but with the file type DBF Visual dBase 7 Visual studio return an error message:
"the format table is not on right format expected".
Here is the follow codes I use for an console application:
static void Main(string[] args)
{
string filepath = #"C:\Users\user\Desktop\BGF\DATA\";
OdbcConnection CC =
new OdbcConnection("Driver={Microsoft dBase Driver
(*.dbf)};SourceType=DBF;SourceDB=" + filepath + ";Exclusive=No;
Collate=Machine;NULL=NO;DELETED=NO;BACKGROUNDFETCH=NO;");
CC.Open();
OdbcCommand cmd = new OdbcCommand("Select * From MyDBF_file", CC);
OdbcDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
DataTable dt = new DataTable();
dt.Load(dr);
}
CC.Close();
`enter code here`Console.WriteLine("Successful");
Console.Read();
}
I think the provider is not compatible, but I have tried with Microsoft.Jet.OLEDB.4.0 does not work.
And with vfpoledb provider same problem.
I have tried with simple query as
OdbcCommand cmd = new OdbcCommand("Select * From MyDBF_file", CC);
And same problem :-(
Thx in advance for your help or for all approach contribute to a part of solution ;-)
You appear to be correct on the CONNECTION. That should be to the PATH where the tables are located. However, you QUERY should
select * from SomeTableWithinThatPath
You are trying to query the PATH, not a specific table.
For Visual dBase level 7 format this connections will not work.
You can use code library of dBase IV reader and modify headers to work with Dbase 7 files.
You can find Dbase 7 header information from here.
http://www.dbase.com/KnowledgeBase/int/db7_file_fmt.htm
And DBF reader for older version from here.
https://github.com/eXavera/NDbfReader
I have a requirement to generate output report in Excel format and open the same on the screen when the processing is complete. But in this case, it should not save the report on the drive anywhere and only open on the screen.
I tried to use ADO using OLEDB but it always generates file before writing anything to it.
This is what I have tried so far.
using (OleDbConnection con = new OleDbConnection(connString))
{
try
{
con.Open();
}
catch (InvalidOperationException invalidEx)
{
//Exception handling
}
// Create table for excel structure
StringBuilder strSQL = new StringBuilder();
strSQL.Append("CREATE TABLE [" + tableName + "]([TITLE] text,[SURNAME] text,[STATUS] text)");
// Define file columns
StringBuilder strfield = new StringBuilder();
strfield.Append("[TITLE],[SURNAME],[STATUS]");
OleDbCommand cmd = new OleDbCommand(strSQL.ToString(), con);
cmd.ExecuteNonQuery(); // This creates the table
//Actual row for creating and insering row - logic not shown completely
cmd.CommandText = strSQL.Append(" insert into [" + tableName + "]( ")
.Append(strfield.ToString())
.Append(") values (").Append(strvalue).Append(")").ToString();
success = cmd.ExecuteNonQuery();
But this always creates the file first which I do not want.
Please advise if anyone has worked on the similar requirement. Thanks.
Ok, first off use ADO (a database access technology) to try and create a spreadsheet is bizarre, possibly doable, but definitely not easy.
Secondly you're saying create a spreadsheet and open it, without creating a file, this means that you'll also have to create ALL the functionality to open, parse, format and display spreadsheets (basically recreate Excel!)...as Excel cannot do this for you.
So I would question the "generate output report in Excel format" requirement, does this really mean "display in a grid"? Or is it "display in a grid that allows formatting, totalling?"
If it the Excel format really is a requirement, then the only thing I can suggest is you will have to create a temporary Excel file, then delete it after you've displayed it.
I would look at the ClosedXML library that really simplifies the use of OpenXML to create xlsx spreadsheets.
Perhaps this Microsoft Article will help: How to: Open a spreadsheet document from a stream (Open XML SDK)
I'm using C# and .NET 3.5, trying to import some data from old dbf files using ODBC with Microsoft dBase Driver.
The dbf's are in dBase III format and using ibm850 encoding for strings.
Now, when I run my program on my machine, all string data read from OdbcDataReader comes out converted to UTF-16 or UTF-8 or something, idk and I save it as UTF-8 and everything is ok, but when I try to use this program on an XP box, some characters aren't converted correctly to UTF-8. 'Õ' for example. There may be some others too. Characters like 'Ä', 'Ö' and 'Ü' are ok. This is the problem.
Maybe the ODBC or the driver uses some machine culture info or something to mess everything up.
Is it possible to read strings from the database as binary? Maybe some functions like CONVERT or CAST? Or where could I find some references for SQL functions and syntax which works for this dBase driver or other drivers? I searched around and couldn't find anything. I feel so blind when using ODBC and SQL.
Right now I'm using a temporary hack that replaces all σ's with Õ's.
Thanks!
Example code:
System.Data.Odbc.OdbcConnection oConn = new System.Data.Odbc.OdbcConnection();
oConn.ConnectionString = #"Driver={Microsoft dBase Driver (*.dbf)};DriverID=277;Dbq=" + dbPath + ";";
oConn.Open();
System.Data.Odbc.OdbcCommand oCmd = oConn.CreateCommand();
oCmd.CommandText = #"SELECT name FROM " + dbPath + "TABLE.DBF";
System.Data.Odbc.OdbcDataReader reader = oCmd.ExecuteReader();
reader.Read();
byte[] buf = Encoding.UTF8.GetBytes(reader.GetString(0));
BinaryWriter writer = new BinaryWriter(File.Open(#"C:\DBF\Test.txt", FileMode.Create));
writer.Write(buf);
Result:
E5 in dbf (Õ in 850)
Test.txt on pc1: C3 95 (Õ in UTF-8)
Test.txt on pc2: CF 83 (σ in UTF-8)
If you are still having a problem with these files, I may be able to help you.
What is in the "codepage byte" aka "language driver id" (LDID) at offset 29 (decimal) in the file?
I have a Python-based DBF reader which can read just about any field data type and just about any codepage -- it has a long list compiled from various sources of mappings from codepage byte to codepage number. Options are (1) believe the LDID, deliver Unicode (2) ignore the LDID, deliver undecoded bytes (3) override the LDID, decode with a specific codepage into Unicode. The Unicode can of course be then encoded into UTF-8.
The DBF reader also does a whole lot of reasonableness cross-checks which may help investigating why VFP thinks the file is corrupt.
How do you know that it's using IBM850? Another piece of Python code that I have is a prototype encoding detector, which unlike detectors like 'chardet' which are derived from Mozilla code is not web-centric and can happily recognise most old DOS codepages -- this may help.
A observation: the Greek letter lowercase sigma (σ) is 0xE5 in codepage 437, which was succeded by codepage 850 -- "pc2" seems a little outdated ...
If you think I can be of any help, feel free to e-mail me at insert_punctuation("sjmachin", "lexicon", "net")
Try this code.
var oConn = new System.Data.Odbc.OdbcConnection();
oConn.ConnectionString = "Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + dbPath;
oConn.Open();
var oCmd = oConn.CreateCommand();
oCmd.CommandText = #"SELECT name FROM " + dbPath + "TABLE.DBF";
var reader = oCmd.ExecuteReader();
reader.Read();
byte[] A = Encoding.GetEncoding(Encoding.Default.CodePage).GetBytes(reader.GetString(0));
string p = Encoding.Unicode.GetString((Encoding.Convert(Encoding.GetEncoding(850), Encoding.Unicode, A)));
When you read dbf file you should understand that you should take into account 3 types of encoding:
1.Encoding in which database provider reads the file. It depends on
provider and current operation system. This encoding shall be used for bytes array receiving. For example on my PC:
when I use connection string "Data Source={0};
Provider=Microsoft.JET.OLEDB.4.0;Extended Properties=DBase IV;User
ID=;Password=;", strings are read using 866 code page (Russian
MS-DOS)
when I use connection string "Data Source={0};
Provider=vfpoledb.1;Exclusive=No;Collating Sequence=Machine", strings
are read using Encoding.Default (1251 code page)
2.Encoding in which strings are written to dbf file. It can be received from 29 byte of dbf file, but in fact there is no matter what how dbf file encoding is marked, you should just know what encoding was used. This encoding shall be used as source encoding during string conversion
3.Encoding to which string shall be converted. This is UTF-8 usually.
So string conversion should look like this:
byte[] bytes = Encoding.GetEncoding(codePage1).GetBytes(reader.GetString(0));
string result = Encoding.UTF8.GetString((Encoding.Convert(Encoding.GetEncoding(codePage2), Encoding.UTF8, bytes)));
Have you tried using the Visual Foxpro driver "VFPOleDb" driver instead???