怎样在C#中运用存储过程向数据库进行修改添加

解决方案 »

  1.   

    Ado.NET/ActiveX Data Object.NET
      

  2.   

    小例子:        public static void CommDeleteAndUpdate(int querytype, string selecttext)
            {
                SqlDataAdapter sda = new SqlDataAdapter();
                SqlCommand cmd = new SqlCommand("lt_query", GetConn());
                sda.SelectCommand = cmd;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@querytype", System.Data.SqlDbType.Int).Value = querytype;
                cmd.Parameters.Add("@selecttext", System.Data.SqlDbType.NVarChar, 4000).Value = selecttext;
                DataSet ds = new DataSet();
                sda.Fill(ds);
                GetConn().Close();
            }SET QUOTED_IDENTIFIER ON 
    GO
    SET ANSI_NULLS ON 
    GOALTER    proc lt_inoutquery
    (
    @querytype int,
    @selecttext nvarchar(4000)
    )
    as
    declare @selectsql nvarchar(4000)--最终查询语句
    if(@querytype=1)--历史记录查询
    begin
    set @selectsql=@selecttext
    exec (@selectsql)
    end
    else if(@querytype=2)--同批次查询
    begin
    set @selectsql=@selecttext
    exec (@selectsql)
    end
    else if(@querytype=3)
    begin
    set @selectsql=@selecttext
    exec (@selectsql)
    endGO
    SET QUOTED_IDENTIFIER OFF 
    GO
    SET ANSI_NULLS ON 
    GO
      

  3.   

    using(SqlConnection   con=   new   SqlConnection(connectionString);)
    {    
    SqlCommand   cmd=new   SqlCommand("proc_Insert",con);   
      cmd.CommandType=CommandType.StoredProcedure;      
      SqlParameter  s1=new   SqlParameter("@name",SqlDbType.VarChar,20);   
      parProvinceID.Value="";   
      cmd.Parameters.Add(s1);     
      con.Open();   
      int result=cmd.ExecuteNonQuery();   
      con.Close();   
      if(result>0)   
      {   
      return   true;   
      }   
      else   
      {   
      return   false;   
      }   
    }
    create proc proc_Insert
    @name VARCHAR(20)
    AS
    DECLARE @sql varchar(200)
    set @sql=' insert into Tb(name) values('+@name+')'
    exec @sql
    可看看sqlhelper或51aspx.com里看看,操作实例很多