我的计划表有 15 个字段,使用 ExecuteScalar 似乎只从第一列获取结果,如何指定我想要的列名?例如,当我运行下面的代码时,它将返回 1,这是我的 ID 列中的值,但我想要plan_rate
列。
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CustomerDataConnectionString"].ConnectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM [dbo].[plans] WHERE age_group = @age_group AND coverage_type = @coverage_type", conn);
cmd.Parameters.AddWithValue("@age_group", dd_ageGroup.Text);
cmd.Parameters.AddWithValue("@coverage_type", dd_coverageType.Text);
Object result = cmd.ExecuteScalar();
if (result != null)
Label1.Text = "Plan rate: $" + result.ToString();
else
Label1.Text = "Plan is not available in this state.";
}