C# WINFORM 查询功能请教 本帖最后由 wanghaibo8746 于 2012-12-16 13:27:22 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 一般在设计系统时用到ORM映射关系,其实在UI层将对应的字段放到Model类中这样只要将参数传入数据处理层即可返回执行结果eg: 姓名: txtName 编号:txtId 年龄:txtAgePeopleModel model =new PeopleModel()model.Name =txtName.text;model.Id =txtId.text;model.Age =txtAge.text; 将这个Model传给数据处理层,典型的三层做法。 private void button2_Click(object sender, EventArgs e) { SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=DataBase;User ID=sa;password=sa"); DataTable dt = new DataTable(); //查询语句 string sql = "SELECT * FROM 表名 WHERE " + label1.Text.Trim() + " ='" + textBox1.Text.Trim().ToString() + "'"; connection.Open(); DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(sql, connection); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); dt = ds.Tables[0]; dataGridView1.DataSource = dt; }是这样吗? 数据从哪里来?sql数据库那怎么查数据?sql语句?那前台的条件怎么传入sql?拼sql语句select * from table where id = 前台的textbox.text if(!string.IsNullOrEmpty(textbox1.text.trim())){ sql拼接;}if(!string.IsNullOrEmpty(textbox2.text.trim())){ sql拼接;} vs2008开发的程序依赖性问题 关行tabcontrol隐藏问题,请前辈指点! 求救报表 c#.net可以开发COM+组件吗? 从Excel读取数据的问题。 C#中字符串的问题:会的就请帮我一把了!!!! 求助不同EXCEL文件Sheet数据复制的问题 关于Com interop的问题 C#利用webBrowser怎么去掉网页中不想显示的部分,高手请指教!! 怎么直接传递控件的拥有者信息到函数内部? 游戏开发房间操作 对话框操作
eg: 姓名: txtName 编号:txtId 年龄:txtAge
PeopleModel model =new PeopleModel()
model.Name =txtName.text;
model.Id =txtId.text;
model.Age =txtAge.text;
将这个Model传给数据处理层,典型的三层做法。
{
SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=DataBase;User ID=sa;password=sa");
DataTable dt = new DataTable();
//查询语句
string sql = "SELECT * FROM 表名 WHERE " + label1.Text.Trim() + " ='" + textBox1.Text.Trim().ToString() + "'";
connection.Open();
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(sql, connection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
dt = ds.Tables[0]; dataGridView1.DataSource = dt;
}
是这样吗?
sql数据库
那怎么查数据?
sql语句?
那前台的条件怎么传入sql?
拼sql语句select * from table where id = 前台的textbox.text
if(!string.IsNullOrEmpty(textbox1.text.trim()))
{
sql拼接;
}
if(!string.IsNullOrEmpty(textbox2.text.trim()))
{
sql拼接;
}