ExecuteScalar() 与CommandBehavior.SingleRow返回区别

本文探讨了如何通过使用CommandBehavior.SingleRow优化SQL查询,提高应用程序执行效率。具体介绍了SQL连接、命令创建、读取单行数据及关闭连接的流程,并强调了在返回单行记录时使用此选项的好处。

ExecuteScalar()返回单个结果(第一行第一列的值),而

CommandBehavior.SingleRow值表示应该返回完整的一行(返回第一行的所有列的值).

 

附上CommandBehavior.SingleRo使用的一个实例,它比

SqlConnection MyConnection = new SqlConnection(YourOwnConnectionString);

SqlCommand MyCommand = new SqlCommand("SELECT mycount = COUNT(*) FROM Table",MyConnection);

MyConnection.Open();

SqlDataReader dr = MyCommand.ExecuteReader(CommandBehavior.SingleRow);

if (dr.Read())

DataGrid1.VirtualItemCount = (int)dr["mycount"];

dr.Close();

MyConnection.Close();

 

注意:

MyCommand.ExecuteReader(CommandBehavior.SingleRow),而大家一般用的只是MyCommand.ExecuteReader(),但请记住,如果返回记录集只有一行的话,用CommandBehavior.SingleRow进行标识可以提高应用程序的执行效率。这是因为不注明的一般情况下,MyCommand执行是使用 IRowset 接口。而注明了之后,就会利用IRow 接口(如果可用)执行绑定。

 

System.Data.SQLite.SQLiteException HResult=0x87AF202F Message=constraint failed UNIQUE constraint failed: tab_test.id Source=System.Data.SQLite StackTrace: at System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt) at System.Data.SQLite.SQLiteDataReader.Step(SQLiteStatement statement) at System.Data.SQLite.SQLiteDataReader.NextResult() at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave) at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior) at System.Data.SQLite.SQLiteCommand.ExecuteScalar(CommandBehavior behavior) at System.Data.SQLite.SQLiteCommand.ExecuteScalar() at SqlSugar.AdoProvider.GetScalar(String sql, SugarParameter[] parameters) at SqlSugar.AdoProvider.GetInt(String sql, SugarParameter[] parameters) at SqlSugar.InsertableProvider`1.ExecuteReturnIdentity() at Xcf.Global.DB.Insertable[T](List`1 mode) in F:\ProjectDir\VSProject\应知应会系统\NeedKnowNeedCan1\NeedKnowNeedCan\NeedKnowNeedCan\Global\GlobalSetting.cs:line 94 at Xcf.UI.Controls.UC_ManagerMain.ImportMain(List`1 pathList) in F:\ProjectDir\VSProject\应知应会系统\NeedKnowNeedCan1\NeedKnowNeedCan\NeedKnowNeedCan\UI\Controls\UC_ManagerMain.xaml.cs:line 1199 at Xcf.UI.Controls.UC_ManagerMain.<>c__DisplayClass52_0.<Btn_批量导入1>b__0() in F:\ProjectDir\VSProject\应知应会系统\NeedKnowNeedCan1\NeedKnowNeedCan\NeedKnowNeedCan\UI\Controls\UC_ManagerMain.xaml.cs:line 1120 at System.Threading.Tasks.Task.InnerInvoke() in System.Threading.Tasks\Task.cs:line 2404 at System.Threading.Tasks.Task.Execute() in System.Threading.Tasks\Task.cs:line 2194 此异常最初是在此调用堆栈中引发的: System.Data.SQLite.SQLite3.Step(System.Data.SQLite.SQLiteStatement) System.Data.SQLite.SQLiteDataReader.Step(System.Data.SQLite.SQLiteStatement) System.Data.SQLite.SQLiteDataReader.NextResult() System.Data.SQLite.SQLiteDataReader.SQLiteDataReader(System.Data.SQLite.SQLiteCommand, System.Data.CommandBehavior) System.Data.SQLite.SQLiteCommand.ExecuteReader(System.Data.CommandBehavior) System.Data.SQLite.SQLiteCommand.ExecuteScalar(System.Data.CommandBehavior) System.Data.SQLite.SQLiteCommand.ExecuteScalar() SqlSugar.AdoProvider.GetScalar(string, SqlSugar.SugarParameter[]) SqlSugar.AdoProvider.GetInt(string, SqlSugar.SugarParameter[]) SqlSugar.InsertableProvider<T>.ExecuteReturnIdentity() ... [调用堆栈已截断]
最新发布
09-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值