I am trying to open a DBF in C# file and upload it to a MySQL database. Right now I am just trying to open the DBF file, but I am getting the following error:
A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Error: Failed to retrieve the required data from the DataBase.
Unrecognized database format 'C:\Users\Path\..\..\..\SOMEFILE.DBF'.
My code is as follows.
private void button2_Click(object sender, EventArgs e)
{
DirectoryInfo dir = new DirectoryInfo(Regex.Replace(textBox1.Text, #"\\", #"\\"));
foreach (FileInfo file in dir.GetFiles())
{
MessageBox.Show(file.Name);
string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir + file.Name;
string strAccessSelect = "SELECT * FROM "+file.Name.Substring(0,file.Name.Length-4);
DataSet myDataSet = new DataSet();
OleDbConnection myAccessConn = null;
try
{
myAccessConn = new OleDbConnection(strAccessConn);
}
catch (Exception ex)
{
Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
return;
}
try
{
OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect, myAccessConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
myAccessConn.Open();
myDataAdapter.Fill(myDataSet);
}
catch (Exception ex)
{
Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message);
return;
}
finally
{
myAccessConn.Close();
}
}
}
I only get the first MessageBox with the file name and then it throws the error.
Your connection string should identify that the OleDB data source is a DBASE type:
string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir + ";Extended Properties=dBase III";
Also note that when connecting to DBase via OleDB, you do not specify the DBF file, but rather the folder which contains it. The individual files are tables.
Use datasource without file name, only path.
string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir;
Related
I'm trying to connect my windows form app with my embedded database (.dbf) and I keep getting this message no matter what I do to the connection string:
error isam instalable cant be found
Here is the code I'm using to test the whole thing:
private void bGuardar_Click(object sender, EventArgs e)
{
try
{
string cadena = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =D:\\; Extended Properties = dBASE IV; UserID =; Password =;";
OleDbConnection con = new OleDbConnection();
con.ConnectionString = cadena;
con.Open();
MessageBox.Show("conected");
con.Close();
}
catch (OleDbException exp)
{
MessageBox.Show("Error: " + exp.Message);
}
}
I think the problem is in the connection string.
I am trying to connect the MS Access Database from the server and finding no luck.
I see the below image("Insert Operation Error") message while trying to save the information.
Can anyone please help? What went wrong in the below code?
Insert Operation Error
protected void btnsave_Click(object sender, EventArgs e)
{
string constring = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + Server.MapPath("DB\\Contact.DB");
string SqlString = "Insert Into BUREAUXDETUDES (mail1,mail2,tel1,tel2) Values (#mail1,#mail2,#tel1,#tel2)";
OleDbConnection con = new OleDbConnection(constring);
try
{
OleDbCommand cmd = new OleDbCommand(SqlString, con);
con.Open();
cmd.Parameters.AddWithValue("#mail1", txtemail1.Text);
cmd.Parameters.AddWithValue("#mail2", txtemail2.Text);
cmd.Parameters.AddWithValue("#tel1", txttel1.Text);
cmd.Parameters.AddWithValue("#tel2", txttel2.Text);
cmd.ExecuteNonQuery();
lblmessage.Text = "Your Information Saved Successfully";
}
catch (Exception emsg)
{
lblmessage.Text = emsg.Message;
}
finally
{
con.Close();
}
}
The Error is telling you that the location of your .db file is incorrect.
You can change the path to the file in this line.
string constring = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + Server.MapPath("DB\\Contact.DB");
You need to install Microsoft Excel engine first.
You can download it from the link bellow
https://www.microsoft.com/en-us/download/details.aspx?id=13255
for a small project I need to create a DataTable filled with data from an Excelsheet. Im getting this data with using an Oledb connection. Sometimes other people are using this file on the server. So I'm getting the "The process cannot access the file because it is being used by another process"
private DataTable excelSelectStatement() {
try {
string par = this._colParameters.getParameterByName("SheetName", false, null).Value;
excelDataSet = new DataSet();
string ace12 = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + _selectionStatement + "';Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';";
using (OleDbConnection connection = new OleDbConnection(ace12)) {
OleDbDataAdapter da = new OleDbDataAdapter("select * from [" + par + "$]", connection);
da.TableMappings.Add("Table", "TestTable");
da.Fill(excelDataSet);
}
}
catch (Exception e) { MessageBox.Show(e.Message); }
return excelDataSet.Tables[0];
}
Is there any way with completing this with Oledb?
I've written the following code and when it was simply uploading the file to the folder everything was fine. I've changed it to insert the file name and file path into a database and I'm getting an error:
A field or property with the name 'DataUpload' was not found on the selected data source
DataUpload is the folder name and worked fine before. I'm probably missing something simple but I'm not seeing it.
protected void ButtonSubmit_Click(object sender, EventArgs e)
{
try
{
FileUpload1.SaveAs(Server.MapPath("DataUpload\\" + FileUpload1.FileName));
Guid newGUID = Guid.NewGuid();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string InsertUser = "INSERT INTO UserUpload (ID, Comment, FilePath, FileName) VALUES (#ID, #Comment, #FilePath, #FileName)";
SqlCommand com = new SqlCommand(InsertUser, conn);
com.Parameters.AddWithValue("#ID", newGUID.ToString());
com.Parameters.AddWithValue("#Comment", TextBoxComment.Text);
com.Parameters.AddWithValue("#FilePath", "DataUpload/" + FileName);
com.Parameters.AddWithValue("#FileName", FileName);
com.ExecuteNonQuery();
LabelMessage.Text = ("Your Upload Is Complete");
conn.Close();
}
catch (Exception ex)
{
LabelMessage.Text = ("Error:" + ex.Message);
}
}
Add single quotes to the string you are creating for the FilePath, like:string.Format("'DataUpload/{0}'", FileName);
Here users browse to the Excel file from there system and then data is uploaded to sql database table.
I get this Error in Following Code : The Microsoft Jet database engine could not find the object 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\Student Registration1.xls'. Make sure the object exists and that you spell its name and the path name correctly.
I have no idea why it is asking for this Path ? C:\Program
Files\Microsoft Visual Studio 9.0\Common7\IDE\
protected void import_xls_Click(object sender, EventArgs e)
{
try
{
if (xmlupload.HasFile)
{
string path = xmlupload.PostedFile.FileName;
string excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
excelConnection.Open();
OleDbCommand cmd = new OleDbCommand("select * from [Sheet2$]", excelConnection);
OleDbDataReader dReader = cmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(SqlConnectionstring.mainConnectionString, SqlBulkCopyOptions.KeepIdentity);
sqlBulk.DestinationTableName = "dbo.studentA";
sqlBulk.ColumnMappings.Add("UserName", "UserName");
sqlBulk.ColumnMappings.Add("Password", "Password");
sqlBulk.ColumnMappings.Add("Name", "Name");
sqlBulk.ColumnMappings.Add("Standard", "Standard");
sqlBulk.ColumnMappings.Add("Division", "Division");
sqlBulk.ColumnMappings.Add("SchoolName", "SchoolName");
sqlBulk.ColumnMappings.Add("Language", "Language");
sqlBulk.ColumnMappings.Add("ExamStatus", "ExamStatus");
sqlBulk.ColumnMappings.Add("Result", "Result");
sqlBulk.WriteToServer(dReader);
lblmsg.Visible = true;
lblmsg.Text = "Your data uploaded successfully";
excelConnection.Close();
}
}
catch (Exception ex)
{
lblmsg.Visible = true;
lblmsg.Text = ex.Message.ToString();
}
SqlConnectionstring.cs :
public class SqlConnectionstring
{
public SqlConnectionstring()
{
//
// TODO: Add constructor logic here
//
}
public static readonly string mainConnectionString = ConfigurationManager.ConnectionStrings["constring"].ToString();
}
I will try to save the file in a working directory and then try to open from there.
If the fileName has no path then the connection could find the file only if it is in the
current directory (Usually where your program start)
string savePath = #"c:\temp\uploads";
if (xmlupload.HasFile)
{
string fileName = xmlupload.FileName;
savePath = Path.Combine(savePath, fileName);
xmlupload.SaveAs(savePath);
string excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
savePath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
....
On a web server, to avoid permission problems, it is better to keep the file in a subfolder of the root folder of your site.
In this case the folder could be fully qualified using the HttpServerUtility.MapMath method.