我的目的是向一个表中添加一条记录,表中的字段是:表名为:aaa
productid    productname   operate  userid   operateDateproductid和userid都是上一个页面传过来来的,怎么写这个存储过程呀?我没用过存储过程,以及在
c#中如何使用这个存储过程传递参数啊!恳求各位大侠帮忙!!!

解决方案 »

  1.   

    CREATE PROCEDURE Insertaaa
    @productid varchar(50),
    @productname varchar(200),
    @operate varchar(50),
    @userid varchar(50),
    @operateDate DateTime AS
    insert into aaa(productid,productname,operate,userid,operateDate) values(@productid,@productname,@operate,@userid,@operateDate)GO
      

  2.   

    System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection( "server = localhost;database = 数据库名;user id = sa ;pwd=" );
    conn.Open();
    System.Data.SqlClient.SqlCommand  cmd = new System.Data.SqlClient.SqlCommand();
    cmd.CommandText = "你的存储过程名";
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Connection = conn;
    //给存储过程的参数声名和赋值
    cmd.Parameters.Add( "@参数1",SqlDbType.NChar);
    cmd.Parameters["@参数1"].Value = "aaaa";
    cmd.ExecuteNonQuery();
      

  3.   

    comm = new OleDbCommand("sp_Delete_QuanXianRole",conn);
    comm.CommandType = CommandType.StoredProcedure;
    comm.Parameters.Add("@IIDD",OleDbType.Integer,16).Value = 15;
    comm.ExecuteNonQuery();
      

  4.   

    http://www.i2key.com/TechDoc/Index.aspx?grads=0&Curpage=1&SearchKey=%b4%e6%b4%a2%b9%fd%b3%cc&ID=
      

  5.   

    我的存储过程:
    CREATE PROCEDURE minususermoney (
     @userid nvarchar(10),
                                @productprice int,
                                @productid int ,
                                @operate  nvarchar(50)   
                           
    )AS update  useraccount
    set balance=balance-@productprice,ableinvoice=ableinvoice+@productprice,totalmoney=totalmoney+@productprice WHERE userid=@userid
             
              insert into  optLog  (productid,operate,userid) values(@productid,  @operate,@userid )


    GO
    我的调用:
      string connstr = "Data Source=MASONG;Initial Catalog=kj250;Persist Security Info=True;User ID=sa";
            SqlConnection logconn = new SqlConnection();
            SqlCommand logcomm = new SqlCommand();
            logconn.ConnectionString = connstr;
            logcomm.Connection = logconn;
            logcomm.CommandType = CommandType.StoredProcedure;
            logcomm.CommandText = "minususermoney";
            logcomm.Parameters.Add(" @userid", SqlDbType.NVarChar, 10).Value = userid;
            logcomm.Parameters.Add(" @productprice", SqlDbType.Int, 4).Value = ductprice;
            logcomm.Parameters.Add(" @productid", SqlDbType.Int, 4).Value = productid;
            logcomm.Parameters.Add(" @operate", SqlDbType.NVarChar, 50).Value = operate;
            logconn.Open();
            logcomm.ExecuteNonQuery();
    有问题吗?执行报错:@ @userid 不是过程 minususermoney 的参数。
    我用事件探查器跟踪这样的结果:
    exec minususermoney @ @userid = N'a1', @ @productprice = 50, @ @productid = 7, @ @operate = N'发发' 什么原因,怎么变量前面两个@!!!!!
      

  6.   

    comm = new OleDbCommand("sp_Delete_QuanXianRole",conn);
    comm.CommandType = CommandType.StoredProcedure;
    comm.Parameters.Add("@参数1",OleDbType.nvarchar,160).Value =  comm.Parameters.Add("@参数",OleDbType.Integer,16).Value =  
    comm.ExecuteNonQuery();
      

  7.   

    其实楼主可以查下msdn或者上google找一下,一大把