我得到一个“索引超出了数组的范围。” 在 dr1[1].ToString()。
我有 3 列的表contribution_master:type_id (int)、name (varchar(20)、amount (float)。我想提取所有金额行。
SqlConnection SQLCon1 = new SqlConnection(ConfigurationManager.ConnectionStrings["PayrollConnStr"].ConnectionString.ToString());
SqlCommand SQLCmd1 = new SqlCommand("SELECT amount FROM contribution_master", SQLCon1);
try
{
SQLCon1.Open();
SqlDataReader dr1 = SQLCmd1.ExecuteReader();
while (dr1.Read())
{
employer_epf = Convert.ToDouble(dr1[0].ToString());
employer_admin = Convert.ToDouble(dr1[1].ToString());
employer_edli = Convert.ToDouble(dr1[2].ToString());
employer_admin_edli = Convert.ToDouble(dr1[3].ToString());
employer_esi = Convert.ToDouble(dr1[4].ToString());
}
}
catch (SqlException ex)
{
string errorMessage = "Error ";
errorMessage += ex.Message;
lblWarn.Text = errorMessage;
lblWarn.Visible = true;
}
finally
{
SQLCon1.Close();
}