两种写法不同作用相同的存储过程

第一种:
      conn.Open();
      SqlCommand cmd=new SqlCommand("AddNews",conn);
      cmd.CommandType = CommandType.StoredProcedure;

      cmd.Parameters.Add(new SqlParameter("@id", SqlDbType.Int, 4));
      cmd.Parameters.Add(new SqlParameter("@title", SqlDbType.NVarChar, 200));
      cmd.Parameters.Add(new SqlParameter("@content", SqlDbType.NVarChar,4000));
      cmd.Parameters.Add(new SqlParameter("@publishtime", SqlDbType.DateTime, 8));
      cmd.Parameters.Add(new SqlParameter("@replies", SqlDbType.Int, 4));
      cmd.UpdatedRowSource=UpdateRowSource.None;

      cmd.Parameters["@id"].Value=art.ID;
      cmd.Parameters["@title"].Value=art.Title;
      cmd.Parameters["@content"].Value=art.Content;
      cmd.Parameters["@publishtime"].Value=art.PublishTime;
      cmd.Parameters["@replies"].Value=art.Replies;
      cmd.ExecuteNonQuery();
      conn.Close();
第二种:
  private bool BorrowReader(string ReaderID)
     {
      if(ReaderBorrowedNumber(ReaderID)<8)
   {
    SqlCommand borrowbook=new SqlCommand();
    borrowbook.Connection=this.sqlConnection1;
    borrowbook.CommandType=CommandType.StoredProcedure;
    borrowbook.CommandText="dbo.StoredProcedureBorrowReader";
    SqlParameter parinput=borrowbook.Parameters.Add("@ReaderID",SqlDbType.Char);
    parinput.Direction=ParameterDirection.Input;
    parinput.Value=ReaderID;
    try
    {
     this.sqlConnection1.Open();   
     borrowbook.ExecuteNonQuery();
     this.sqlConnection1.Close();
     return true;
    }
    catch(System.Exception e)
    {
     this.ErrorHandle(e);     
     this.sqlConnection1.Close();
     return false;

    }
   }
   else
    return false;
  }


些以上两种方法中颜色加粗的进行结合,书写与理解更好。

posted on 2006-08-11 00:32  C#家园  阅读(383)  评论(0编辑  收藏  举报