0

I am trying to use a switch statement by accessing data in the DataTable using SqlDataAdapter but not sure how to access the fields using switch statement.

Here is what I have started:

  SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString);
  SqlDataAdapter da = new SqlDataAdapter("SELECT NAME, DEPARTMENT, LOCATION from MyTable WHERE ID =  @ID", con);

  DataTable dt= new DataTable();
  da.SelectCommand.Parameters.AddWithValue("@ID", int.Parse(ID));
  da.Fill(dt);

  DataRow row = dt.Rows[0];

here is where I get lost and I am sure the switch statement below is not the right syntax

switch (dt[0].Location)
   {
       case 1:
       lblStage.Text = dt[0].Location.ToString() + " - It has been completed.";
        break;
   }   
4

2 回答 2

0

你必须Convert.ToInt32(dt[0]["Location"].ToString())在switch语句中做

于 2013-10-27T04:15:41.957 回答
0
DataRow row = dt.Rows[0];
int temp = Convert.ToInt32(row["Location"].ToString());

switch (temp)
{
    case 1:
    lblStage.Text = temp.ToString() + " has been completed";
    break;

    case 2:
    lblStage.Text = temp.ToString() + " has been completed";
    break;

    default:
    // Perform altrenative, default logic
    break;
} 
于 2013-10-27T04:25:31.930 回答