使用asp.net动态输入sql查询

发布于 2024-11-07 03:45:08 字数 353 浏览 6 评论 0原文

我有一个超链接,应该从 sql 数据库打印信息。但我不知道如何将该超链接值赋予 sql 查询。

SqlConnection conn = new SqlConnection("Server=ILLUMINATI;" + "Database=DB;Integrated Security= true");
        SqlDataAdapter ADP = new SqlDataAdapter("select * from News where Headlines= au_id", conn);

我想动态获取 au_id 值,单击超链接后任何人都可以帮助我。

就像当我点击标题时我应该得到相应的新闻。

I have a hyperlink which should print information from sql database.But I'm not able to know how to give that hyperlink value to sql query.

SqlConnection conn = new SqlConnection("Server=ILLUMINATI;" + "Database=DB;Integrated Security= true");
        SqlDataAdapter ADP = new SqlDataAdapter("select * from News where Headlines= au_id", conn);

I want to get value au_id dynamically can anybody help me with this after clicking on the hyperlink.

Its like when i click on the headlines i should get the corresponding news.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

—━☆沉默づ 2024-11-14 03:45:08

首先,您应该使用 LinkBut​​ton 而不是 Hyperlink 控件,因为超链接会将页面重定向到指定的 URL。但 LinkBut​​ton 有一个 Click 事件处理程序。单击该按钮即可获取 ID。

您的查询将类似于...

SqlDataAdapter ADP = new SqlDataAdapter("select * from News where Headlines = " + au_id, conn);

但是,如果您使用参数化查询来避免 SQL 注入攻击,那就更好了。

SqlDataAdapter ADP = new SqlDataAdapter("select * from News where Headlines = @au_id", conn);
ADP.SelectCommand.Parameters.Add("@au_id", System.Data.SqlDbType.Int, 4, "au_id");

First of all, you should use a LinkButton instead of a Hyperlink control as hyperlink redirects the page to a specified URL. But the LinkButton has a Click Event handler. On that click you can get the ID.

Your query will be look like...

SqlDataAdapter ADP = new SqlDataAdapter("select * from News where Headlines = " + au_id, conn);

But It would be better if you use a Parameterized query to save yourself from a SQL Injection Attack.

SqlDataAdapter ADP = new SqlDataAdapter("select * from News where Headlines = @au_id", conn);
ADP.SelectCommand.Parameters.Add("@au_id", System.Data.SqlDbType.Int, 4, "au_id");
嘦怹 2024-11-14 03:45:08

我不确定你到底想要什么,但这是一个简单的例子:

using ( command = new SqlCommand( ("select * from News where Headlines=" + au_id ), conn) //send au_id as string variable
{
    DataTable outDT = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        adapter.Fill(outDT); 
        return outDT; // Your DataTable
}

I'm not sure what do you exactly want, but here is simple example :

using ( command = new SqlCommand( ("select * from News where Headlines=" + au_id ), conn) //send au_id as string variable
{
    DataTable outDT = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        adapter.Fill(outDT); 
        return outDT; // Your DataTable
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文