public SqlConnection conn = new SqlConnection(sataicrw.getconn());
        public DataSet ds = new DataSet();        //单条件查询
        private DataSet QueryOnePara(string Para , string ControlName)
        {
            string strCmd = "select LoginName , LoginPass , TrueName , Ugroup , Email , WYPXJL , Mobile , Tel from MemberUser where "
                            + ControlName.ToString().Trim()
                            + " = '"
                            + Para.ToString().Trim()
                            + "' and Email is not null";            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(strCmd, conn);
            conn.Close();
            da.Fill(ds, "newtable");
            return ds;
        }上面这个函数,
para是控件里面的值
ControlName 是空间的名字(对应表中字段)查询有可能是多条件查询有可能有两个字段查询我想写重载函数,..该怎么写..麻烦各位了...

解决方案 »

  1.   

    //单条件查询 
    private DataSet QueryOnePara(string Para , string ControlName) 

               string strCmd = "select LoginName , LoginPass , TrueName , Ugroup , Email , WYPXJL , Mobile , Tel from MemberUser where " 
                                + ControlName.ToString().Trim() 
                                + " = '" 
                                + Para.ToString().Trim() 
                                + "' and Email is not null"; 
               return ExectueSQL(strCmd);
    } //两个条件的查询 
    private DataSet QueryOnePara(string Para , string ControlName, string field2) 

               return ExectueSQL(SQL语句);
    } //三个条件的查询 
    private DataSet QueryOnePara(string Para , string ControlName, string field2, string field3) 
    {
               return ExectueSQL(SQL语句);
    } //被调用的查询函数
    private DataSet ExectueSQL(string strCmd)
    {
                conn.Open(); 
                SqlDataAdapter da = new SqlDataAdapter(strCmd, conn); 
                conn.Close(); 
                da.Fill(ds, "newtable"); 
                return ds; 
    }
      

  2.   

    看你的要求不需要用重载using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;namespace WindowsFormsApplication8
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            DataTable DT = QueryWithManyNames(new String[] { "C1", "C2", "C3" }, new String[] { "V1", "V2", "V3" });
            }
            private DataTable QueryWithManyNames(String[] ControlNames, String[] Values)
            {
                using (SqlConnection Connection = new SqlConnection())
                {
                    SqlDataAdapter DataAdapter = new SqlDataAdapter("SELECT LOGINNAME,LOGINPASS,TRUENAME,UGROUP,EMAIL,"
                        + "WYPXJL,MOBILE,TEL FROM MEMBERUSER WHERE 2>1", Connection);
                    for (int i = 0; i < ControlNames.Length; i++)
                    {
                        DataAdapter.SelectCommand.CommandText += " AND (" + ControlNames[i] + "=@P" + i.ToString() + ") ";
                        DataAdapter.SelectCommand.Parameters.AddWithValue("P" + i.ToString(), Values[i]);
                    }
                    DataTable DT = new DataTable();
                    DataAdapter.Fill(DT);
                    return DT;
                }
            }
        }
    }
    你还可以把ControlName和Value 改成 Dictionary 的方式
      

  3.   

    private DataSet QueryOnePara(string Para , string ControlName) 

              string strCmd = "select LoginName , LoginPass , TrueName , Ugroup , Email , WYPXJL , Mobile , Tel from MemberUser where 1=1 " ;
    if(ControlName.ToString().Trim()!=null )
    {
    strCmd+=" and ColumnName like '%"+ControlName.ToString().Trim() 
    "%'";
    }
    ...//(其它如上类推)
                               
              return ExectueSQL(strCmd); 

    //被调用的查询函数 
    private DataSet ExectueSQL(string strCmd) 

                conn.Open(); 
                SqlDataAdapter da = new SqlDataAdapter(strCmd, conn); 
                conn.Close(); 
                da.Fill(ds, "newtable"); 
                return ds; 
    }
    给分!
      

  4.   

    1,
    不必写重载,可以传两个ArrayList 参数,ArrayList 里添加的项目个数 是自由的。
    不过 两个ArrayList 里添加的项目个数要一样,而且 字段值 和字段要对应起来。
    private DataSet QueryOnePara(ArrayList Para , ArrayList ControlName) 
       { 
           略。
       } 
    2, 也可以传一个Dictionary 类型的参数,Dictionary<key,value>
    key 是字段名,value 字段值