存储过程
CREATE PROCEDURE insert1
@in_num char(15),
@aaa char(15)
@id int out
as
set nocount on
insert into aaa(in_num,aaa) values(@in_num,@aaa)
select @id = @@identity
GO调用代码:
str conn="----" //连接字符串
 SqlParameter[] parms = new SqlParameter[]
        {
            new SqlParameter("@in_num",SqlDbType.Char,15),
            new SqlParameter("@aaa",SqlDbType.Char,15)
            new SqlParameter("@id",SqlDbType.Int)
        };
        parms[0].Value = "testok";
        parms[1].Value = "testok1";
        parms[2].Direction = ParameterDirection.Output;SqlDataReader myDr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "insert1", parms);
        string id = myDr.GetValue("@id").ToString();
        Response.Write(id);为什么系统提示在没有任何数据时进行无效的读取尝试。
错误行: string id = myDr.GetValue("@id").ToString();
请帮忙看看!

解决方案 »

  1.   

    补充:
     string id = myDr.GetValue("@id").ToString();写错了,
    应该是
    string id = myDr.GetValue(0).ToString();谢谢!!
      

  2.   

    string id = parms[2].Value.ToString();
      

  3.   

    object o = SqlHelper.ExecuteScalar(connection,CommandType.Text,sql,pmType,pmUserAcount,pmIntegral,pmRelationId,pmRelationDate,pmCreator);
    this.id = int.Parse(o.ToString());  
    id就是你要插入记录的id
      

  4.   

    if(myDr.HasRows)
    {
    ...
    }