求c#高手修改代码,主要实现一个多条件查询功能.我初学,代码错误较多,希望耐心修改
publicpartialclassgroupBox1:Form{publicgroupBox1(){InitializeComponent();}staticstrin...
public partial class groupBox1 : Form
{
public groupBox1()
{
InitializeComponent();
}
static string connectionString = "Data Source=(local);DataBase=biao ;Uid=sa;Pwd=;";
SqlConnection conn = new SqlConnection(connectionString);
SqlDataAdapter Adapter;
DataSet dataSet = new DataSet();
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请填写产品型号");
return;
}
else
{
if (conn.State==ConnectionState.Closed)
conn.Open();
string select_1="Select * from biao where 1=1 and cpxh=";
str+="+textBox1.Text+";
str+=% % ;
SqlConnection conn = new SqlConnection("server=.;database=db_15;uid=sa;pwd=");
Adapter = new SqlDataAdapter(select_1 , conn);
Adapter.Fill(dataSet);
dataGridView1.DataSource = dataset.Tables[0];
conn .Close ();
}
}
private void button3_Click(object sender, EventArgs e)
{
if (comboBox1 == "" && comboBox2 == "" && comboBox3 == "" && comboBox4 == "" && comboBox5 == "")
{ MessageBox.Show ("请选择产品特点再进行搜索")}
if (conn.State == ConnectionState.Closed)
{ conn.Open(); }
string Select_2=Select *from biao where 1=1 and
if (comboBox2 !="")
{
Select_2+="+Sort=comboBox2.Text+";
if (comboBox4!="")
Select_2+ ="+SCN=comboBox4.Text+";
}
if (comboBox1.Text!=="")
Select_2+="+dkhj=comboBox1.Text+";
if (comboBox3.Text!=="")
Select_2+="+yyly=comboBox3.Text+";
if (comboBox5.Text!=="")
Select_2+="+Conn=comboBox5.Text+";
Adapter=new SqlDataAdapter(Select_2 ,conn);
Adapter.Fill(dataSet) ;
dataGridView.dataSource=dataSet .Tables[0];
conn.Close();
}
这个只是最主要的代码,因为贴不下了 展开
{
public groupBox1()
{
InitializeComponent();
}
static string connectionString = "Data Source=(local);DataBase=biao ;Uid=sa;Pwd=;";
SqlConnection conn = new SqlConnection(connectionString);
SqlDataAdapter Adapter;
DataSet dataSet = new DataSet();
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请填写产品型号");
return;
}
else
{
if (conn.State==ConnectionState.Closed)
conn.Open();
string select_1="Select * from biao where 1=1 and cpxh=";
str+="+textBox1.Text+";
str+=% % ;
SqlConnection conn = new SqlConnection("server=.;database=db_15;uid=sa;pwd=");
Adapter = new SqlDataAdapter(select_1 , conn);
Adapter.Fill(dataSet);
dataGridView1.DataSource = dataset.Tables[0];
conn .Close ();
}
}
private void button3_Click(object sender, EventArgs e)
{
if (comboBox1 == "" && comboBox2 == "" && comboBox3 == "" && comboBox4 == "" && comboBox5 == "")
{ MessageBox.Show ("请选择产品特点再进行搜索")}
if (conn.State == ConnectionState.Closed)
{ conn.Open(); }
string Select_2=Select *from biao where 1=1 and
if (comboBox2 !="")
{
Select_2+="+Sort=comboBox2.Text+";
if (comboBox4!="")
Select_2+ ="+SCN=comboBox4.Text+";
}
if (comboBox1.Text!=="")
Select_2+="+dkhj=comboBox1.Text+";
if (comboBox3.Text!=="")
Select_2+="+yyly=comboBox3.Text+";
if (comboBox5.Text!=="")
Select_2+="+Conn=comboBox5.Text+";
Adapter=new SqlDataAdapter(Select_2 ,conn);
Adapter.Fill(dataSet) ;
dataGridView.dataSource=dataSet .Tables[0];
conn.Close();
}
这个只是最主要的代码,因为贴不下了 展开
2个回答
展开全部
首先你的如下语句:
string select_1="Select * from biao where 1=1 and cpxh=";
str+="+textBox1.Text+";
str+=% % ;
不知道你的str是不是select_1这个变量?是你打错了嘛?如果是那么在select里加百分号可以格式化字符串 string select_1=string.format("Select * from biao where 1=1 and cpxh=%{0}%",textBox1.Text); 不过这句sql是有问题的 要用like %%
string select_1="Select * from biao where 1=1 and cpxh=";
str+="+textBox1.Text+";
str+=% % ;
不知道你的str是不是select_1这个变量?是你打错了嘛?如果是那么在select里加百分号可以格式化字符串 string select_1=string.format("Select * from biao where 1=1 and cpxh=%{0}%",textBox1.Text); 不过这句sql是有问题的 要用like %%
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询