i am getting an "Index was outside the bounds of the array." at dr1[1].ToString().
在dr1[1]. tostring()中,我得到一个“索引超出数组的边界”。
I have table contribution_master with 3 columns: type_id (int), name (varchar(20), amount (float). I want to extract all the amount rows.
表contribution_master包含3列:type_id (int)、名称(varchar(20)、金额(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();
}
3 个解决方案
#1
3
The dr1[x]
value relates to the position of the column it has read from the database.
dr1[x]值与它从数据库中读取的列的位置有关。
The SQL you specify SELECT amount FROM contribution_master
will return the amount
column, however the data in this can only be accessed either using dr1[0]
or dr1["amount"]
.
您指定的从contribution_master中选择金额的SQL将返回金额列,但是其中的数据只能使用dr1[0]或dr1[“金额”]访问。
while (dr1.Read())
will iterate through each row of data in a loop until there is no more. However, if you need to access each row specifically, you might be better off filling the data into a datatable and assigning the values from there.
而(dr1.Read())将在循环中遍历每一行数据,直到没有其他数据为止。但是,如果您需要特定地访问每一行,您最好将数据填充到一个datatable中,并从该处分配值。
For example:
例如:
SqlDataAdapter da = new SqlDataAdapter(SQLCmd1);
DataTable dt = new DataTable();
da.Fill(dt);
employer_epf = Convert.ToDouble(dt.Rows[0][0].ToString());
employer_admin = Convert.ToDouble(dt.Rows[1][0].ToString());
employer_edli = Convert.ToDouble(dt.Rows[2][0].ToString());
employer_admin_edli = Convert.ToDouble(dt.Rows[3][0].ToString());
employer_esi = Convert.ToDouble(dt.Rows[4][0].ToString());
Hopefully this helps, but would it not be easier to have the axis of your table the other way around?
希望这能有所帮助,但如果将表格的轴反过来,会不会更容易呢?
#2
1
You're only returning one field from your query (amount) so there is only one field in the datareader (field 0)
您只从查询(数量)返回一个字段,因此datareader中只有一个字段(字段0)
Each call to datareader.Read()
returns a single row. If you want subsequent rows, call Read()
again.
每个对datareader.Read()的调用都返回一行。如果需要后续行,请再次调用Read()。
So, in your code...
因此,在您的代码…
if (dr1.Read())
{
employer_epf = Convert.ToDouble(dr1[0].ToString());
if (dr1.Read())
{
employer_admin = Convert.ToDouble(dr1[0].ToString());
// etc...
}
}
Or you can populate a DataTable
that will return all the rows at once.
或者您可以填充一个DataTable,它将同时返回所有的行。
#3
0
Change SELECT statement. (You've select amount
column from the table)
改变SELECT语句。(您已经从表中选择了amount列)
SELECT * FROM contribution_master
#1
3
The dr1[x]
value relates to the position of the column it has read from the database.
dr1[x]值与它从数据库中读取的列的位置有关。
The SQL you specify SELECT amount FROM contribution_master
will return the amount
column, however the data in this can only be accessed either using dr1[0]
or dr1["amount"]
.
您指定的从contribution_master中选择金额的SQL将返回金额列,但是其中的数据只能使用dr1[0]或dr1[“金额”]访问。
while (dr1.Read())
will iterate through each row of data in a loop until there is no more. However, if you need to access each row specifically, you might be better off filling the data into a datatable and assigning the values from there.
而(dr1.Read())将在循环中遍历每一行数据,直到没有其他数据为止。但是,如果您需要特定地访问每一行,您最好将数据填充到一个datatable中,并从该处分配值。
For example:
例如:
SqlDataAdapter da = new SqlDataAdapter(SQLCmd1);
DataTable dt = new DataTable();
da.Fill(dt);
employer_epf = Convert.ToDouble(dt.Rows[0][0].ToString());
employer_admin = Convert.ToDouble(dt.Rows[1][0].ToString());
employer_edli = Convert.ToDouble(dt.Rows[2][0].ToString());
employer_admin_edli = Convert.ToDouble(dt.Rows[3][0].ToString());
employer_esi = Convert.ToDouble(dt.Rows[4][0].ToString());
Hopefully this helps, but would it not be easier to have the axis of your table the other way around?
希望这能有所帮助,但如果将表格的轴反过来,会不会更容易呢?
#2
1
You're only returning one field from your query (amount) so there is only one field in the datareader (field 0)
您只从查询(数量)返回一个字段,因此datareader中只有一个字段(字段0)
Each call to datareader.Read()
returns a single row. If you want subsequent rows, call Read()
again.
每个对datareader.Read()的调用都返回一行。如果需要后续行,请再次调用Read()。
So, in your code...
因此,在您的代码…
if (dr1.Read())
{
employer_epf = Convert.ToDouble(dr1[0].ToString());
if (dr1.Read())
{
employer_admin = Convert.ToDouble(dr1[0].ToString());
// etc...
}
}
Or you can populate a DataTable
that will return all the rows at once.
或者您可以填充一个DataTable,它将同时返回所有的行。
#3
0
Change SELECT statement. (You've select amount
column from the table)
改变SELECT语句。(您已经从表中选择了amount列)
SELECT * FROM contribution_master