[英]INSERT query OleDB C#
I know it was 1000000000 times already, but none solution helped to me. 我知道它已经有10亿次,但没有任何解决方案对我有帮助。 I want to insert data in C# using OleDB. 我想使用OleDB在C#中插入数据。 I tried mln solutions but here is the easiest one which should work but it doesn't: 我试过mln解决方案,但这里最简单的应该可以使用,但它没有:
SQLCONNECTION = @"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=|DataDirectory|\dbScenariusz.mdb";
using (OleDbConnection connection = new OleDbConnection(SQLCONNECTION))
{
string sql = "INSERT INTO Table (content) VALUES('lala')";
connection.Open();
OleDbCommand command = new OleDbCommand(sql, connection);
command.ExecuteNonQuery();
connection.Close();
}
SQLCONNECTION is ok. SQLCONNECTION没问题。 It works fine for the SELECT query. 它适用于SELECT查询。
string sql - I tried this query in Access and it works fine. string sql - 我在Access中尝试了这个查询,它运行正常。
I get no error . 我没有错 。 It just didn't insert anything to the database. 它只是没有向数据库插入任何内容。 When I run the query in Access (the same database) the row is inserted. 当我在Access(同一数据库)中运行查询时,将插入行。
The strange thing is that command.ExecuteNonQuery(); 奇怪的是command.ExecuteNonQuery(); returns 1! 返回1! That means that 1 row was affected! 这意味着1排受到影响!
I really have no idea where the problem is, so I really appreciate any help. 我真的不知道问题出在哪里,所以我非常感谢任何帮助。 Sorry for my english. 对不起我的英语不好。
UPDATE: Another strange thing. 更新:另一件奇怪的事情。 I change query to update and it works fine! 我更改查询更新,它工作正常! really wtf? 真的wtf? :) :)
You are connecting to the DataDirectory. 您正在连接到DataDirectory。 Is the .mbd file being copied after the build? 是否在构建后复制.mbd文件? In other words are you re-deploying the database with each build and thereby losing the inserts? 换句话说,您是否在每次构建时重新部署数据库,从而丢失了插入内容?
A quick test could be using the same code and same connection to do a select after the insert. 快速测试可能是使用相同的代码和相同的连接在插入后进行选择。
In the solution Explorer, check app.config
. 在解决方案资源管理器中,检查app.config
。 Double click the app.config
, then re-enter the connectionString
. 双击app.config
,然后重新输入connectionString
。 Give the path of your database. 提供数据库的路径。 For example, in my project the default location of connectionString is: 例如,在我的项目中,connectionString的默认位置是:
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\App_Data\\database.accdb;Persist Security Info=True" connectionString =“Provider = Microsoft.ACE.OLEDB.12.0; Data Source = | DataDirectory | \\ App_Data \\ database.accdb; Persist Security Info = True”
Suppose the database is stored in this location: 假设数据库存储在以下位置:
C:\\Documents and Settings\\Amuk\\My Documents\\Visual Studio 2010\\Projects\\insertion\\insertion\\App_Data C:\\ Documents and Settings \\ Amuk \\ My Documents \\ Visual Studio 2010 \\ Projects \\ insertion \\ insertion \\ App_Data
therefore, replace the connectionString with 因此,用。替换connectionString
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Documents and Settings\\Amuk\\My Documents\\Visual Studio 2010\\Projects\\insertion\\insertion\\App_Data\\database.accdb;Persist Security Info=True" connectionString =“Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\\ Documents and Settings \\ Amuk \\ My Documents \\ Visual Studio 2010 \\ Projects \\ insertion \\ insertion \\ App_Data \\ database.accdb; Persist Security Info = True”
your problem will be solved, i hope this will definitely help you, i was also getting the same problem, and by replacing the connectionString
with original path, the database is storing all records. 您的问题将得到解决,我希望这肯定会帮助您,我也遇到了同样的问题,并通过用原始路径替换connectionString
,数据库存储所有记录。
You can try to use transaction to commit your changes. 您可以尝试使用事务来提交更改。
command.Transaction = connection.BeginTransaction(); //after connection.open()
Then add 然后加
command.Transaction.Commit(); //Before connection.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.