从 QueryString ASP.NET 中检索 table
Retrieving table from QueryString ASP.NET
我正在创建一个简单的查询,以根据文本框中保存的 ID 获取一行数据。但是,它不会检索信息,也不会出错。
我有一个文本框,其中填充了 URL 中传递的查询字符串参数。这是有效的,并在页面上显示了确切的 ID。
我正在使用它来将其余信息抓取到相关字段中。
C#
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True");
con.Open();
try
{
SqlDataAdapter sda = new SqlDataAdapter("Select Recipe_Name, Recipe_Description, Recipe_Instructions FROM Recipe Where Recipe_ID= @recipeid", con);
sda.SelectCommand.Parameters.Add("@recipeid", SqlDbType.Int).Value = RecipeID.Text;
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
nameTxt.Text = dt.Rows[0][0].ToString();
descriptionTxt.Text = dt.Rows[0][1].ToString();
instructionsTxt.Text = dt.Rows[0][2].ToString();
dt.Clear();
}
catch (Exception ex)
{
}
con.Close();
}
ASP.NET
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<hgroup class="title">
<h1><asp:Label ID="RecipeID" runat="server" ><%=Request.QueryString["id"] %></asp:Label></h1>
<asp:Label ID="nameTxt" runat="server" Text="Name"></asp:Label>
</hgroup>
<table style="width:926px">
<tr>
<td class="auto-style2" > IMAGE </td>
<td >
<asp:Panel ID="descriptionPnl" runat="server" BackColor="White" Height="160px" Width="472px">
<asp:Label ID="descriptionTxt" runat="server" Text="Label"></asp:Label>
</asp:Panel>
</td>
</tr>
</table>
<h6> Step by Step Guide</h6>
<table style="width:900px">
<tr>
<td >
<asp:Panel ID="guidePnl" runat="server" BackColor="White" Height="200px" Width="900px">
<asp:Label ID="instructionsTxt" runat="server" Text="Label"></asp:Label>
</asp:Panel>
</td>
</tr>
</table>
</asp:Content>
谁能帮我解决这个问题?我哪里出错了,我需要添加或更改什么。谢谢。
这不是错误,因为您捕获了所有异常但什么都不做。
此外,您很容易受到 sql 注入该代码的攻击(正如评论中正确指出的那样)。
您应该使用相对路径来定位数据库文件(部署时会中断),并且您应该将类似的配置信息放在 Web.config 文件中。
protected void Page_Load(object sender, EventArgs e)
{
string ID = Request.QueryString["id"];
RecipeID.Text = ID;
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True");
con.Open();
try
{
SqlDataAdapter sda = new SqlDataAdapter("Select Recipe_Name, Recipe_Description, Recipe_Instructions FROM Recipe Where Recipe_ID= @recipeid", con);
sda.SelectCommand.Parameters.Add("@recipeid", SqlDbType.Int).Value = RecipeID.Text;
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
nameTxt.Text = dt.Rows[0][0].ToString();
descriptionTxt.Text = dt.Rows[0][1].ToString();
instructionsTxt.Text = dt.Rows[0][2].ToString();
dt.Clear();
}
catch (Exception ex)
{
}
con.Close();
}
sda.SelectCommand.Parameters.Add("@recipeid", SqlDbType.Int).Value = Request.QueryString["id"];
我正在创建一个简单的查询,以根据文本框中保存的 ID 获取一行数据。但是,它不会检索信息,也不会出错。
我有一个文本框,其中填充了 URL 中传递的查询字符串参数。这是有效的,并在页面上显示了确切的 ID。
我正在使用它来将其余信息抓取到相关字段中。
C#
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True");
con.Open();
try
{
SqlDataAdapter sda = new SqlDataAdapter("Select Recipe_Name, Recipe_Description, Recipe_Instructions FROM Recipe Where Recipe_ID= @recipeid", con);
sda.SelectCommand.Parameters.Add("@recipeid", SqlDbType.Int).Value = RecipeID.Text;
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
nameTxt.Text = dt.Rows[0][0].ToString();
descriptionTxt.Text = dt.Rows[0][1].ToString();
instructionsTxt.Text = dt.Rows[0][2].ToString();
dt.Clear();
}
catch (Exception ex)
{
}
con.Close();
}
ASP.NET
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<hgroup class="title">
<h1><asp:Label ID="RecipeID" runat="server" ><%=Request.QueryString["id"] %></asp:Label></h1>
<asp:Label ID="nameTxt" runat="server" Text="Name"></asp:Label>
</hgroup>
<table style="width:926px">
<tr>
<td class="auto-style2" > IMAGE </td>
<td >
<asp:Panel ID="descriptionPnl" runat="server" BackColor="White" Height="160px" Width="472px">
<asp:Label ID="descriptionTxt" runat="server" Text="Label"></asp:Label>
</asp:Panel>
</td>
</tr>
</table>
<h6> Step by Step Guide</h6>
<table style="width:900px">
<tr>
<td >
<asp:Panel ID="guidePnl" runat="server" BackColor="White" Height="200px" Width="900px">
<asp:Label ID="instructionsTxt" runat="server" Text="Label"></asp:Label>
</asp:Panel>
</td>
</tr>
</table>
</asp:Content>
谁能帮我解决这个问题?我哪里出错了,我需要添加或更改什么。谢谢。
这不是错误,因为您捕获了所有异常但什么都不做。
此外,您很容易受到 sql 注入该代码的攻击(正如评论中正确指出的那样)。
您应该使用相对路径来定位数据库文件(部署时会中断),并且您应该将类似的配置信息放在 Web.config 文件中。
protected void Page_Load(object sender, EventArgs e)
{
string ID = Request.QueryString["id"];
RecipeID.Text = ID;
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True");
con.Open();
try
{
SqlDataAdapter sda = new SqlDataAdapter("Select Recipe_Name, Recipe_Description, Recipe_Instructions FROM Recipe Where Recipe_ID= @recipeid", con);
sda.SelectCommand.Parameters.Add("@recipeid", SqlDbType.Int).Value = RecipeID.Text;
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
nameTxt.Text = dt.Rows[0][0].ToString();
descriptionTxt.Text = dt.Rows[0][1].ToString();
instructionsTxt.Text = dt.Rows[0][2].ToString();
dt.Clear();
}
catch (Exception ex)
{
}
con.Close();
}
sda.SelectCommand.Parameters.Add("@recipeid", SqlDbType.Int).Value = Request.QueryString["id"];