SqlConnection con = Class1.DBCon();
          con.Open();
         SqlCommand cmd = new SqlCommand("Survey_Question_Save",con);
         cmd.CommandType = CommandType.StoredProcedure;
         cmd.Parameters.Add(new SqlParameter("@QuestionName", SqlDbType.NVarChar, 50));
         string QuestionNm = TextBox1.Text.ToString();
         cmd.Parameters.Add(new SqlParameter("@QuestionId",SqlDbType.Int,4));
         cmd.Parameters["@QuestionId"].Value=??????
  我的存储过程是增加Question 其中QuestionId是主键,递增的,identity  当调用存储过程时,如何给它赋值。
        存储过程如下:CREATE procedure Survey_Question_Save
@QuestionId int,
@SurveyId int,
@QuestionName nvarchar(50),
@OptionNum int,
@JoinNum int
as
if @QuestionId=0
begin begin Transaction
insert into [Survey_Quesiton]([SurveyId],[QuestionName],[OptionNum],[JoinNum]) values (@SurveyId,@QuestionName,@OptionNum,@JoinNum) 
set @QuestionId=@@identity
if @@error<>0
 rollback
else 
  commit;
end
else 
begin
update [Survey_Question] set [QuestionName]=@QuestionName,[OptionNum]=@OptionNum,[JoinNum]=@JoinNum where QuestionId=@QuestionId
end
GO

解决方案 »

  1.   

    QuestionId是主键,递增的
    --------------------
    那你就不用为QuestionId赋值了,插入的时候去掉这个列就行了。也就是不用参数@QuestionId int
    当调用存储过程时,如何给它赋值
    ---------------------
    exec Survey_Question_Save '"+参数一的值+"','"++参数二的值"'     依此类推。
      

  2.   

    cmd.Parameters["@QuestionId"].Value=?????? 
    -------------------------------
    自增列不用手动赋值,你可以在存储过程中将这个参数去掉就行了。