多条件查询数据库并显示(c#与ASP.NET)
我从文本框里读出多个数据,我要在数据库里面查询是否符合多个数据条件的数据并显示
string loginName = TextBoxLoginName.Text;
string。。。
string。。。。。
。。。
string Title = TextBoxTitle.Text;
这多个条件中随便满足多少条件多可以(即,一个,二个....或全部满足);
我已经实现两个条件的查询并显示了,但是条件一多的话,那我那个代码效率是非常低的,请高手指点我该怎么改进我的代码。谢谢
我写的代码如下:
string loginName = TextBoxLoginName.Text;
string Title = TextBoxTitle.Text;
if (TextBoxTitle.Text == "" && TextBoxLoginName.Text == "")
{
Response.Write("<Script Language=JavaScript>alert('请输入查询条件!');</Script>");
}
else
{
try
{
//连接数据库
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=SERVER;Database=mydata;User Id=sa;Pwd=";
conn.Open();
if (TextBoxTitle.Text != "" && TextBoxLoginName.Text != "")
{
string strSql = "select * from [Topic] where [UserLoginName] =" + SqlStringFormat.GetQuotedString(loginName) + "and Title = " + SqlStringFormat.GetQuotedString(Title);
SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Topic");
GV.DataSource = ds.Tables["Topic"].DefaultView;
GV.DataBind();
conn.Close();
}
else
{
if (TextBoxTitle.Text != "")
{
string strSql = "select * from [Topic] where Title = " + SqlStringFormat.GetQuotedString(Title);
SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Topic");
GV.DataSource = ds.Tables["Topic"].DefaultView;
GV.DataBind();
conn.Close();
}
if (TextBoxLoginName.Text != "")
{
string strSql = "select * from [Topic] where [UserLoginName] =" + SqlStringFormat.GetQuotedString(loginName);
SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Topic");
GV.DataSource = ds.Tables["Topic"].DefaultView;
GV.DataBind();
conn.Close();
}
}
}
catch
{
Response.Write("<FONT STYLE='Z-INDEX:100;LEFT:80;POSITION:absolute;TOP:60;'>该用户或该文章不存在</FONT>");
}
}
}
string loginName = TextBoxLoginName.Text;
string。。。
string。。。。。
。。。
string Title = TextBoxTitle.Text;
这多个条件中随便满足多少条件多可以(即,一个,二个....或全部满足);
我已经实现两个条件的查询并显示了,但是条件一多的话,那我那个代码效率是非常低的,请高手指点我该怎么改进我的代码。谢谢
我写的代码如下:
string loginName = TextBoxLoginName.Text;
string Title = TextBoxTitle.Text;
if (TextBoxTitle.Text == "" && TextBoxLoginName.Text == "")
{
Response.Write("<Script Language=JavaScript>alert('请输入查询条件!');</Script>");
}
else
{
try
{
//连接数据库
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=SERVER;Database=mydata;User Id=sa;Pwd=";
conn.Open();
if (TextBoxTitle.Text != "" && TextBoxLoginName.Text != "")
{
string strSql = "select * from [Topic] where [UserLoginName] =" + SqlStringFormat.GetQuotedString(loginName) + "and Title = " + SqlStringFormat.GetQuotedString(Title);
SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Topic");
GV.DataSource = ds.Tables["Topic"].DefaultView;
GV.DataBind();
conn.Close();
}
else
{
if (TextBoxTitle.Text != "")
{
string strSql = "select * from [Topic] where Title = " + SqlStringFormat.GetQuotedString(Title);
SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Topic");
GV.DataSource = ds.Tables["Topic"].DefaultView;
GV.DataBind();
conn.Close();
}
if (TextBoxLoginName.Text != "")
{
string strSql = "select * from [Topic] where [UserLoginName] =" + SqlStringFormat.GetQuotedString(loginName);
SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Topic");
GV.DataSource = ds.Tables["Topic"].DefaultView;
GV.DataBind();
conn.Close();
}
}
}
catch
{
Response.Write("<FONT STYLE='Z-INDEX:100;LEFT:80;POSITION:absolute;TOP:60;'>该用户或该文章不存在</FONT>");
}
}
}
...全文
相关推荐
asp.net留言板源代码(C#)
用ASP.NET c#做的留言板源代码
C# ASP.NET中连接数据库并使用GridView和DropdownList展示数据
ADO.NET 是一个用于在 .NET 环境下访问数据源的 API,它允许程序直接操作数据库中的数据。ADO.NET 架构由多个关键组件构成,主要包括以下几个模块:数据提供者 (Data Providers):为特定类型的数据库提供与数据源交互所需的功能。每个数据提供者包含四个核心组件:连接对象、命令对象、数据读取器和数据适配器。连接对象 (Connection):用于建立到数据源的物理连接。命令对象 (Command)
ASP.NET and C# 滚动新闻控件设计与实现
本文还有配套的精品资源,点击获取 简介:在ASP.NET和C#环境中,滚动新闻控件是动态显示新闻消息的常见需求。本教程将指导你逐步实现一个这样的控件,涵盖数据访问、业务逻辑、页面设计、C#代码、自定义控件、JavaScript和CSS、响应式设计和性能优化等技术要点。通过实践本教程,你将掌握构建高效且用户友好的新闻滚动控件所需的技能。 1. ASP.NET和C#基础 ...
ASP.NET二进制图片显示(C#)
是讲采用vs2005 + sqlServer2000。访问有名的Northwind数据库中的Employee表。其中Employee表中的photo中保存了员工的信息(image类型)。
ASP.NET中读取excel内容,并显示在界面上
NULL 博文链接:https://niunan.iteye.com/blog/274145



