C#问题Access数据库 [英] C# problem Access database
本文介绍了C#问题Access数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
OleDbConnection h5 = new OleDbConnection( Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:\\users \\IJC Database.accdb);
OleDbCommand cmd5;
h5.Open();
cmd5 = new OleDbCommand( select MAX (ID)来自ijc + ;,h5);
cmd5.ExecuteNonQuery();
如何从cmd5命令获取结果并将此结果赋予变量。
解决方案
ExecuteNonQuery返回命令中涉及的行数。它可能正在运行您的命令,但由于没有行受到影响,因此返回0。 ExecuteNonQuery应该用于更新,插入和删除,而不是选择。
而不是使用cmd5.ExecuteNonQuery(),请执行以下操作:
int maxID =( int )cmd5.ExecuteScalar();
然后maxID将保存查询中的MaxID值。
< blockquote>使用 ExecuteScalar
而不是 ExecuteNonQuery
。
请参阅 DbCommand .ExecuteScalar方法
OleDbConnection h5 = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\users\\IJC Database.accdb");
OleDbCommand cmd5;
h5.Open();
cmd5 = new OleDbCommand("select MAX(ID) from ijc"+";",h5);
cmd5.ExecuteNonQuery();
how can i take the result from cmd5 command and give this result to variable.
解决方案
ExecuteNonQuery returns the number of rows involved in the command. Its probably running your command, but returning 0 since no rows were affected. ExecuteNonQuery should be used for Updates, Inserts, and Deletes, not Selects.
Instead of using cmd5.ExecuteNonQuery(), do this:
int maxID = (int)cmd5.ExecuteScalar();
Then maxID will hold the MaxID value from the query.
UseExecuteScalar
instead ofExecuteNonQuery
.
See DbCommand.ExecuteScalar Method
这篇关于C#问题Access数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文