输入字段信息 winform自动进入sql的三张表查询,如果有数据就返回哪张表有信息,没有就不返回。
代码用于学习,希望尽量简单易理解!
输入字段信息不知道什么意思。
查询3个表,不是直接组合sql语句,然后dataadapter对象Fill DataSet就行了。dataset的tables为返回的datatable,顺序和sql先后一样
//如果你的输入字段意思是查询3个表中都存在的字段是否包含输入内容用下面的
string s = "张";//输入的关键字
string cd = " where name like '%"+s+"%'";//查询3个表中都存在name字段
string sql = "select * from table1"+cd+";"
+ "select * from table2" + cd + ";"
+ "select * from table3" + cd + ";";
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=.;uid=sa;pwd=xxxxx;database=xxxx");
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(sql, conn);
System.Data.DataSet ds = new System.Data.DataSet();
da.Fill(ds);
//ds.Tables[0].Rows.Count//table1查询出的数据
//ds.Tables[1].Rows.Count//table2查询出的数据
//ds.Tables[2].Rows.Count//table3查询出的数据