先由execute_scalar去访问数据库。
public object execute_scalar(string sql)
{
if (Util.get_setting("LogSqlEnabled", "1") == "1")
{
Util.write_to_log("sql=\n" + sql);
} using (SqlConnection conn = get_sqlconnection())
{
object returnValue;
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
returnValue = cmd.ExecuteScalar();
conn.Close();
return returnValue;
}
}
SQL语句如下:
sql = "insert into bugs(bg_short_desc,bg_reported_user,
其中bugs表中还有一个字段是自动增加的,叫bg_id。
在下面这个代码里面执行SQL语句,
一旦增加了触发器,就总是再插入一条记录时,就报错,但后台这条记录却也增加到数据库中了,错误信息如下: 对象不能从 DBNull 转换为其他类型。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 对象不能从 DBNull 转换为其他类型。 int bugid = Convert.ToInt32(dbutil.execute_scalar(sql)); 好像是增加了触发器,dbutil.execute_scalar(sql));这个方法,执行了添加纪录后无法返回自增字段的值。 本来这些代码执行都没有问题,为了一些业务需求,我增加了一个触发器如下:
Create trigger tri_bugs on bugs instead of insert
as
if exists(select 1 from inserted where (bg_category in (6,7) and [Application Name] !='None') or (bg_category not in (6,7) and [Application Name] ='None'))
insert into bugs(bg_short_desc,bg_reported_user,bg_reported_date,
bg_status,bg_priority,bg_org,bg_category,bg_project,bg_assigned_to_user,bg_last_updated_user,
bg_last_updated_date,bg_user_defined_attribute,[Application Name],Weighting,Modules,
[Preferred Engineer],Owner,Detail,Solution,[Finished Date])
select bg_short_desc,bg_reported_user,bg_reported_date,
bg_status,bg_priority,bg_org,bg_category,bg_project,bg_assigned_to_user,bg_last_updated_user,
bg_last_updated_date,bg_user_defined_attribute,[Application Name],Weighting,Modules,
[Preferred Engineer],Owner,Detail,Solution,[Finished Date] from inserted else
RAISERROR ('Input Data Error: Only Category is Application or Customization, then Application Name should not be none',16, 1) ; GO 触发器是可以触犯的,也能实现我的想要的功能
int bugid = Convert.ToInt32(dbutil.execute_scalar(sql));
这里的bugid就是添加进数据库的纪录的字段bg_id的值。即自增字段的值
请大侠们帮忙看看,先行谢谢了。
public object execute_scalar(string sql)
{
if (Util.get_setting("LogSqlEnabled", "1") == "1")
{
Util.write_to_log("sql=\n" + sql);
} using (SqlConnection conn = get_sqlconnection())
{
object returnValue;
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
returnValue = cmd.ExecuteScalar();
conn.Close();
return returnValue;
}
}
SQL语句如下:
sql = "insert into bugs(bg_short_desc,bg_reported_user,
其中bugs表中还有一个字段是自动增加的,叫bg_id。
在下面这个代码里面执行SQL语句,
一旦增加了触发器,就总是再插入一条记录时,就报错,但后台这条记录却也增加到数据库中了,错误信息如下: 对象不能从 DBNull 转换为其他类型。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 对象不能从 DBNull 转换为其他类型。 int bugid = Convert.ToInt32(dbutil.execute_scalar(sql)); 好像是增加了触发器,dbutil.execute_scalar(sql));这个方法,执行了添加纪录后无法返回自增字段的值。 本来这些代码执行都没有问题,为了一些业务需求,我增加了一个触发器如下:
Create trigger tri_bugs on bugs instead of insert
as
if exists(select 1 from inserted where (bg_category in (6,7) and [Application Name] !='None') or (bg_category not in (6,7) and [Application Name] ='None'))
insert into bugs(bg_short_desc,bg_reported_user,bg_reported_date,
bg_status,bg_priority,bg_org,bg_category,bg_project,bg_assigned_to_user,bg_last_updated_user,
bg_last_updated_date,bg_user_defined_attribute,[Application Name],Weighting,Modules,
[Preferred Engineer],Owner,Detail,Solution,[Finished Date])
select bg_short_desc,bg_reported_user,bg_reported_date,
bg_status,bg_priority,bg_org,bg_category,bg_project,bg_assigned_to_user,bg_last_updated_user,
bg_last_updated_date,bg_user_defined_attribute,[Application Name],Weighting,Modules,
[Preferred Engineer],Owner,Detail,Solution,[Finished Date] from inserted else
RAISERROR ('Input Data Error: Only Category is Application or Customization, then Application Name should not be none',16, 1) ; GO 触发器是可以触犯的,也能实现我的想要的功能
int bugid = Convert.ToInt32(dbutil.execute_scalar(sql));
这里的bugid就是添加进数据库的纪录的字段bg_id的值。即自增字段的值
请大侠们帮忙看看,先行谢谢了。
解决方案 »
- SWFUpload 获取图片路径的问题
- 无法调度消息,因为终结点地址“net.tcp:**”上的服务对该地址的协议不可用。
- 跪求vs2008适用的分页控件!
- Response.Redirect 如何打开新页面?
- 关于现在网站编写的语言
- asp.ent验证控件的问题!
- VS2005项目发布后放IIS中不能访问。。。。。。。。。
- (求助)我用ASP.NET写的第一个邮件发送程序遇到的问题
- B/S架构的程序中,如何处理后台定时周期性任务?
- 有关CheckBox的问题,请多多指教!
- 怎么实现无刷新验证textbox是否为空?
- GridView鼠标移出某一行获取行号,或者某一列的值,或者DataKeyNames的 ID
能再说明白点吗
as
if exists(select 1 from inserted where (bg_category in (6,7) and [Application Name] !='None') or (bg_category not in (6,7) and [Application Name] ='None'))
insert into bugs(bg_short_desc,bg_reported_user,bg_reported_date,
bg_status,bg_priority,bg_org,bg_category,bg_project,bg_assigned_to_user,bg_last_updated_user,
bg_last_updated_date,bg_user_defined_attribute,[Application Name],Weighting,Modules,
[Preferred Engineer],Owner,Detail,Solution,[Finished Date])
select bg_short_desc,bg_reported_user,bg_reported_date,
bg_status,bg_priority,bg_org,bg_category,bg_project,bg_assigned_to_user,bg_last_updated_user,
bg_last_updated_date,bg_user_defined_attribute,[Application Name],Weighting,Modules,
[Preferred Engineer],Owner,Detail,Solution,[Finished Date] from inserted select @@identity else
RAISERROR ('Input Data Error: Only Category is Application or Customization, then Application Name should not be none',16, 1) ; GO