日期:2014-05-18  浏览次数:20487 次

一个 值获取的问题
public int insertbook(string bookname,int classid, string author, string publish, string publishdate, string isbn, string oldprice, string nowprice, string zaiyao, int tuijian, int isonframe, int ischeap)//添加图书
  {
  SqlConnection conn = new SqlConnection(common.Class1.con);
  conn.Open();
  SqlCommand com = new SqlCommand(common.Class1.insertb, conn);
  com.CommandType = CommandType.StoredProcedure;
  com.Parameters.Add("@bookname", SqlDbType.VarChar, 50).Value = bookname;
  com.Parameters.Add("@classid", SqlDbType.Int).Value = classid;
  com.Parameters.Add("@author", SqlDbType.VarChar, 50).Value = author;
  com.Parameters.Add("@publish", SqlDbType.VarChar, 200).Value = publish;
  com.Parameters.Add("@publishdate", SqlDbType.VarChar, 50).Value = publishdate;
  com.Parameters.Add("@isbn", SqlDbType.VarChar, 50).Value = isbn;
  com.Parameters.Add("@oldprice", SqlDbType.VarChar, 50).Value = oldprice;
  com.Parameters.Add("@nowprice", SqlDbType.VarChar, 50).Value = nowprice;
  com.Parameters.Add("@zaiyao", SqlDbType.VarChar, 200).Value = zaiyao;
  com.Parameters.Add("@tuijian", SqlDbType.Int).Value = tuijian;
  com.Parameters.Add("@isonframe", SqlDbType.Int).Value = isonframe;
  com.Parameters.Add("@ischeap", SqlDbType.Int).Value = ischeap;
  com.Parameters.Add("@bid", SqlDbType.Int);
  com.Parameters["@bid"].Direction = ParameterDirection.Output;//我要获取@bid的值啊!怎么写才对啊.以下获取都错了.!  
  SqlDataAdapter da = new SqlDataAdapter();
  da.InsertCommand = com; 
  com.ExecuteNonQuery();
  int temp = Convert.ToInt32(com.ExecuteScalar());
  conn.Close();
  return temp;
  }

存储过程:
CREATE PROCEDURE inserbook
@bookname varchar(50),
@classid int,
@author varchar(50),
@publish varchar(200),
@publishdate varchar(50),
@isbn varchar(50),
@oldprice varchar(50),
@nowprice varchar(50),
@zaiyao varchar(200),
@tuijian int,
@ischeap int,
@isonframe int,
@bid int output

 AS
BEGIN
 set nocount on;
 insert into book (bookname,classid,author,publish,publishdate,isbn,oldprice,nowprice,zaiyao,tuijian,ischeap,isonframe) values (@bookname,@classid,@author,@publish,@publishdate,@isbn,@oldprice,@nowprice,@zaiyao,@tuijian,@ischeap,@isonframe)
select @bid=@@identity //要得是它那个系统自加的IDreturn
END
GO


------解决方案--------------------
int temp = Convert.ToInt32(com.ExecuteScalar()); 

改成int temp=(int)cmdDB.Parameters["@bid"].Value;


C# code

   /// <summary>
        /// 记录是否存在
        /// </summary>
        /// <param name="ItemID">1 baidu知道 2 新闻 3 图片</param>
        /// <param name="TypeID">1-1 人 1-2问题  2-1 新闻  3-1 图片</param>
        /// <param name="Ident">判断记录的唯一条件比如说网址,姓名</param>
        /// <param name="MobileID">请传入当前某个频道循环的PKID</param>
        /// <returns></returns>
        public static int ExistRows(int ItemID, int TypeID, string Ident, int MobileID)
        {
            SqlConnection conDB = new SqlConnection(strLinkDB);
            conDB.Open();
            SqlCommand cmdDB = new SqlCommand("Item_Exist