使用 ADO.net c# 将数据从 Azure 数据库添加到下拉列表
Adding data to dropdown list from Azure database using ADO.net c#
我需要获取从 select 语句中获得的数据并将其放入下拉框中,但我不知道在获得 select 语句后要做什么。这是我当前的代码。我正在使用 c# 和 ADO.net
protected void Possition1_SelectedIndexChanged(object sender, EventArgs e)
{
using (var connection = new QC.SqlConnection("Server=CONNECTIONINFO"))
{
connection.Open();
using (var command = new QC.SqlCommand())
{
command.Connection = connection;
command.CommandType = DT.CommandType.Text;
command.CommandText = @"SELECT * [firstName], [lastName]
FROM [dbo].[Players]
WHERE [position] = 'QB'
ORDER BY [firstName] ASC";
}
}
}
任何帮助将不胜感激。
可以使用 ExecuteReader() 函数执行查询,因此可以将结果放入数据集中以填充下拉列表。
如果它是一个 Asp Mvc doprdown 列表可以以 SelectListItem 的形式作为值放置在下面提到的类似形式的文本中
string constr = ConfigurationManager.ConnectionStrings["Server=CONNECTIONINFO"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * [firstName], [lastName]
FROM [dbo].[Players]
WHERE [position] = 'QB'
ORDER BY [firstName] ASC"))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
ddlPlayers.DataSource = cmd.ExecuteReader();
ddlPlayers.DataTextField = "firstName";
ddlPlayers.DataValueField = "PlayerId";
ddlPlayers.DataBind();
con.Close();
}
}
ddlCustomers.Items.Insert(0, new ListItem("--Select Player--", "0"));
希望对您有所帮助
我需要获取从 select 语句中获得的数据并将其放入下拉框中,但我不知道在获得 select 语句后要做什么。这是我当前的代码。我正在使用 c# 和 ADO.net
protected void Possition1_SelectedIndexChanged(object sender, EventArgs e)
{
using (var connection = new QC.SqlConnection("Server=CONNECTIONINFO"))
{
connection.Open();
using (var command = new QC.SqlCommand())
{
command.Connection = connection;
command.CommandType = DT.CommandType.Text;
command.CommandText = @"SELECT * [firstName], [lastName]
FROM [dbo].[Players]
WHERE [position] = 'QB'
ORDER BY [firstName] ASC";
}
}
}
任何帮助将不胜感激。
可以使用 ExecuteReader() 函数执行查询,因此可以将结果放入数据集中以填充下拉列表。 如果它是一个 Asp Mvc doprdown 列表可以以 SelectListItem 的形式作为值放置在下面提到的类似形式的文本中
string constr = ConfigurationManager.ConnectionStrings["Server=CONNECTIONINFO"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * [firstName], [lastName]
FROM [dbo].[Players]
WHERE [position] = 'QB'
ORDER BY [firstName] ASC"))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
ddlPlayers.DataSource = cmd.ExecuteReader();
ddlPlayers.DataTextField = "firstName";
ddlPlayers.DataValueField = "PlayerId";
ddlPlayers.DataBind();
con.Close();
}
}
ddlCustomers.Items.Insert(0, new ListItem("--Select Player--", "0"));
希望对您有所帮助