我试图填写一个下拉列表,它只给我一个值。如何将数据库中提供的所有值都放在其中?
var con = new SqlConnection("server=(local);Initial Catalog=Test;Integrated Security=True");
try
{
con.Open();
SqlCommand command = new SqlCommand("GetDates", con);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@Dates", SqlDbType.DateTime, 50, ParameterDirection.Output, false, 0, 50, "Dates", DataRowVersion.Default, null));
command.Parameters.Add(new SqlParameter("@Hours", SqlDbType.NChar, 10, ParameterDirection.Output, false, 0, 50, "Hours", DataRowVersion.Default, null));
command.UpdatedRowSource = UpdateRowSource.OutputParameters;
command.ExecuteNonQuery();
DateTime Dates = (DateTime)command.Parameters["@Dates"].Value;
string Hours = (string)command.Parameters["@Hours"].Value;
day.Items.Add(Hours);
}
catch (Exception ex)
{
con.Close();
}
这就是SP
ALTER PROCEDURE GetDates (@Dates datetime OUTPUT, @Hours NCHAR(10) OUTPUT)
AS
SELECT @Dates = Dates, @Hours = Hours from test..Dates where taken = 0
GO
答案 0 :(得分:0)
您可以更改GetDates
存储过程以返回Dates, Hours
,如下所示
ALTER PROCEDURE GetDates ()
AS
SELECT Dates, Hours from test..Dates where taken = 0
GO
然后
con.Open();
SqlCommand command = new SqlCommand("GetDates", con);
command.CommandType = CommandType.StoredProcedure;
DataTable dt = new DataTable();
dt.Load(command.ExecuteReader());
day.DataSource = dt;
day.DataTextField = "Hours";
day.DataValueField = "Dates";
day.DataBind();
答案 1 :(得分:0)
DataSet DropDownListValues = urclassname.GetDates();
DataRow dr;
DropDownList1.Items.Clear();
DropDownList1.Items.Add("");
if (DropDownListValues.Tables[0].Rows.Count > 0)
{
int k = 0;
while (k < DropDownListValues.Tables[0].Rows.Count)
{
dr = DropDownListValues.Tables[0].Rows[k];
string Hours = dr["Hours"].ToString().Trim();
string Dates = dr["Dates"].ToString().Trim();
DropDownList1.Items.Add(new ListItem(Hours , Dates));
k++;
}
}