ASP.NET如果调用存储过程?分为多种~带参数,带返回值 之类的一些情况。。我的存储过程如下,就是生成卡号~
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
goALTER proc [dbo].[GetCode]
@shuliang int
as
declare @i int
    declare @OrderNumber nvarchar(50)
declare @bianliang nvarchar(50)
set @i = 0 
while @i < @shuliang 
begin
  select top 1 @OrderNumber=UserName from lk_Users order by userid desc
  set @bianliang=right('0000000000'+cast(right(@OrderNumber,10)+1 as varchar),10)
      insert into lk_Users(username,password) values('2012HJLS'+@bianliang,'***')
  set @i = @i + 1 
end 后台调用代码:
 protected void Button1_Click(object sender, EventArgs e)
    {
        int shu = int.Parse(TextBox1.Text);        string connection = ConfigurationSettings.AppSettings["con"];
        //建立连接并打开
        SqlConnection con = new SqlConnection(connection);
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "GetCode";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@shuliang", SqlDbType.Int,4);
        cmd.Parameters["@shuliang"].Value = shu;
     
        cmd.ExecuteNonQuery();
        con.Close();
        con.Dispose();
        cmd.Dispose();
    }这样做好像不正确~~请教高手指点~谢谢!

解决方案 »

  1.   

     SqlCommand cmd = new SqlCommand(con);
      

  2.   

    没看到你的stored procedure里返回值了。
      

  3.   

    Return或者Output返回值接受
      

  4.   


    我1楼的试了吗?你的Connection没有用在Command里面。
      

  5.   


       protected void Button1_Click(object sender, EventArgs e)
        {
            int shu = int.Parse(TextBox1.Text);        string connection = ConfigurationSettings.AppSettings["con"];
            //建立连接并打开
            SqlConnection con = new SqlConnection(connection);
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.Connection.Open();
            cmd.CommandText = "GetCode";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@shuliang", SqlDbType.Int,4);
            cmd.Parameters["@shuliang"].Value = shu;
            cmd.ExecuteNonQuery();
            con.Close();
            con.Dispose();
            cmd.Dispose();
        }
    这样调用正确了,但是我感觉此代码有点longyu  ~~~~ Connection 放在command 里面 得需要  CommandText