在.net中使用sql存储过程

1:dataset
string con=ConfigurationSettings.AppSettings["dsn"];
 SqlConnection  conn = new SqlConnection(con);//
连接字符串
 conn.Open();
 SqlDataAdapter myCommand = new SqlDataAdapter();  //创建SqlDataAdapter 类
 myCommand.SelectCommand=new SqlCommand("sp_searchCkArticleByTitle",conn);
 myCommand.SelectCommand.CommandType=CommandType.StoredProcedure ;
 SqlParameter title=myCommand.SelectCommand.Parameters.Add("@title",SqlDbType.NVarChar ,500);
  title.Value=Request["keyword"] ;
DataSet ds=new DataSet();  //建立并填充数据集
 myCommand.Fill(ds,"Articl");
 MyDataGrid.DataSource=ds;    //绑定
 MyDataGrid.DataBind();
 conn.Close();
2:SqlCommand
try
    {   
     SqlCommand cd=new SqlCommand("sp_addFclass",myConnection); 
     cd.CommandType=CommandType.StoredProcedure;  
     
     SqlParameter className= cd.Parameters.Add("@className", SqlDbType.Char,40);    
     className.Value=ClassName.Text;
     
      int se=cd.ExecuteNonQuery() ;    
     
 myLabel.Text="addsuccess!"
myConnection.Close();          }
      catch(SqlException e)
    {
     Response.Write("Exception in Main: " + e.Message); 
    }
3:datareader
try
   {
    string con=ConfigurationSettings.AppSettings["dsn"]; //取得DSN字符
   
    SqlConnection myConnection = new SqlConnection(con);//连接字符串
    myConnection.Open();          //连接数据库
    
    SqlCommand cd=new SqlCommand("sp_selAdmin",myConnection); //设置SQL命令
    cd.CommandType=CommandType.StoredProcedure;   //调用存储过程

    SqlParameter username= cd.Parameters.Add("@username", SqlDbType.Char,240);    //设置存储过程参数
    SqlParameter password= cd.Parameters.Add("@password", SqlDbType.NChar,240);
    username.Value=t1;
    password.Value=t2;

    SqlDataReader selreader ; //设置SqlDataReader 类
    selreader=cd.ExecuteReader();       //命令执行
    
    if (selreader.Read()==true) 
{
........
}
catch(Exception ex)
{
 Response.Write("Exception in Main: " + e.Message); 
}

posted @ 2007-06-05 14:52  早班火车  阅读(268)  评论(0编辑  收藏  举报