贴子漏了一个字,不好意思在sqlserver2000存储过程中,使用select * from .... 返回一个数据集,
但这个数据集只有一行,现在我要把存储过程封装在一个c#函数中,
在asp.net页面的后台的c#文件调用该函数。
但我需要取出每一列的值(只有一行)。也即把每列值赋给相应的参数。
请问应该如何主调用存储过程,如何定义函数的参数?
由于存储过程不是我编写的,所以不能修改存储过程。
谢谢!

解决方案 »

  1.   

    找了半天也没看出来帖子漏了哪个字....晕,不过,你写的还是看不大明白.
    这是从IBuySpy里拿的一段调用存储过程的函数,参考一下.        public SqlDataReader GetSingleUser(String email) 
            {            // Create Instance of Connection and Command Object
                SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
                SqlCommand myCommand = new SqlCommand("Portal_GetSingleUser", myConnection);            // Mark the Command as a SPROC
                myCommand.CommandType = CommandType.StoredProcedure;            // Add Parameters to SPROC
                SqlParameter parameterEmail = new SqlParameter("@Email", SqlDbType.NVarChar, 100);
                parameterEmail.Value = email;
                myCommand.Parameters.Add(parameterEmail);            // Open the database connection and execute the command
                myConnection.Open();
                SqlDataReader dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);            // Return the datareader
                return dr;
            }
      

  2.   

    给你一个VB.NET的例子,改改就可以用了   
    Dim conn As New SqlConnection("Server=localhost;user id=sa;password=xxx;database=Northwind;")
            Dim cmd As New SqlCommand
            cmd.CommandText = "test"
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = conn
            Try
                conn.Open()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            Do While dr.Read
                response.write(dr("CategoryName") & "<br>")
            Loop
      

  3.   

    我的存储过程为test,内容为:CREATE PROCEDURE  test
    as 
    select * from categories
    GO
      

  4.   

    根本用不着参数啊,呵呵
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "test";
    da.SelectCommand = cmd;
    da.Fill(ds, yourtablename);
    呵呵,不过说句实在的,如果是分层大系统的话,最好还是不要象我这样写
    写个数据访问基类,里面封装一系列的RunProcedure方法。参数即使没有的话也要把他设为null
      

  5.   

    上边几位。怎么搞的;要对的起自己的两个’角‘;
    楼主我来回答你这个问题:如有不对,请指正;
    漏的一个字是:
    也即把每列值赋给相的参数
    也即把每列值赋给相应的参数。
    ----------------------
    1:
     private void getPara(out string name , out string psd)
     {
      SqlCommand cmd = new SqlCommand();
      SqlConnection con = new SqlConnection(conStr);
      cmd.Connection = con;
      cmd.CommandText = "test";
      con.open();
      SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
      if (sdr.Read())
      {
        name = sdr[0];  //name = sdr["name"];
        psd  = sdr[1];  //psd  = sdr["password"];
        
      }
      }
      
      //调用方法;
      ........
       string sn , sp;
       
       getPara(out sn ,out sp);
       this.TextBox1.Text = sn;
       this.TextBox2.Text = sp;
     ........
      

  6.   

    sorry; 忘记关闭con了;
    在getPara的最后加一句;
    cmd.Close();//connection 将会自动关闭; 不需理会; 
    ------------------------------
    喜欢对你有帮助(我想要分,分,哈哈)
      

  7.   

    int GetID=int.Parse(Request["id"]);
    SqlDataAdapter sqledit = new SqlDataAdapter("Getchose",conn.myConnection);
    sqledit.SelectCommand.CommandType=CommandType.StoredProcedure;
    sqledit.SelectCommand.Parameters.Add("@ID",System.Data.SqlDbType.Int);
    sqledit.SelectCommand.Parameters["@ID"].Value=GetID;
    DataSet dsedit = new DataSet();
    sqledit.Fill(dsedit,"editTable");
    ============================
    此段调用了个Getchose的存储过程,传递了一个ID的参数,返回数据fill到了一个DataTable editTable中,然后你就可以在程序中对返回的数据进行操作了。