查询代码如下:
/// <summary>
/// 执行sql并返回记录的1行1列
/// </summary>
/// <param name="sqlCommand"></param>
/// <returns></returns>
public object ExecuteScalar(string sqlCommand)
{
if(TransactionContext.Current!=null) //需要执行事务
{
try
{
return TransactionContext.Current.Provider.Database.ExecuteScalar(TransactionContext.Current.DbTransaction, CommandType.Text, sqlCommand);
}
catch(Exception ex)
{
TransactionContext.Current.Rollback();
throw new Exception(ex.Message);
}
}
else //不需要事务
{
return _provider.Database.ExecuteScalar(_provider.ConnectionString, CommandType.Text, sqlCommand);
}
}返回的是Object,这里的Object是不是可以是“DataTable”,也可以是“int”呢?至少可以转换为“int”吧
/// <summary>
/// 执行sql并返回记录的1行1列
/// </summary>
/// <param name="sqlCommand"></param>
/// <returns></returns>
public object ExecuteScalar(string sqlCommand)
{
if(TransactionContext.Current!=null) //需要执行事务
{
try
{
return TransactionContext.Current.Provider.Database.ExecuteScalar(TransactionContext.Current.DbTransaction, CommandType.Text, sqlCommand);
}
catch(Exception ex)
{
TransactionContext.Current.Rollback();
throw new Exception(ex.Message);
}
}
else //不需要事务
{
return _provider.Database.ExecuteScalar(_provider.ConnectionString, CommandType.Text, sqlCommand);
}
}返回的是Object,这里的Object是不是可以是“DataTable”,也可以是“int”呢?至少可以转换为“int”吧
解决方案 »
- 只从用了【CodeSmith Studio】 手写代码能力直线下降了
- 为什么checkbox全选不能选中,只想选中其中一列的checkbox?代码如下。
- 关于SQL的问题,跪求解决方法
- Microsoft JScript 运行时错误: 属性“$”的值为 null、未定义或不是 Function 对象
- 急死人了!SQL存储过程的问题,感觉很难,急求解决方案!!!!
- 我有一字符数组,应如何实现数据源绑定呢
- 在VS2005中使用URLRewrite 出现的问题.无法创建控件
- 求救:这个问题不知如何解决?
- 急,急,急 网页乱码的问题,多谢,多谢
- 如何在asp.net页面自动获取服务器端消息??
- 视频的分享功能:获得该视频的flash地址和html地址
- 后台动态绑定Gridview时,数据怎么绑定了2次
问题是这样的:
这个Object是个Dt吧,怎么可以转换为Int呢???
if(dt != null && dt.Rows.Count > 0)
{
int result = (int)(dt.Rows[0][0].ToString()==""?"0":dt.Rows[0][0].ToString());
}
{
string strsql = "select max(" + FieldName + ")+1 from " + TableName;
object obj = SqlHelper.GetSingle(strsql);
if (obj == null)
{
return 1;
}
else
{
return int.Parse(obj.ToString());
}
}判断if (obj == null)是否为空其实他就是返回一个值 不是DataTable