日期:2014-05-20  浏览次数:20947 次

不调试没有问题,单步调试反而会抛出异常
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。真是不明白是怎么回事了!谢谢告知啊~~

------解决方案--------------------
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())
{