网上找到的一个数据基类,有一个带ref参数的方法  /// <summary>
  /// 返回指定Sql语句的SqlDataReader,请注意,在使用后请关闭本对象,同时将自动调用closeConnection()来关闭数据库连接
  /// 方法关闭数据库连接
  /// </summary>
  /// <param name="sqlstr">传入的Sql语句</param>
  /// <param name="dr">传入的ref DataReader 对象</param>
  public static void dataReader(string sqlstr,ref SqlDataReader dr)
  {
   try
   {
    openConnection();
    comm.CommandText =sqlstr;
    comm.CommandType =CommandType.Text ;
    dr=comm.ExecuteReader(CommandBehavior.CloseConnection); 
   }
   catch
   {
    try
    {
     if(dr!=null && !dr.IsClosed)
      dr.Close();
    }
    catch
    {
    }
    finally
    {
     closeConnection();
    }
   }
  }
但不知具体怎么用,比如选取一个表的数据绑定至DataGrid!!!哪位大侠给几行代码或思路,先谢了!

解决方案 »

  1.   

    方法参数上的 ref 方法参数关键字使方法引用传递到方法的同一个变量。当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。若要使用 ref 参数,必须将参数作为 ref 参数显式传递到方法。ref 参数的值被传递到 ref 参数。
      

  2.   

    示例
    // cs_ref.cs
    using System;
    public class MyClass 
    {
       public static void TestRef(ref char i) 
       {
          // The value of i will be changed in the calling method
          i = 'b';
       }   public static void TestNoRef(char i) 
       {
          // The value of i will be unchanged in the calling method
          i = 'c';
       }   // This method passes a variable as a ref parameter; the value of the 
       // variable is changed after control passes back to this method.
       // The same variable is passed as a value parameter; the value of the
       // variable is unchanged after control is passed back to this method.
       public static void Main() 
       {
       
          char i = 'a';    // variable must be initialized
          TestRef(ref i);  // the arg must be passed as ref
          Console.WriteLine(i);
          TestNoRef(i);
          Console.WriteLine(i);
       }
    }
    输出
    b
    b
      

  3.   

    这个方法签名比较有意思
    为什么不直接
    public static SqlDataReader dataReader(string sqlstr)
      

  4.   

    我找的这个基类里写了两个,其中一个就是brightheroes提到的那个,我搞定了,埋单!