查询的字段有姓名、学号、班级、年龄、语文成绩、数学成绩、外语成绩、操作系统成绩等。
我想在这此字段中输入任何一个,就可以查询出相对应的记录,如同时输入多个字段时,则同时查询出满足条件的相应记录。
要求代码简短,如果在多种字段的组合查询中能用。。。。多谢大家。。。。
8 个解决方案
#1
VB语法我不熟,但是应该差不多:
//BCB:
AnsiString s;
s = "select * from table where 1>0);
if (edtName->Text != "")
s = s + " and Name LIKE '" + edtName->Text + "%'";
if (edtID->Text != "")
s = s + " and ID LIKE '" + edtID->Text + "%'";
...
//BCB:
AnsiString s;
s = "select * from table where 1>0);
if (edtName->Text != "")
s = s + " and Name LIKE '" + edtName->Text + "%'";
if (edtID->Text != "")
s = s + " and ID LIKE '" + edtID->Text + "%'";
...
#2
可看一下*王国荣的书《数据库程序设计》邮电出版社。有你需要的东西。
#3
If CField1 = "" Or Coper1 = "" Or Cvalue = "" Then
MsgBox "查询条件中筛选字段、比较运算、值都必须有值,不能为空!", vbInformation, Ds
Cvalue.SetFocus
Else
If FrmCond.Enabled Then
If CField1.Tag = "2" Then '字符型数据
LsqlWhere = LsqlWhere + IIf(Oand.Value = True, " and ", " or ") + CField1 + Coper1 + "'" + Cvalue + "' "
ElseIf CField1.Tag = "3" Then '日期加单引号
LsqlWhere = LsqlWhere + IIf(Oand.Value = True, " and ", " or ") + "convert(varchar(10)," + CField1 + ",20)" + Coper1 + "'" + Cvalue + "' "
Else
LsqlWhere = LsqlWhere + IIf(Oand.Value = True, " and ", " or ") + CField1 + Coper1 + Cvalue + " "
End If
Else '第一次添加条件
If CField1.Tag = "2" Then
LsqlWhere = LsqlWhere + " where " + CField1 + Coper1 + "'" + Cvalue + "' "
ElseIf CField1.Tag = "3" Then '字符、日期加单引号
LsqlWhere = LsqlWhere + " where " + "convert(varchar(10)," + CField1 + ",20)" + Coper1 + "'" + Cvalue + "' "
Else
LsqlWhere = LsqlWhere + " where " + CField1 + Coper1 + Cvalue
End If
End If
TSql.Text = "select " + Ltop + " * from " + Ldatabase + LsqlWhere + LsqlOrder
If Trim(LsqlWhere) <> "" Then FrmCond.Enabled = True
ClearCond
End If
CField1.Tag中保存数据类型代码
MsgBox "查询条件中筛选字段、比较运算、值都必须有值,不能为空!", vbInformation, Ds
Cvalue.SetFocus
Else
If FrmCond.Enabled Then
If CField1.Tag = "2" Then '字符型数据
LsqlWhere = LsqlWhere + IIf(Oand.Value = True, " and ", " or ") + CField1 + Coper1 + "'" + Cvalue + "' "
ElseIf CField1.Tag = "3" Then '日期加单引号
LsqlWhere = LsqlWhere + IIf(Oand.Value = True, " and ", " or ") + "convert(varchar(10)," + CField1 + ",20)" + Coper1 + "'" + Cvalue + "' "
Else
LsqlWhere = LsqlWhere + IIf(Oand.Value = True, " and ", " or ") + CField1 + Coper1 + Cvalue + " "
End If
Else '第一次添加条件
If CField1.Tag = "2" Then
LsqlWhere = LsqlWhere + " where " + CField1 + Coper1 + "'" + Cvalue + "' "
ElseIf CField1.Tag = "3" Then '字符、日期加单引号
LsqlWhere = LsqlWhere + " where " + "convert(varchar(10)," + CField1 + ",20)" + Coper1 + "'" + Cvalue + "' "
Else
LsqlWhere = LsqlWhere + " where " + CField1 + Coper1 + Cvalue
End If
End If
TSql.Text = "select " + Ltop + " * from " + Ldatabase + LsqlWhere + LsqlOrder
If Trim(LsqlWhere) <> "" Then FrmCond.Enabled = True
ClearCond
End If
CField1.Tag中保存数据类型代码
#4
随风而去你好,多谢你的答案,但这样写如果有几十个字段要求我们查询不是我们就这样写几十种可能吗?这样写代码是不是会写死几个程序员呢?我们是否还应该考虑更精彩的代码呢?
#5
BCB:
AnsiString AddToSQL(AnsiString FieldName, AnsiString FieldValue)
{
if (FieldValue != "")
return " and " + FieldName + " LIKE '" + FieldValue + "%'";
else
return "";
}
主程序:
AnsiString sql;
sql = "select * from table where 1>0)
+ AddToSQL("Name", edtName->Text)
+ AddToSQL("ID", edtID->Text)
+ ...;
AnsiString AddToSQL(AnsiString FieldName, AnsiString FieldValue)
{
if (FieldValue != "")
return " and " + FieldName + " LIKE '" + FieldValue + "%'";
else
return "";
}
主程序:
AnsiString sql;
sql = "select * from table where 1>0)
+ AddToSQL("Name", edtName->Text)
+ AddToSQL("ID", edtID->Text)
+ ...;
#6
在vb中,如果用控件数组的话,就很简单了:
假设你有10个字段,那么你可以通过在窗体上的10个编辑框中输入查询条件来执行查询。首先你放一个编辑框在窗体上,然后用copy、paste的方法创建其余编辑框,这样形成了一个控件数组,分别为text1(0)~text1(10),设置其Tag属性分别为各字段的名称,text属性为空,则代码如下:
Dim i, j As Integer
Dim SqlStr As String
SqlStr = "select * from tableName where "
For i = 0 To 9
If Me.Text1.Item(i).Text <> "" Then
j = j + 1
If j = 1 Then
SqlStr = SqlStr & Me.Text1.Item(i).Tag & "='" & Me.Text1.Item(i).Text & "'"
Else
SqlStr = SqlStr & Me.Text1.Item(i).Tag & "='" & Me.Text1.Item(i).Text & "' and where "
End If
End If
Next i
此段代码会生成一个sql语句,即SqlStr的值,然后随便你用什么方法去执行这个sql语句就行了!
假设你有10个字段,那么你可以通过在窗体上的10个编辑框中输入查询条件来执行查询。首先你放一个编辑框在窗体上,然后用copy、paste的方法创建其余编辑框,这样形成了一个控件数组,分别为text1(0)~text1(10),设置其Tag属性分别为各字段的名称,text属性为空,则代码如下:
Dim i, j As Integer
Dim SqlStr As String
SqlStr = "select * from tableName where "
For i = 0 To 9
If Me.Text1.Item(i).Text <> "" Then
j = j + 1
If j = 1 Then
SqlStr = SqlStr & Me.Text1.Item(i).Tag & "='" & Me.Text1.Item(i).Text & "'"
Else
SqlStr = SqlStr & Me.Text1.Item(i).Tag & "='" & Me.Text1.Item(i).Text & "' and where "
End If
End If
Next i
此段代码会生成一个sql语句,即SqlStr的值,然后随便你用什么方法去执行这个sql语句就行了!
#7
"select * from 表名 where 姓名 like " & "'*" & Text1.Text & "*'" & " and 学号 like " & "'*" & Text2.Text & "*'" & " and 班级 Like " & " '*" & Text3.Text & "*'" & " and 年龄 Like " & " '*" & Text4.Text & "*'" & " and 语文成绩 like " & "'*" & Text5.Text & "*'" & " and 数学成绩 Like " & " '*" & Text6.Text & "*'" & " and 外语成绩 Like " & " '*" & Text7.Text & "*'"
#8
只要这个SQL语句就够了,下面是个例子,具体还要看你的情况改变
"select * from 表名 where 姓名 like " & "'*" & Text1.Text & "*'" & " and 学号 like " & "'*" & Text2.Text & "*'" & " and 班级 Like " & " '*" & Text3.Text & "*'" & " and 年龄 Like " & " '*" & Text4.Text & "*'" & " and 语文成绩 like " & "'*" & Text5.Text & "*'" & " and 数学成绩 Like " & " '*" & Text6.Text & "*'" & " and 外语成绩 Like " & " '*" & Text7.Text & "*'"
"select * from 表名 where 姓名 like " & "'*" & Text1.Text & "*'" & " and 学号 like " & "'*" & Text2.Text & "*'" & " and 班级 Like " & " '*" & Text3.Text & "*'" & " and 年龄 Like " & " '*" & Text4.Text & "*'" & " and 语文成绩 like " & "'*" & Text5.Text & "*'" & " and 数学成绩 Like " & " '*" & Text6.Text & "*'" & " and 外语成绩 Like " & " '*" & Text7.Text & "*'"
#1
VB语法我不熟,但是应该差不多:
//BCB:
AnsiString s;
s = "select * from table where 1>0);
if (edtName->Text != "")
s = s + " and Name LIKE '" + edtName->Text + "%'";
if (edtID->Text != "")
s = s + " and ID LIKE '" + edtID->Text + "%'";
...
//BCB:
AnsiString s;
s = "select * from table where 1>0);
if (edtName->Text != "")
s = s + " and Name LIKE '" + edtName->Text + "%'";
if (edtID->Text != "")
s = s + " and ID LIKE '" + edtID->Text + "%'";
...
#2
可看一下*王国荣的书《数据库程序设计》邮电出版社。有你需要的东西。
#3
If CField1 = "" Or Coper1 = "" Or Cvalue = "" Then
MsgBox "查询条件中筛选字段、比较运算、值都必须有值,不能为空!", vbInformation, Ds
Cvalue.SetFocus
Else
If FrmCond.Enabled Then
If CField1.Tag = "2" Then '字符型数据
LsqlWhere = LsqlWhere + IIf(Oand.Value = True, " and ", " or ") + CField1 + Coper1 + "'" + Cvalue + "' "
ElseIf CField1.Tag = "3" Then '日期加单引号
LsqlWhere = LsqlWhere + IIf(Oand.Value = True, " and ", " or ") + "convert(varchar(10)," + CField1 + ",20)" + Coper1 + "'" + Cvalue + "' "
Else
LsqlWhere = LsqlWhere + IIf(Oand.Value = True, " and ", " or ") + CField1 + Coper1 + Cvalue + " "
End If
Else '第一次添加条件
If CField1.Tag = "2" Then
LsqlWhere = LsqlWhere + " where " + CField1 + Coper1 + "'" + Cvalue + "' "
ElseIf CField1.Tag = "3" Then '字符、日期加单引号
LsqlWhere = LsqlWhere + " where " + "convert(varchar(10)," + CField1 + ",20)" + Coper1 + "'" + Cvalue + "' "
Else
LsqlWhere = LsqlWhere + " where " + CField1 + Coper1 + Cvalue
End If
End If
TSql.Text = "select " + Ltop + " * from " + Ldatabase + LsqlWhere + LsqlOrder
If Trim(LsqlWhere) <> "" Then FrmCond.Enabled = True
ClearCond
End If
CField1.Tag中保存数据类型代码
MsgBox "查询条件中筛选字段、比较运算、值都必须有值,不能为空!", vbInformation, Ds
Cvalue.SetFocus
Else
If FrmCond.Enabled Then
If CField1.Tag = "2" Then '字符型数据
LsqlWhere = LsqlWhere + IIf(Oand.Value = True, " and ", " or ") + CField1 + Coper1 + "'" + Cvalue + "' "
ElseIf CField1.Tag = "3" Then '日期加单引号
LsqlWhere = LsqlWhere + IIf(Oand.Value = True, " and ", " or ") + "convert(varchar(10)," + CField1 + ",20)" + Coper1 + "'" + Cvalue + "' "
Else
LsqlWhere = LsqlWhere + IIf(Oand.Value = True, " and ", " or ") + CField1 + Coper1 + Cvalue + " "
End If
Else '第一次添加条件
If CField1.Tag = "2" Then
LsqlWhere = LsqlWhere + " where " + CField1 + Coper1 + "'" + Cvalue + "' "
ElseIf CField1.Tag = "3" Then '字符、日期加单引号
LsqlWhere = LsqlWhere + " where " + "convert(varchar(10)," + CField1 + ",20)" + Coper1 + "'" + Cvalue + "' "
Else
LsqlWhere = LsqlWhere + " where " + CField1 + Coper1 + Cvalue
End If
End If
TSql.Text = "select " + Ltop + " * from " + Ldatabase + LsqlWhere + LsqlOrder
If Trim(LsqlWhere) <> "" Then FrmCond.Enabled = True
ClearCond
End If
CField1.Tag中保存数据类型代码
#4
随风而去你好,多谢你的答案,但这样写如果有几十个字段要求我们查询不是我们就这样写几十种可能吗?这样写代码是不是会写死几个程序员呢?我们是否还应该考虑更精彩的代码呢?
#5
BCB:
AnsiString AddToSQL(AnsiString FieldName, AnsiString FieldValue)
{
if (FieldValue != "")
return " and " + FieldName + " LIKE '" + FieldValue + "%'";
else
return "";
}
主程序:
AnsiString sql;
sql = "select * from table where 1>0)
+ AddToSQL("Name", edtName->Text)
+ AddToSQL("ID", edtID->Text)
+ ...;
AnsiString AddToSQL(AnsiString FieldName, AnsiString FieldValue)
{
if (FieldValue != "")
return " and " + FieldName + " LIKE '" + FieldValue + "%'";
else
return "";
}
主程序:
AnsiString sql;
sql = "select * from table where 1>0)
+ AddToSQL("Name", edtName->Text)
+ AddToSQL("ID", edtID->Text)
+ ...;
#6
在vb中,如果用控件数组的话,就很简单了:
假设你有10个字段,那么你可以通过在窗体上的10个编辑框中输入查询条件来执行查询。首先你放一个编辑框在窗体上,然后用copy、paste的方法创建其余编辑框,这样形成了一个控件数组,分别为text1(0)~text1(10),设置其Tag属性分别为各字段的名称,text属性为空,则代码如下:
Dim i, j As Integer
Dim SqlStr As String
SqlStr = "select * from tableName where "
For i = 0 To 9
If Me.Text1.Item(i).Text <> "" Then
j = j + 1
If j = 1 Then
SqlStr = SqlStr & Me.Text1.Item(i).Tag & "='" & Me.Text1.Item(i).Text & "'"
Else
SqlStr = SqlStr & Me.Text1.Item(i).Tag & "='" & Me.Text1.Item(i).Text & "' and where "
End If
End If
Next i
此段代码会生成一个sql语句,即SqlStr的值,然后随便你用什么方法去执行这个sql语句就行了!
假设你有10个字段,那么你可以通过在窗体上的10个编辑框中输入查询条件来执行查询。首先你放一个编辑框在窗体上,然后用copy、paste的方法创建其余编辑框,这样形成了一个控件数组,分别为text1(0)~text1(10),设置其Tag属性分别为各字段的名称,text属性为空,则代码如下:
Dim i, j As Integer
Dim SqlStr As String
SqlStr = "select * from tableName where "
For i = 0 To 9
If Me.Text1.Item(i).Text <> "" Then
j = j + 1
If j = 1 Then
SqlStr = SqlStr & Me.Text1.Item(i).Tag & "='" & Me.Text1.Item(i).Text & "'"
Else
SqlStr = SqlStr & Me.Text1.Item(i).Tag & "='" & Me.Text1.Item(i).Text & "' and where "
End If
End If
Next i
此段代码会生成一个sql语句,即SqlStr的值,然后随便你用什么方法去执行这个sql语句就行了!
#7
"select * from 表名 where 姓名 like " & "'*" & Text1.Text & "*'" & " and 学号 like " & "'*" & Text2.Text & "*'" & " and 班级 Like " & " '*" & Text3.Text & "*'" & " and 年龄 Like " & " '*" & Text4.Text & "*'" & " and 语文成绩 like " & "'*" & Text5.Text & "*'" & " and 数学成绩 Like " & " '*" & Text6.Text & "*'" & " and 外语成绩 Like " & " '*" & Text7.Text & "*'"
#8
只要这个SQL语句就够了,下面是个例子,具体还要看你的情况改变
"select * from 表名 where 姓名 like " & "'*" & Text1.Text & "*'" & " and 学号 like " & "'*" & Text2.Text & "*'" & " and 班级 Like " & " '*" & Text3.Text & "*'" & " and 年龄 Like " & " '*" & Text4.Text & "*'" & " and 语文成绩 like " & "'*" & Text5.Text & "*'" & " and 数学成绩 Like " & " '*" & Text6.Text & "*'" & " and 外语成绩 Like " & " '*" & Text7.Text & "*'"
"select * from 表名 where 姓名 like " & "'*" & Text1.Text & "*'" & " and 学号 like " & "'*" & Text2.Text & "*'" & " and 班级 Like " & " '*" & Text3.Text & "*'" & " and 年龄 Like " & " '*" & Text4.Text & "*'" & " and 语文成绩 like " & "'*" & Text5.Text & "*'" & " and 数学成绩 Like " & " '*" & Text6.Text & "*'" & " and 外语成绩 Like " & " '*" & Text7.Text & "*'"