使用OleDbDataAdapter.Fill()抛出OleDB异常

时间:2014-05-24 13:31:10

标签: c# ado.net oledb

OleDbConnection conn = new OleDbConnection(OLEDBhelper.ConnectionString);
String SqlStt = "SELECT * FROM Groups where CodeTribe=@Code";
OleDbCommand command = new OleDbCommand(SqlStt, conn);
command.Parameters.AddWithValue("@Code", p);
conn.Open();
OleDbDataAdapter Data = new OleDbDataAdapter(command.CommandText, conn);
conn.Close();
DataSet ds = new DataSet();

Data.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows)
{
     DataGridViewRow NewRow = (DataGridViewRow)dataGridViewGroups.Rows[0].Clone();
     NewRow.Cells[0].Value = row["CodeGroup"].ToString();
     NewRow.Cells[1].Value = row["NameGroup"].ToString();
     dataGridViewGroups.Rows.Add(NewRow);
}

p是一个数字

连接正常,我在

获得OleDB例外
Data.Fill(ds);

我认为我的查询有问题,但我不知道在哪里

1 个答案:

答案 0 :(得分:0)

尝试移动.Close();

OleDbDataAdapter Data = new OleDbDataAdapter(command.CommandText, conn);

DataSet ds = new DataSet();

Data.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows)
{
     DataGridViewRow NewRow = (DataGridViewRow)dataGridViewGroups.Rows[0].Clone();
     NewRow.Cells[0].Value = row["CodeGroup"].ToString();
     NewRow.Cells[1].Value = row["NameGroup"].ToString();
     dataGridViewGroups.Rows.Add(NewRow);
}

conn.Close();//You might be able to add this under the Data.Fill()