在一个Form中我有两个ListBox,一个ComboBox,其中都填充了很多项
我想通过Sql数据库从三个控件中获取他们的选中的值组成条件,从数据库中选择查询
Sql语句我能写,可是它不能实现我所有要求,原因是,ListBox和ComboBox我都是用SelectedItem属性实现的
那样我只能实现的三个控件都选择了值后,才能执行程序
而我想实现的是他们三个的组合选择,他又很多种情况,但是,我用了SelectedItem属性后,若那个控件不选择值的话,就会提示错误,我应该怎么做,才能到达我想要的那种功能
希望高手指点迷经!
我在线等待,急,先谢为快!!!
我想通过Sql数据库从三个控件中获取他们的选中的值组成条件,从数据库中选择查询
Sql语句我能写,可是它不能实现我所有要求,原因是,ListBox和ComboBox我都是用SelectedItem属性实现的
那样我只能实现的三个控件都选择了值后,才能执行程序
而我想实现的是他们三个的组合选择,他又很多种情况,但是,我用了SelectedItem属性后,若那个控件不选择值的话,就会提示错误,我应该怎么做,才能到达我想要的那种功能
希望高手指点迷经!
我在线等待,急,先谢为快!!!
string sql=select * from table;
string sql1=where 1=1;
string sql2=and 1=1;
string sql3=and 1=1;根据你的 控件的值得情况 分别对sql1,sql2,sql3 进行赋值。组合最后的SQL string strSql=str+str1+sr2+str3;
就要提示错误
出错信息:未将对象应用到实例
大哥,你把我的意思理解错了
那么你每个控件都新增一个初始栏 就设个默认值 让它 CHECKED 就好了 啊
怎么个默认的就是你在组合SQL 的时候 自己去取这一栏的值
if (textBox1.Selectedvalue != null)
{
sql+=" and 写条件......"
}
if(....以此类推只不过是先做判断后拼sql串啊)
若你不选择这个ListBox的话,就要提示错误,就必须选择这个ListBox的内容才行,可是我我一定要选择
最后在此重申
我不是不会写sql语句,语句我已设计好了,就跟11楼的雷同,不过他的textBox错了
就是有三个ListView
我可能选择其中一个或者其中两个,或者三个都选
我的问题是,我是用这三个ListView的SelectedItem来得到他们的值的,可是当我只选其中一个或者其中两个的时候就要提示错误,我该怎么修改才能正确
SqlCommand cmd1 = new SqlCommand(); string[] array1 = new string[5];
array1[0] = txt_ch_name.Text.ToString ().Trim (); array1[1]=this .listBox1 .SelectedItem.ToString();
array1[2]=this .listBox1 .SelectedItem.ToString(); array1[3] = txt_ch_singer.Text.ToString ().Trim (); array1[4] = comb_ch_number.SelectedItem.ToString();
string str1 = "";
str1 = query2.query_list(array1); cmd1.CommandText = "select * from song_table where " + str1;
cmd1.Connection = strconn.conn;
SqlDataReader read = cmd1.ExecuteReader();
string[] array = new string[8];
this.listView2.Items.Clear();
while (read.Read())
{
array[0] = read.GetSqlInt32(0).ToString();
array[1] = read.GetSqlInt32(1).ToString();
array[2] = read.GetSqlString(2).ToString();
array[3] = read.GetSqlString(3).ToString();
array[4] = read.GetSqlString(4).ToString();
array[5] = read.GetSqlString(5).ToString();
array[6] = read.GetSqlString(6).ToString();
array[7] = read.GetSqlInt32(7).ToString();
this.listView2.Items.Add(new ListViewItem(array));
}
read.Close();
下面是一个类,我用一个query的query_list方法来实现返回sql语句的
public class query1
{
public string query_list(string []arr)
{
string str = "1=1";
string[] arr1 = new string[5]{"song_name","song_language","song_type","singer","song_wordcount"};
for (int i = 0; i < 5; i++)
{
if (i < 4 && arr[i] != "")
str += " and " + arr1[i] + "='" + arr[i] + "'";
else
{
if (arr[i] != "")
str += " and " + arr1[i] + "=" + arr[i];
}
}
return str;
} }
只有全部都选择的话,才会通过调试!!!但那样功能就太单一了
if(listBox1.SelectedItem!=null){
listSelectedValue= this .listBox1 .SelectedItem.ToString();
}
array1[1]=listSelectedValue;
但是,只有这几个Listview选择了才会有结果显示,不然就什么都没有