如何从数据库中检索特定的列行 [英] How to retrieve specific column row from the database

查看:93
本文介绍了如何从数据库中检索特定的列行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Dropdownlist的数据源是我数据库中的一个列。我想检索在Gridview.please帮助中的下拉列表中选择的列行。 #ASP.NET #c#gridview



我尝试过:



i能够检索完整的列而不是特定的行

The Datasource of my Dropdownlist is one of the Columns in my database.i want to retrieve the column row which is selected in the dropdownlist ...in the Gridview.please help. #ASP.NET #c # gridview

What I have tried:

i am able to retrive the complete colum not thespecific rows

<title>

#Select1
{
width: 197px;
}

<asp:DropDownList ID="DropDownList1" runat="server"

DataSourceID="SqlDataSource1" DataTextField="Album_name"

DataValueField="Album_name">
<asp:listitem>Lamborghini
<asp:listitem>BMW

<asp:SqlDataSource ID="SqlDataSource1" runat="server"

ConnectionString="<%$ ConnectionStrings:iuploadaz %>"

SelectCommand="SELECT [Album_name] FROM [tb_Gallery]">
<asp:ScriptManager ID="ScriptManager1" runat="server">

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:AnimationExtender ID="Button1_AnimationExtender" runat="server"

Enabled="True" TargetControlID="Button1">

<asp:GridView ID="gvsearch" runat="server" AutoGenerateColumns="False"

DataSourceID="SqlDataSource1" Width="305px">
<columns>
<asp:BoundField DataField="Album_name" HeaderText="Album_name"

SortExpression="Album_name" />


<asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource1"

DataTextField="Album_name" DataValueField="Album_name">



代码:


code:

public partial class searchaz : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["iuploadaz"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)
{

// Label3.Text = a.ToString();
}

protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("select * from tb_Gallery", con);
// SqlDataAdapter da = new SqlDataAdapter(cmd);

// DataSet ds = new DataSet();
// da.Fill(ds, "album_name");
// DropDownList1.DataSource = ds;
// DropDownList1.DataTextField = "album_name";
//DropDownList1.DataBind();
SqlDataAdapter da = new SqlDataAdapter("select album_name from tb_Gallery where album_name like '" + DropDownList1.Text + "'", con);
//DataSet ds = new DataSet();
//da.Fill(ds);
//DropDownList1.DataSource = ds;
//DropDownList1.DataTextField = "album_name";
//DropDownList1.DataBind();
//gvsearch.DataSource = ds;
//gvsearch.DataBind();

}
}
}

推荐答案

ConnectionStrings:iuploadaz%>

SelectCommand = SELECT [Album_name] FROM [tb_Gallery] >
< span class =code-keyword>< asp:ScriptManager ID = ScriptManager1 runat = server >

< asp:按钮 ID = Button1 runat = server onclick = Button1_Click 文字 = < span class =code-keyword>按钮 / >
< asp:AnimationExtender ID = Button1_AnimationExtender runat = server

已启用 = True TargetControlID = Button1 >

< asp:GridView ID = gvsearch runat = server AutoGenerateColumns = 错误

DataSourceID = SqlDataSource1 宽度 = 305px >
< >
< asp:BoundField DataField = Album_name HeaderText = Album_name

SortExpression = Album_name / >


< asp:ListBox ID = ListBox1 runat = server DataSourceID = SqlDataSource1

DataTextField = Album_name DataValueField = Album_name >
ConnectionStrings:iuploadaz %>" SelectCommand="SELECT [Album_name] FROM [tb_Gallery]"> <asp:ScriptManager ID="ScriptManager1" runat="server"> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> <asp:AnimationExtender ID="Button1_AnimationExtender" runat="server" Enabled="True" TargetControlID="Button1"> <asp:GridView ID="gvsearch" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Width="305px"> <columns> <asp:BoundField DataField="Album_name" HeaderText="Album_name" SortExpression="Album_name" /> <asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Album_name" DataValueField="Album_name">



代码:


code:

public partial class searchaz : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["iuploadaz"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)
{

// Label3.Text = a.ToString();
}

protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("select * from tb_Gallery", con);
// SqlDataAdapter da = new SqlDataAdapter(cmd);

// DataSet ds = new DataSet();
// da.Fill(ds, "album_name");
// DropDownList1.DataSource = ds;
// DropDownList1.DataTextField = "album_name";
//DropDownList1.DataBind();
SqlDataAdapter da = new SqlDataAdapter("select album_name from tb_Gallery where album_name like '" + DropDownList1.Text + "'", con);
//DataSet ds = new DataSet();
//da.Fill(ds);
//DropDownList1.DataSource = ds;
//DropDownList1.DataTextField = "album_name";
//DropDownList1.DataBind();
//gvsearch.DataSource = ds;
//gvsearch.DataBind();

}
}
}


查询数据库时,它会返回符合查询条件的所有行。因此,您需要一种方法只询问您感兴趣的行或行。



最好的方法是使用IDENTITY [ ^ ]列 - 这被称为选择列表中的值。然后你可以选择那个特定的行并用它做你想做的事。



如果是按类别,你需要以某种方式标记你的数据,每个类别行被声明 - 然后您可以使用该类别作为选择列表的值。



当然,您可以只请求您感兴趣的字段或字段in。
When you query a database, it returns all of the rows that fit your query's criteria. You therefore need a way to ask for only the rows or row that you're interested in.

The best way is to have an IDENTITY [^] column - and that is referred to as the value in your select list. Then you can pick that specific row and do what you want with it.

If it's by category, you need to mark your data in some manner whereby the category of each row is declared - then you can use the category as the value of your select list.

You can then, of course, request only the field or fields you are interested in.


首先,不要将控件中的数据连接到SQL查询。这使您可以使用 SQL注入 - 维基百科 [ ^ ]。正确的方法是使用 SqlParameter Class(System。 Data.SqlClient) [ ^ ]



您的查询结果如何,我不太明白为什么要获取数据你已经在下面的查询中

First of all, do not concatenate data from controls to the SQL query. This leaves you open to SQL injection - Wikipedia[^] . The proper way is to use SqlParameter Class (System.Data.SqlClient)[^]

What comes to your query, I don't quite understand why you want to fetch the data you already have in the query below
SqlDataAdapter da = new SqlDataAdapter("select album_name from tb_Gallery where album_name like '" + DropDownList1.Text + "'", con);



但是如果想要使用LIKE比较,你可能想要某处有一张通配符。使用参数,例如


But if the idea is to use LIKE comparison, you probably want to have a wildcard somewhere. With parameters, something like

SqlDataAdapter da = new SqlDataAdapter("select album_name from tb_Gallery where album_name like @albumname + '%'", con);



有关详细信息,请参阅 LIKE(Transact-SQL)| Microsoft Docs [ ^ ]



ADDITION

------- -

要使用参数,您需要定义它并为其提供值。例如

...


For more information, see LIKE (Transact-SQL) | Microsoft Docs[^]

ADDITION
--------
To use a parameter you need to define it and provide a value for it. For example
...

SqlDataAdapter da = new SqlDataAdapter("select album_name from tb_Gallery where album_name like @albumname + '%'", con);
da.SelectCommand.Parameters.Add(
    "@albumname ", SqlDbType.VarChar, 100).Value = DropDownList1.Text;
....


这篇关于如何从数据库中检索特定的列行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆