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 是空间的名字(对应表中字段)查询有可能是多条件查询有可能有两个字段查询我想写重载函数,..该怎么写..麻烦各位了...
解决方案 »
- 无边窗体怎么最小化(控件不是直接在窗体上)
- 怎么实现像QQ面板那样在屏幕四周可以自动隐藏功能?
- 请教,如何用javascript获取treeview中TreeNode.Value的值
- C#如何在一张图象上写字并保存?
- 急救:C#--Web点击一个按钮想把指定Panel上的所有TextBox文本框的文本去掉?问题解决马上揭贴!!!
- 超市管理系统
- 我手上有个C/S的程序,频繁切换程序的窗口的话,有时会报未处理异常的错。非常困惑!
- 怎么用C#来写发送电子邮件的程序?
- 请高手解决
- MVC流形式存储图片读取问题!
- 碰到了一个奇怪的问题,向高手请教。
- C#里怎么自动的定时压缩文件(比如隔一天或是几天自动压缩一次)
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;
}
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 的方式
{
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;
}
给分!
不必写重载,可以传两个ArrayList 参数,ArrayList 里添加的项目个数 是自由的。
不过 两个ArrayList 里添加的项目个数要一样,而且 字段值 和字段要对应起来。
private DataSet QueryOnePara(ArrayList Para , ArrayList ControlName)
{
略。
}
2, 也可以传一个Dictionary 类型的参数,Dictionary<key,value>
key 是字段名,value 字段值