I am using OLEDB to Update data in .dbf database from c#.
I get Error: System.Data.OleDb.OleDbException {"Undefined function 'replace' in expression."} on ExecuteNonQuery.
How can I make this work with least changes, i need to replace double quotes with single quotes in many files, so i have to automate this process.
Should I try ODBC or something else for .dbf database?
Help please!
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + directory +";Extended Properties=dBASE III;";
OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "update Addres_1 set NAME_ENU = replace(NAME_ENU, 'a', 'b') where NAME_ENU like '*a*'";
int res = cmd.ExecuteNonQuery();
Replace is not supported by used data provider.
I will update answer if and when i find out how to do this in fast and simple way on large dataset.
Related
I am trying with oracle database to Insert and select Hebrew letters
and it not working well.
I tried
Insert into mytable values ('היי');
and the result is ??? and not היי
can someone help me with that
Edit:
Now after i ask from DBA for hebrew option i can write in Hebrew from the sqlplus
but now from my project it still write ???
my code is
OleDbConnection conn = Connect();
conn.Open();
OleDbCommand com = new OleDbCommand("Insert into mytable values ('היי')", conn);
com.ExecuteNonQuery();
and still the result is ???
I can't really test this because I don't know anything about your database (not even your column names), but you should do that command with parameters:
var testString = "היי"; // Do be aware that Visual Studio displays Hebrew text right-to-left, so the actual string is reversed from what you see.
using (OleDbConnection conn = Connect())
{
conn.Open();
using (OleDbCommand com = conn.CreateCommand())
{
// OleDbCommand com = new OleDbCommand("Insert into mytable values ('היי')", conn);
com.CommandText = "Insert into mytable values (?)";
com.Parameters.Add(new OleDbParameter { OleDbType = OleDbType.VarWChar }).Value = testString;
com.ExecuteNonQuery();
}
}
Also, don't forget to dispose your disposables via a using statement.
Relatedly, here is a report that using a parameterized query fixed a similar problem with OracleCommand.
I am having a problem with this. I want to update a record in my database but, It keeps showing this message
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'values='85' WHERE stud_no='2014-0317-TSF-1'' at line 1
string myConnection = "datasource=localhost;port=3306;username=root;password=";
MySqlConnection myConn = new MySqlConnection(myConnection);
string Query = "UPDATE stud_grades.firstyear_firstgrading SET values='" + valuesTextBox.Text + "' WHERE stud_no='" + stud_noTextBox.Text + "';";
MySqlCommand SelectCommand = new MySqlCommand(Query, myConn);
myConn.Open();
SelectCommand.ExecuteNonQuery();
Use backticks for values bacause it is a keyword
http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html
Always use parameterized query to avoid SQL Injection
How does SQLParameter prevent SQL Injection
string Query = "UPDATE stud_grades.firstyear_firstgrading SET `values`=#values where
stud_no=#stud_no";
MySqlCommand SelectCommand = new MySqlCommand(Query, myConn);
SelectCommand.Parameters.AddWithValue("#values ", valuesTextBox.Text);
SelectCommand.Parameters.AddWithValue("#stud_no", stud_noTextBox.Text);
myConn.Open();
SelectCommand.ExecuteNonQuery();
VALUES is a reserved MySQL word: http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html
Either rename this field or include values in back-ticks: `
I am getting the following error "No value given for one or more required parameters." On the ExceuteNonQuery() line of the below code.
System.Data.OleDb.OleDbConnection finalConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null;
finalConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source ='c:\\temp\\test.xlsx'; Extended Properties ='Excel 12.0 Xml;HDR=NO';");
finalConnection.Open();
myCommand.Connection = finalConnection;
foreach (VinObject v in VinList)
{
sql = "Update [Sheet1$] set O = ? where S = ?;";
myCommand.Parameters.Add(new OleDbParameter("#amt", v.CostNewAmt));
myCommand.Parameters.Add(new OleDbParameter("#vin", v.VIN));
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
}
finalConnection.Close();
I have also tried using a separate command each time, same error.
foreach (VinObject v in VinList)
{
using (OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source ='c:\\temp\\test.xlsx'; Extended Properties ='Excel 12.0 Xml;HDR=No';"))
{
con.Open();
string query = #"UPDATE [Sheet1$] SET O = ? WHERE S = ?";
OleDbCommand cmd = new OleDbCommand(query, con);
cmd.Parameters.AddWithValue("#param1", v.CostNewAmt);
cmd.Parameters.AddWithValue("#param2", v.VIN);
cmd.ExecuteNonQuery();
con.Close();
}
}
I am able to modify that into an insert and insert into a new excel spreadsheet, but for the life of me cannot get this update to work. Any idea what I am doing wrong? Thanks for the help.
You're getting the error because Excel doesn't recognize the column letter aliases "O" and "S". It needs the actual column "name", which is the value of the cell in the first populated row. If there is not a valid value in that cell, or you have specified HDR=NO in your connection string, the columns will be named F1, F2...Fn. If you're not sure what the inferred column names are, examine the names using OleDbConnection.GetSchema(String,String[]) or OleDbDataReader.GetName(Int32).
Since you have specified HDR=NO in your connection string, your correct SQL will likely be
"Update [Sheet1$] set F15 = ? where F19 = ?;"
For future reference, check out:
How to query and display excel data by using ASP.NET, ADO.NET, and Visual C# .NET
How to transfer data to an Excel workbook by using Visual C# 2005 or Visual C# .NET
How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With Visual Basic .NET. (Still lots of helpful info even if you are using C#)
OLEDB can be used to read and write Excel sheets. Consider the following code example:
using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\my\\excel\\file.xls;Extended Properties='Excel 8.0;HDR=Yes'")) {
conn.Open();
OleDbCommand cmd = new OleDbCommand("CREATE TABLE [Sheet1] ([Column1] datetime)", conn);
cmd.ExecuteNonQuery();
cmd = new OleDbCommand("INSERT INTO Sheet1 VALUES (#mydate)", conn);
cmd.Parameters.AddWithValue("#mydate", DateTime.Now.Date);
cmd.ExecuteNonQuery();
}
This works perfectly fine. Inserting numbers, text, etc. also works well. However, inserting a value with a time component fails:
using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\my\\excel\\file.xls;Extended Properties='Excel 8.0;HDR=Yes'")) {
conn.Open();
OleDbCommand cmd = new OleDbCommand("CREATE TABLE [Sheet1] ([Column1] datetime)", conn);
cmd.ExecuteNonQuery();
cmd = new OleDbCommand("INSERT INTO Sheet1 VALUES (#mydate)", conn);
cmd.Parameters.AddWithValue("#mydate", DateTime.Now); // <-- note the difference here
cmd.ExecuteNonQuery();
}
Executing this INSERT fails with an OleDbException: Data type mismatch in criteria expression.
Is this a known bug? If yes, what can be done to workaround it? I've found one workaround that works:
cmd = new OleDbCommand(String.Format(#"INSERT INTO Sheet1 VALUES (#{0:dd\/MM\/yyyy HH:mm:ss}#)", DateTime.Now), conn);
It basically creates an SQL statement that looks like this: INSERT INTO Sheet1 VALUES (#05/29/2011 13:12:01#). Of course, I don't have to tell you how ugly this is. I'd much rather have a solution with a parameterized query.
It appears to be a known bug https://connect.microsoft.com/VisualStudio/feedback/details/94377/oledbparameter-with-dbtype-datetime-throws-data-type-mismatch-in-criteria-expression
You might want to truncate the milisecond like this it appear to work for OleDbParameter:
DateTime org = DateTime.UtcNow;
DateTime truncatedDateTime = new DateTime(org.Year, org.Month, org.Day, org.Hour, org.Minute, org.Second);
And add this instead of the DateTime.Now into your parameter value.
The problem is the cell containing datetime value cannot be directly put into excel' column. You have to either insert the date component or the time component. The reason for failure is the default property of excel' cell is "values" instead of "datetime" in excel.
I want to take a backup of my Access database Pragmatically.
And After taking all data in backup i want to delete data from source database.
( So that it will not take much time while querying and filtering through application.)
The source database name is Data.mdb
The destination database name is Backup.mdb
Both are protected by same password.
For these purpose i am writing a query in C# like this.
string conString = "Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=Backup.mdb;Jet
OLEDB:Database Password=12345";
OleDbConnection dbconn = new OleDbConnection();
OleDbDataAdapter dAdapter = new OleDbDataAdapter();
OleDbCommand dbcommand = new OleDbCommand();
try
{
if (dbconn.State == ConnectionState.Closed)
dbconn.Open();
string selQuery = "INSERT INTO [Bill_Master] SELECT * FROM [MS Access;DATABASE="+
"\\Data.mdb" + "; Jet OLEDB:Database Password=12345;].[Bill_Master]";
dbcommand.CommandText = selQuery;
dbcommand.CommandType = CommandType.Text;
dbcommand.Connection = dbconn;
int result = dbcommand.ExecuteNonQuery();
}
catch(Exception ex) {}
Everything goes fine if i try with without password database file.
I think error in passing password on query statement.
I am trying to execute through access query but it is saying "Invalid argument".
Please is there any other programing logic for doing that.
Thanks
prashant
YuvaDeveloper
Are Data.mdb and Backup.mdb identically in strcuture? If so, I wouldn't bother copying data via SQL but just copy the whole file.
Try remove the space between the ; and Jet …
So the format would be:
INSERT INTO [Bill_Master] SELECT * FROM [MS Access;DATABASE="+
"\\Data.mdb" + ";Jet OLEDB:Database Password=12345;].[Bill_Master]
You can copy and rename Data.mdb, and then truncate all the tables in Data.mdb. Far easier than trying to copy a table at a time..
Don't delete data. This becomes a lot mroe difficult in the future to do analysis or inquiries. If it's taking a long time then review indexing or upszing to SQL Server. The Express edition is free and can handle databases up to 4 Gb.