还有我的代码:
private SqlCommand button1_Click(object sender, EventArgs e)
{
//数据库连接字符
SqlConnection con = new SqlConnection("server=Sofia;uid='" + _value2 + "';pwd='" + _value2 + "';database=renvecf");
//开启数据连接
con.Open();
//传入数据库的执行语句
SqlCommand com = new SqlCommand("select USER_ID 用户编号, USER_NAME 用户名, PASSWORD 密码, AGE 年龄, SEX 性别 from dbo.T_user "+userid+"", con);
if (this.textBox1.Text.Length > 0)
{
userid += "Where USER_ID='"textBox1.Text.ToString()"'";
}
if (this.textBox2.Text.Length > 0)
{
username+="Where USER_ID='"textBox1.Text.ToString()"'";
}
//DataSet 和 SQL Server之间的桥接器(数据适配器)
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = com;
//和数据库交互(增/删/改)少时用DataSet
DataSet ds = new DataSet();
//调用传入数据库的执行语句(通过适配器填充到DataSet(内存数据库))
da.Fill(ds, "T_emp_view");
//datagripview控件的绑定数据库数据
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "T_emp_view";
}
我如果有N个输入框不可能一个一个区筛选,但是SQL语句后面 循环添,我又不会写, 现在userid那一句的text.Box1 和分号 都报应输入; 的错误 我也没少写啊 userid += "Where USER_ID='"textBox1.Text.ToString()"'"; 怎么改 , 然后 有AND 和没AND的sql语句怎么衔接啊???? 请贴出代码 ~~谢谢,人人有分~
5 个解决方案
#1
string sqlStr = "select USER_ID 用户编号, USER_NAME 用户名, PASSWORD 密码, AGE 年龄, SEX 性别 from dbo.T_user";
List<string> whereList = new List<string>();
if (this.textBox1.Text.Length > 0) whereList.Add("USER_ID='"+textBox1.Text.ToString()+"'");
if (this.textBox2.Text.Length > 0) whereList.Add("USER_ID='"+textBox1.Text.ToString()+"'");
if (whereList.count > 0) sqlStr += string.Join(" and ", whereList.ToArray());
SqlCommand com = new SqlCommand(sqlStr, con);
#2
哥哥,判断输入框是否有值,如果有,就拼接字符串这种做法没错,错的是,你为啥每次拼接都是where,而不是and?然后where部分拼完后最后记得判断下是否成功组织了where条件,因为有可能一个都没输入
#3
1楼最后的拼接少了个where关键字
#4
userid += "Where USER_ID='"textBox1.Text.ToString()"'";
你的代码明显少了2个+号吧 所以才报分号的错
userid += "Where USER_ID='"+textBox1.Text.ToString()+"'";
另外你下面的username也少了2个+号
#5
查询功能作完了,我要做增删改了,吼吼,慢慢爬吧,没学过C#啊
private void button1_Click(object sender, EventArgs e)
{
string userid = textBox1.Text.Trim();
string username = textBox2.Text.Trim();
string userage = textBox3.Text.Trim();
string usersex = textBox4.Text.Trim();
//数据库连接字符
SqlConnection con = new SqlConnection("server=Sofia;uid='" + _value2 + "';pwd='" + _value2 + "';database=renvecf");
//开启数据连接
con.Open();
//原始语句
string strsql = "select USER_ID 用户编号, USER_NAME 用户名, AGE 年龄, SEX 性别 from T_user where ";
if (userid.Length > 0)
{
strsql += "USER_ID like '%" + userid + "%' and ";
}
//没有条件时
else
{
strsql += "USER_ID like '%%' and ";
}
if (username.Length > 0)
{
strsql += "USER_NAME ='" + username + "' and ";
}
if (userage.Length > 0)
{
strsql += "AGE='" + userage + "' and ";
}
if (usersex.Length > 0)
{
strsql += "SEX='" + usersex + "' and ";
}
if(userid=="" && username=="" && userage=="" && usersex =="")
{
//那么不查询
return;
}
//判断只有一个条件时去掉“and”
if (strsql.EndsWith("and "))
{
strsql = strsql.Remove(strsql.LastIndexOf("and"), 4);
}
//传入数据库的执行语句
SqlCommand com = new SqlCommand(strsql, con);
//DataSet 和 SQL Server之间的桥接器(数据适配器)
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = com;
//和数据库交互(增/删/改)少时用DataSet
DataSet ds = new DataSet();
//调用传入数据库的执行语句(通过适配器填充到DataSet(内存数据库))
da.Fill(ds, "T_emp_view");
//datagripview控件的绑定数据库数据
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "T_emp_view";
}
#1
string sqlStr = "select USER_ID 用户编号, USER_NAME 用户名, PASSWORD 密码, AGE 年龄, SEX 性别 from dbo.T_user";
List<string> whereList = new List<string>();
if (this.textBox1.Text.Length > 0) whereList.Add("USER_ID='"+textBox1.Text.ToString()+"'");
if (this.textBox2.Text.Length > 0) whereList.Add("USER_ID='"+textBox1.Text.ToString()+"'");
if (whereList.count > 0) sqlStr += string.Join(" and ", whereList.ToArray());
SqlCommand com = new SqlCommand(sqlStr, con);
#2
哥哥,判断输入框是否有值,如果有,就拼接字符串这种做法没错,错的是,你为啥每次拼接都是where,而不是and?然后where部分拼完后最后记得判断下是否成功组织了where条件,因为有可能一个都没输入
#3
1楼最后的拼接少了个where关键字
#4
userid += "Where USER_ID='"textBox1.Text.ToString()"'";
你的代码明显少了2个+号吧 所以才报分号的错
userid += "Where USER_ID='"+textBox1.Text.ToString()+"'";
另外你下面的username也少了2个+号
#5
查询功能作完了,我要做增删改了,吼吼,慢慢爬吧,没学过C#啊
private void button1_Click(object sender, EventArgs e)
{
string userid = textBox1.Text.Trim();
string username = textBox2.Text.Trim();
string userage = textBox3.Text.Trim();
string usersex = textBox4.Text.Trim();
//数据库连接字符
SqlConnection con = new SqlConnection("server=Sofia;uid='" + _value2 + "';pwd='" + _value2 + "';database=renvecf");
//开启数据连接
con.Open();
//原始语句
string strsql = "select USER_ID 用户编号, USER_NAME 用户名, AGE 年龄, SEX 性别 from T_user where ";
if (userid.Length > 0)
{
strsql += "USER_ID like '%" + userid + "%' and ";
}
//没有条件时
else
{
strsql += "USER_ID like '%%' and ";
}
if (username.Length > 0)
{
strsql += "USER_NAME ='" + username + "' and ";
}
if (userage.Length > 0)
{
strsql += "AGE='" + userage + "' and ";
}
if (usersex.Length > 0)
{
strsql += "SEX='" + usersex + "' and ";
}
if(userid=="" && username=="" && userage=="" && usersex =="")
{
//那么不查询
return;
}
//判断只有一个条件时去掉“and”
if (strsql.EndsWith("and "))
{
strsql = strsql.Remove(strsql.LastIndexOf("and"), 4);
}
//传入数据库的执行语句
SqlCommand com = new SqlCommand(strsql, con);
//DataSet 和 SQL Server之间的桥接器(数据适配器)
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = com;
//和数据库交互(增/删/改)少时用DataSet
DataSet ds = new DataSet();
//调用传入数据库的执行语句(通过适配器填充到DataSet(内存数据库))
da.Fill(ds, "T_emp_view");
//datagripview控件的绑定数据库数据
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "T_emp_view";
}