【发布时间】:2012-10-14 15:54:37
【问题描述】:
我正在使用下面的代码从 SQL Server 2005 中创建的数据库中获取页面图块和元描述。我的网站是用 ASP.NET 2.0 和 C# 构建的。
在page_load 我正在执行这段代码:
string query = "select MetaDescription, MetaKeywords, H1Text, Ptitle, H2Text FROM SeoText Where CatalogItemId ='" + this.CurrentEnsemble.ItemId + "'";
SqlConnection myconnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBconnection"].ConnectionString);
SqlCommand SqlCmd = null;
SqlCmd = new SqlCommand(query, myconnection);
SqlDataAdapter ad = new SqlDataAdapter(SqlCmd);
DataTable dt = new DataTable();
ad.Fill(dt);
if (dt.Rows[0]["Ptitle"].ToString() == "")
{
this.Page.Title = this.CurrentEnsemble.Title;
}
else
{
this.Page.Title = this.CurrentEnsemble.Title + " " + dt.Rows[0]["Ptitle"].ToString();
}
HtmlMeta metaDesc = (HtmlMeta)Page.Header.FindControl("metaDesc");
if (dt.Rows[0]["MetaDescription"].ToString() == "")
{
metaDesc.Attributes.Add("Content", this.CurrentEnsemble.Title );
}
else
{
metaDesc.Attributes.Add("Content", dt.Rows[0]["MetaDescription"].ToString());
}
HtmlMeta metaKey = (HtmlMeta)Page.Header.FindControl("metaKey");
if (dt.Rows[0]["MetaKeywords"].ToString() == "")
{
metaKey.Attributes.Add("Content", "site general text");
}
else
{
metaKey.Attributes.Add("Content", dt.Rows[0]["MetaKeywords"].ToString());
}
HtmlGenericControl h1 = (HtmlGenericControl)Master.Master.Master.FindControl("h1top");
if (dt.Rows[0]["H1Text"].ToString() == "")
{
h1.InnerText = "site general text";
}
else
{
h1.InnerText = this.CurrentEnsemble.Title + " " + dt.Rows[0]["H1Text"].ToString();
}
HtmlGenericControl h2 = (HtmlGenericControl)Master.Master.Master.FindControl("h2bottom");
if (dt.Rows[0]["H2Text"].ToString() == "")
{
h2.InnerText = "site general text";
}
else
{
h2.InnerText = this.CurrentEnsemble.Title + " " + dt.Rows[0]["H2Text"].ToString();
}
错误被抛出
ad.Fill(dt)
我不确定我在哪里犯了错误。
感谢并感谢它
【问题讨论】:
-
警告您的代码容易受到 sql 注入攻击。
-
感谢 Daniel 的回复,如果您在谈论 select query where 子句,我知道如果用户在其中输入任何值,它很容易发生 SQL 注入,这个只是在后面的代码中使用并且没有人在其中输入任何内容,它所做的只是从数据库中获取 Ensemble Item Id 之类的产品 id,并根据该表 itemid 检查它。我的主要问题是,它在 sqldatadapter.fill 上引发了超时。我也让 sqldataadapter 处理打开和关闭连接,所以它不像我没有关闭连接,非常有线,因为这是随机发生的。
标签: c# sql-server-2005 asp.net-2.0