public ContractInfo GetContractByNo(string contractNo)
{
ContractInfo contractInfo = new ContractInfo();
OleDbCommand select = new OleDbCommand();
string cmd = "SELECT contracts.*, customers.customerName FROM contracts, customers "
+ "WHERE contracts.customerNo = customers.customerNo and contracts.contractNo = '" + contractNo + "';";
try
{
select.CommandText=cmd;
select.Connection=conn;
conn.Open();
OleDbDataReader DataReader=select.ExecuteReader();
if(DataReader.Read())
{
contractInfo.SetContractNo(DataReader.GetString(0));
contractInfo.SetContractName(DataReader.GetString(2));
contractInfo.SetCustomerNo(DataReader.GetString(3));
contractInfo.SetBookDate(Convert.ToDateTime(DataReader[4]));
contractInfo.SetAddress(DataReader.GetString(5));
contractInfo.SetComType(DataReader.GetString(6));
contractInfo.SetPhone(DataReader.GetString(7));
contractInfo.SetFax(DataReader.GetString(8));
contractInfo.SetAccountNo(DataReader.GetString(9));
contractInfo.SetAccountName(DataReader.GetString(10));
contractInfo.SetBank(DataReader.GetString(11));
contractInfo.SetTaxNo(DataReader.GetString(12));
contractInfo.SetCharge(DataReader.GetString(13));
contractInfo.SetSaleType(Convert.ToDouble(DataReader[14]));
contractInfo.SetAmount(Convert.ToDouble(DataReader[15]));
contractInfo.SetPaymentType(DataReader.GetString(16));
contractInfo.SetPaidAmount(Convert.ToDouble(DataReader[17]));
contractInfo.SetNotPaidAmount(Convert.ToDouble(DataReader[18]));
contractInfo.SetContractStatus(Convert.ToDouble(DataReader[19]));
contractInfo.SetNextPayDate(Convert.ToDateTime(DataReader[20]));
contractInfo.SetProfit(Convert.ToDouble(DataReader[21]));
contractInfo.SetCost(Convert.ToDouble(DataReader[22]));
contractInfo.SetProcessor(DataReader.GetString(23));
contractInfo.SetApproval(DataReader.GetString(24));
contractInfo.SetUpdateDate(Convert.ToDateTime(DataReader[25]));
contractInfo.SetUpdateUser(DataReader.GetString(26));
contractInfo.SetCustomerName(DataReader.GetString(27));
}
conn.Close();
}
catch(Exception e)
{
throw e;
}
finally
{
select.Connection.Dispose();
select.Dispose();
conn.Close();
}
return contractInfo;
}
单步调试的时候总是在执行到DataReader=select.ExecuteReader();时候抛出一个异常:ExecuteReader 需要打开的并且可用的连接。该连接的当前状态是 Open, Fetching。真是不明白是怎么回事了!谢谢告知啊~~
11 个解决方案
select.Connection=conn;
conn.Open();
把这两句对调一下看看
conn.Open();
select.Connection=conn;
public void CreateMyOleDbDataReader(string mySelectQuery,string myConnectionString)
{
OleDbConnection myConnection = new OleDbConnection(myConnectionString);
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
myConnection.Open();
OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
while(myReader.Read())
{
Console.WriteLine(myReader.GetString(0));
}
myReader.Close();
//Implicitly closes the connection because CommandBehavior.CloseConnection was specified.
}
看看msdn上得例子,应该是连接状态得问题
我改写了!依然会出现
“ExecuteReader 需要打开的并且可用的连接。该连接的当前状态是 Open, Fetching”
的异常,我不知道是不是OleDbCommand类型的没有OleDbCommand.Dispose()而导致的,小弟是菜鸟,还希望各位大哥多指教!
呵呵
可不可能是系统自动回收造成的哦?
当你的数据库连接在一段时间不用的时候,系统会认为你的连接已经无用,而回收了呢?
我遇到的更多!
一些静态属性,我明明没有调用用到它,调用的时候就报未引用到实例。
TaskInfo taskInfo = new TaskInfo();
bool result=true;
///////////////////////////////////////////////////////////////////////////
string cmd = "SELECT * "
+ " FROM tasks "
+ " where taskNo = '" + taskNo + "';";
////////////////////////////////////////////////////////////////////////////
///
OleDbCommand select = new OleDbCommand(cmd,conn);
try
{
conn.Open();
OleDbDataReader DataReader = select.ExecuteReader(CommandBehavior.CloseConnection);//在这里添加断点就会抛出异常异常就是“ExecuteReader 需要打开的并且可用的连接。该连接的当前状态是 Open, Fetching”