我写了一个存储过程:
Create Procedure Pro_PostSubject
@PostName nvarchar(100),
@PostSubject nvarchar(200),
@PostMessage text,
@PostTime nvarchar(30),
@PostIP nvarchar(20)
AS
begin tran
insert into post(postname,subject,message,posttime,ip) values(@Postname,@PostSubject,@PostMessage,@PostTime,@PostIP)
commit tran
go
现在我要调用它  代码应该怎么写呢(用C#)
在类里面怎么写呢?
public static string InsertInto(string postname,string subject,string message,string posttime,string ip)
{
SqlConnection conn= new SqlConnection("server=.;database=msgboard;uid=sa;pwd=123456;);
SqlCommand cmd= new SqlCommand();
这里再怎么写呢?
}

解决方案 »

  1.   

    public   static   string   InsertInto(string   postname,string   subject,string   message,string   posttime,string   ip)
    {
    SqlConnection   conn=   new   SqlConnection("server=.;database=msgboard;uid=sa;pwd=123456;);
    SqlCommand   cmd=   new   SqlCommand();
    string cmdText = "存储过程名 '传递的值1', '传递的值2....'";
    cmd.connetion = conn;
    cmd.commandtext = cmdText;
    cmd.ExecuteNonQuery();
    //这样就行了, 其实还有更严格的写法, cmd有一个commandtype 属性, 另外还在绑定参数.
    }
      

  2.   

    string   cmdText   =   "Pro_PostSubject   'p postname',   'p postsubject','p postmessage','p posttime','p postip'"; 
    楼主为什么在time也用 nvarchar 而不是 datetime?!
      

  3.   

    哦 谢谢你哈  我的时间是取的系统时间 然后在插入到数据库中的 并没有用 数据库自带的时间函数
     lblShowTimw.Text=DateTime.Now.ToString()
      

  4.   

    SqlConnection   conn=   new   SqlConnection("server=.;database=msgboard;uid=sa;pwd=123456;); 
    SqlCommand   cmd=   new   SqlCommand(); 
    //要输入的参数
    SqlParameter[] parms = new SqlParameter[]
                              {
                                  new SqlParameter("参数名",参数类型),
                                  .....
                              };
    cmd.ConnectionString = conn;
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "存储过程名";
    cmd.Parameters.AddRange(parms);
    cmd.ExecuteNonQuery();
      

  5.   

    sql = "exec 存储过程名 参数"直接这样就可以了,哈哈,最简单的方法