问题
I am having a strange problem. I have written a query in MS Access which inserts a value perfectly in database when run from MS Access, but when same query is executed from C# then it shows the "Validation Text" as OleDbException and insert command fails. The "Validation Rule" is "@.*" The column saves emails which should have one "@" and one dot "." after it. There is no problem when "Validation Rule" is removed and query is executed from C#. It only occurs when Validation Rule is set and error occurs only from C# and not from MS Access query execution.
I have uploaded the code and database to: http://www.2shared.com/file/XYmFD8Mg/Database_Validation.html
SQL Query in MS Access:
INSERT INTO EMail ( EMail, Company )
VALUES ('bbb@qwerty.com', 'Company');
The code is:
try
{
string dbPath = null;
if (File.Exists(Path.GetFullPath(@"..\..\..\Database.accdb")))
dbPath = Path.GetFullPath(@"..\..\..\Database.accdb");
dbConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbPath);
dbConnection.Open();
if (0 >= new System.Data.OleDb.OleDbCommand("INSERT INTO EMail ( EMail, Company ) VALUES ('bbb@qwerty.com', 'Company')", dbConnection).ExecuteNonQuery())
MessageBox.Show("Could not insert 'bbb@qwerty.com'. Check if already inserted.");
try { dbConnection.Close(); }
catch (Exception) { }
}
catch (Exception Ex)
{
try { dbConnection.Close(); }
catch (Exception) { }
MessageBox.Show(Ex.Message);
}
Database snapshot:
回答1:
You are right, I can confirm the behaviour.
I did a few tests and, apparently, if you change the validation rule to
Like "%@%.%"
it works in C#, but it won't work in Access anymore. I guess the reason is that Access uses DAO for data access, which uses *
as the wildcard character, whereas OLEDB's wildcard character is %
.
I found that the following validation rule produces the desired result in both Access and .NET:
Like "*@*.*" Or Like "%@%.%"
来源:https://stackoverflow.com/questions/17213787/sql-executes-well-in-ms-access-but-not-in-c-sharp-when-validation-rule-is-set