0

我试图填写一个下拉列表,它只给了我一个值。我怎样才能把从数据库中给出的所有值?

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
4

2 回答 2

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++;
            }
        }
于 2013-11-01T04:54:29.927 回答
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();
于 2013-11-01T02:33:25.647 回答