积极答复者
如果碰到数据库的编写的代码太长,而一次性又不能把它全写完,有没有什么好的方法,既可以缩短代码,又可以不出现数据库的错误

问题
-
比方说:有一个数据库的查询代码如下:string SearchStr="select ST1.*,ST2.*,DT.DepartID,DT.DepartName,ST.SpecialtyID,ST.SpecialtyName,GT.GradeID,GT.GradeName,CT.ClassID,CT.ClassName,SD.DutyName from DepartTable as DT,SpecialtyTable as ST,GradeTable as GT,ClassTable as CT,StudentDuty as SD ,StudentTable as ST1,SchoolTable as ST2 where ST2.SchoolID =DT.SchoolID and DT.DepartID=ST.DepartID and CT.GradeID =GT.GradeID and ST.SpecialtyID=CT.SpecialtyID and CT.ClassID=ST1.ClassID and ST1.DutyID=SD.DutyID"
而执行完这段代码这后,会将所有符合条件的记录都显示出来;
而我现在用下拉列表框的方式(比如班级CLASS)来限制显示出来的记录:
则代码就应该是if (DropDownList1.SelectedValue.ToString() != "") //DropDownList1是CLASS的ID
{ string id=DropDownList1.SelectedValue.ToString();
SearchStr = "select ST1.*,ST2.*,DT.DepartID,DT.DepartName,ST.SpecialtyID,ST.SpecialtyName,GT.GradeID,GT.GradeName,CT.ClassID,CT.ClassName,SD.DutyName from DepartTable as DT,SpecialtyTable as ST,GradeTable as GT,ClassTable as CT,StudentDuty as SD ,StudentTable as ST1,SchoolTable as ST2 where ST2.SchoolID =DT.SchoolID and DT.DepartID=ST.DepartID and CT.GradeID =GT.GradeID and ST.SpecialtyID=CT.SpecialtyID and CT.ClassID=ST1.ClassID and ST1.DutyID=SD.DutyID where StudentDuty.DutyID=@DutyID" ;
SqlConnection conn = new SqlConnection(connStr); //创建连接对象
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.Parameters.AddWithValue("@DutyID", id);
}
因为我感觉代码过长不利于程序的编写,而且如果DropDownList控件过多的话,代码写起来会非常的麻烦;
我的意思是:请问有没有一种方法可以使SQL语句根据查询条件的不同,可分开来写?我尝试过,好像不行,所以想问问前辈们的意见
答案
-
SchoolTable.SchoolID 字段是什么类型? 如果varchar类型的话 必须加上单引号
SchoolStr="and SchoolTable.SchoolID='"+DropDownList1.SelectedValue.ToString()+"'";- 已标记为答案 Smiling008 2010年2月1日 7:11
-
1 SQL长使用 string s = @""; 的方式,带 @ 的字符后允许换行, 或者使用存储过程。2 如果你的条件多的话,你这样拼接 SQL 可能导致你的代码不直观。可以考虑使用下面的方式。3 关于你问题的错误,可能是 SchoolId 是字符类型,但你在拼接时没有加上单引号,或者你在拼接时没有在各个条件之间加空格。
string commandText = @"SELECT * FROM SchoolTable WHERE (@SchoolId = '' OR SchoolId = @SchoolId) AND (@SchoolId = '' OR SchoolName = @SchoolName)"; //(SchoolId = '' OR SchoolId = @SchoolId) 表示如果参数为空,不判断 SchoolId。即你没有选中的状态。
知识改变命运,奋斗成就人生!- 已标记为答案 Smiling008 2010年2月1日 7:23
全部回复
-
恩,今天做系统时基本上把GridView的几个控件的功能给理顺了,但是关于取消按钮的超链接,感觉掌握得还不是很好,主要是无论是自己写的代码,还是参考别人的代码,输入调试都没变化!我起初的意思的是:点击取消按钮之后,页面会回到原来的编辑 删除页面,而不是停留在刷新和取消的页面,不知我这么说,您是否明白:代码如下
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1; //GridView编辑项的索引等于单击行的索引
GridViewBind();
} -
对于上面的问题,我再解释一下:它的意思这样的(是我今天在参考书上看到的,但是不能运行,是数据库的错误,我不知道错误在哪)
Session["SearchStr"]="select ST1.*,ST2.*,DT.DepartID,DT.DepartName,ST.SpecialtyID,ST.SpecialtyName,GT.GradeID,GT.GradeName,CT.ClassID,CT.ClassName,SD.DutyName from DepartTable as DT,SpecialtyTable as ST,GradeTable as GT,ClassTable as CT,StudentDuty as SD ,StudentTable as ST1,SchoolTable as ST2 where ST2.SchoolID =DT.SchoolID and DT.DepartID=ST.DepartID and CT.GradeID =GT.GradeID and ST.SpecialtyID=CT.SpecialtyID and CT.ClassID=ST1.ClassID and ST1.DutyID=SD.DutyID"我运行了一下,其结果是可以得出的,但我现在想通过DropDownList来限制记录的显示:书上是这样写的:
if (DropDownList1.SelectedValue.ToString() != "") //DropDownList1是CLASS的ID
{
SchoolStr="and SchoolTable.SchoolID="+DropDownList1.SelectedValue.ToString();
Session["SearchStr"]=Session["SearchStr"]+SchoolStr;
}
应该是这地方错了,我以前见过,可想不起来了,所以想问问看 -
恩,今天做系统时基本上把GridView的几个控件的功能给理顺了,但是关于取消按钮的超链接,感觉掌握得还不是很好,主要是无论是自己写的代码,还是参考别人的代码,输入调试都没变化!我起初的意思的是:点击取消按钮之后,页面会回到原来的编辑 删除页面,而不是停留在刷新和取消的页面,不知我这么说,您是否明白:代码如下
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1; //GridView编辑项的索引等于单击行的索引
GridViewBind();
}
你的意思是取消后 保持更新和取消页面? -
SchoolTable.SchoolID 字段是什么类型? 如果varchar类型的话 必须加上单引号
SchoolStr="and SchoolTable.SchoolID='"+DropDownList1.SelectedValue.ToString()+"'";- 已标记为答案 Smiling008 2010年2月1日 7:11
-
1 SQL长使用 string s = @""; 的方式,带 @ 的字符后允许换行, 或者使用存储过程。2 如果你的条件多的话,你这样拼接 SQL 可能导致你的代码不直观。可以考虑使用下面的方式。3 关于你问题的错误,可能是 SchoolId 是字符类型,但你在拼接时没有加上单引号,或者你在拼接时没有在各个条件之间加空格。
string commandText = @"SELECT * FROM SchoolTable WHERE (@SchoolId = '' OR SchoolId = @SchoolId) AND (@SchoolId = '' OR SchoolName = @SchoolName)"; //(SchoolId = '' OR SchoolId = @SchoolId) 表示如果参数为空,不判断 SchoolId。即你没有选中的状态。
知识改变命运,奋斗成就人生!- 已标记为答案 Smiling008 2010年2月1日 7:23
-
恩,今天做系统时基本上把GridView的几个控件的功能给理顺了,但是关于取消按钮的超链接,感觉掌握得还不是很好,主要是无论是自己写的代码,还是参考别人的代码,输入调试都没变化!我起初的意思的是:点击取消按钮之后,页面会回到原来的编辑 删除页面,而不是停留在刷新和取消的页面,不知我这么说,您是否明白:代码如下
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1; //GridView编辑项的索引等于单击行的索引
GridViewBind();
}
你的意思是取消后 保持更新和取消页面? -
1 SQL长使用 string s = @""; 的方式,带 @ 的字符后允许换行, 或者使用存储过程。
2 如果你的条件多的话,你这样拼接 SQL 可能导致你的代码不直观。可以考虑使用下面的方式。3 关于你问题的错误,可能是 SchoolId 是字符类型,但你在拼接时没有加上单引号,或者你在拼接时没有在各个条件之间加空格。string commandText = @"SELECT * FROM SchoolTable WHERE (@SchoolId = '' OR SchoolId = @SchoolId) AND (@SchoolId = '' OR SchoolName = @SchoolName)"; //(SchoolId = '' OR SchoolId = @SchoolId) 表示如果参数为空,不判断 SchoolId。即你没有选中的状态。
知识改变命运,奋斗成就人生!
好的,谢谢您,我今后试试看! -
恩,今天做系统时基本上把GridView的几个控件的功能给理顺了,但是关于取消按钮的超链接,感觉掌握得还不是很好,主要是无论是自己写的代码,还是参考别人的代码,输入调试都没变化!我起初的意思的是:点击取消按钮之后,页面会回到原来的编辑 删除页面,而不是停留在刷新和取消的页面,不知我这么说,您是否明白:代码如下
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1; //GridView编辑项的索引等于单击行的索引
GridViewBind();
}
你的意思是取消后 保持更新和取消页面?
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="cancel">LinkButton</asp:LinkButton>
不用事件 -
SchoolTable.SchoolID 字段是什么类型? 如果varchar类型的话 必须加上单引号
SchoolStr="and SchoolTable.SchoolID='"+DropDownList1.SelectedValue.ToString()+"'";
这样改过,我运行了一下,还是不行,虽然我的SchoolTable.SchoolID 字段是使用Varchar类型;
什么错误?
数据库错误,错误原因:第 1 行: 'SchoolTable' 附近有语法错误;会不会是它——Session["SearchStr"]=Session["SearchStr"]+SchoolStr;造成的错误啊,之前我好像看过有使用“+=”把它们连在一起的,可现在想不起来了! -
SchoolStr="and ST2.SchoolID='"+DropDownList1.SelectedValue.ToString()+"'";
- 已标记为答案 Smiling008 2010年2月1日 8:55
- 取消答案标记 Smiling008 2010年2月1日 8:55
-
SchoolStr="and ST2.SchoolID='"+DropDownList1.SelectedValue.ToString()+"'";
不行,我试过了,我还是把代码给您看吧,因为好多,怕麻烦你,所以,就挑主要的!
else
{
Session["SearchStr"] = "select ST1.*,ST2.*,DT.DepartID,DT.DepartName,ST.SpecialtyID,ST.SpecialtyName,GT.GradeID,GT.GradeName,CT.ClassID,CT.ClassName,SD.DutyName from DepartTable as DT,SpecialtyTable as ST,GradeTable as GT,ClassTable as CT,StudentDuty as SD ,StudentTable as ST1,SchoolTable as ST2 where ST2.SchoolID =DT.SchoolID and DT.DepartID=ST.DepartID and CT.GradeID =GT.GradeID and ST.SpecialtyID=CT.SpecialtyID and CT.ClassID=ST1.ClassID and ST1.DutyID=SD.DutyID";
if (DropDownList1.SelectedValue.ToString() != "")
{
SchoolStr = "and SchoolTable.SchoolID='" + DropDownList1.SelectedValue.ToString()+"'";
Session["SearchStr"] = Session["SearchStr"] + SchoolStr;if (DropDownList2.SelectedValue.ToString() != "")
{
DepartStr = "and DepartTable.DepartID='" + DropDownList2.SelectedValue.ToString()+"'";
Session["SearchStr"] = Session["SearchStr"] + DepartStr;
if (DropDownList3.SelectedValue.ToString() != "")
{
SpecialStr = "and SpecialtyTable.SpecialtyID='" + DropDownList3.SelectedValue.ToString()+"'";
Session["SearchStr"] = Session["SearchStr"] + SpecialStr;
if (DropDownList4.SelectedValue.ToString() != "")
{
GradeStr = "and GradeTable.GradeID='" + DropDownList4.SelectedValue.ToString()+"'";
Session["SearchStr"] = Session["SearchStr"] + GradeStr;
}
if (DropDownList5.SelectedValue.ToString() != "")
{
ClassStr = "and ClassTable.ClassID='" + DropDownList5.SelectedValue.ToString()+"'";
Session["SearchStr"] = Session["SearchStr"] + ClassStr;
}
if (DropDownList6.SelectedValue.ToString() != "")
{
DutyStr = "and StudentDuty.DutyID='" + DropDownList6.SelectedValue.ToString()+"'";
Session["SearchStr"] = Session["SearchStr"] + DutyStr;
}
}
}
}
}
Session["SearchStr"] = "select ST1.*,ST2.*,DT.DepartID,DT.DepartName,ST.SpecialtyID,ST.SpecialtyName,GT.GradeID,GT.GradeName,CT.ClassID,CT.ClassName,SD.DutyName from DepartTable as DT,SpecialtyTable as ST,GradeTable as GT,ClassTable as CT,StudentDuty as SD ,StudentTable as ST1,SchoolTable as ST2 where ST2.SchoolID =DT.SchoolID and DT.DepartID=ST.DepartID and CT.GradeID =GT.GradeID and ST.SpecialtyID=CT.SpecialtyID and CT.ClassID=ST1.ClassID and ST1.DutyID=SD.DutyID";这个是没有错误的,我在SQL查询分析器中已经运行过了!- 已编辑 Smiling008 2010年2月1日 9:03
-
1 SQL长使用 string s = @""; 的方式,带 @ 的字符后允许换行, 或者使用存储过程。
2 如果你的条件多的话,你这样拼接 SQL 可能导致你的代码不直观。可以考虑使用下面的方式。3 关于你问题的错误,可能是 SchoolId 是字符类型,但你在拼接时没有加上单引号,或者你在拼接时没有在各个条件之间加空格。string commandText = @"SELECT * FROM SchoolTable WHERE (@SchoolId = '' OR SchoolId = @SchoolId) AND (@SchoolId = '' OR SchoolName = @SchoolName)"; //(SchoolId = '' OR SchoolId = @SchoolId) 表示如果参数为空,不判断 SchoolId。即你没有选中的状态。
知识改变命运,奋斗成就人生!
昨天您教我的“@”换行的方法我用了一下,觉得很好,但我将它应用到Update语句时遇到了点问题:
string SqlStrUpdate=@"update StudentTable
set StudentName='"+this .tbxStudentName.Text .ToString ().Trim ()+"',StudentPassword='"+this .tbxStudentPassword .Text .ToString ().Trim ()"'"如果我想将StudentPassword='"+this .tbxStudentPassword .Text .ToString ().Trim ()"'";另起一行,我将怎么使用“@” -
你好!不好的习惯应尽早的纠正。你看下面的方式比你现在的方式清晰,更易读。另外你通过拼接可能有被注入的可能。
String SqlStrUpdate = @" UPDATE StudentTable SET StudentName = @StudentName , StudentPassword = @StudentPassword WHERE StudentId = @StudentId"; comm.Parameters.AddWithValue("@StudentName", this.tbxStudentName.Text); // Text 已经是文本咯,就不需要 ToString comm.Parameters.AddWithValue("@StudentPassword", this.tbxStudentPassword.Text); comm.Parameters.AddWithValue("@StudentId", "YourStudentId");
知识改变命运,奋斗成就人生! -
你好!
不好的习惯应尽早的纠正。你看下面的方式比你现在的方式清晰,更易读。另外你通过拼接可能有被注入的可能。String SqlStrUpdate = @" UPDATE StudentTable SET StudentName = @StudentName , StudentPassword = @StudentPassword WHERE StudentId = @StudentId"; comm.Parameters.AddWithValue("@StudentName", this.tbxStudentName.Text); // Text 已经是文本咯,就不需要 ToString comm.Parameters.AddWithValue("@StudentPassword", this.tbxStudentPassword.Text); comm.Parameters.AddWithValue("@StudentId", "YourStudentId");
知识改变命运,奋斗成就人生!
恩,好的,我试试!