先由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的值。即自增字段的值
请大侠们帮忙看看,先行谢谢了。 

解决方案 »

  1.   

    cooolchen:
    能再说明白点吗
      

  2.   

    在insert 语句后面加 select @@identity
      

  3.   

    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 select @@identity    else 
    RAISERROR  ('Input Data Error: Only Category is Application or Customization, then Application Name should not be none',16,  1) ;    GO 
      

  4.   

    select Scope_identity()函数替代 select @@identity更好点