我尝试使用switch
使用DataTable
访问SqlDataAdapter
中的数据,但不确定如何使用switch语句访问字段。
以下是我的开始:
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];
这里是我迷路的地方,我确信下面的switch语句不是正确的语法
switch (dt[0].Location)
{
case 1:
lblStage.Text = dt[0].Location.ToString() + " - It has been completed.";
break;
}
答案 0 :(得分:0)
您必须在switch语句中执行Convert.ToInt32(dt[0]["Location"].ToString())
答案 1 :(得分: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;
}