public OleDbDataReader ExecuteQueryReturnReader (string sql, string connStr)
{ OleDbConnection conn = new OleDbConnection (); OleDbDataReader reader = null; try
{ conn = new OleDbConnection (connStr); conn.Open ();
OleDbCommand command = conn.CreateCommand (); command.CommandText = sql; reader = command.ExecuteReader (); }
catch
{ }
finally
{ if (conn! = null)
conn.Close (); }
return reader; }
These are the following code in the project was so used:
string DataBase = "HCConfig.mdb"; string strConnection = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" + Application.StartupPath + "/ /" + DataBase; ExecSql con = new ExecSql (); string Sql = "select * form config where ID = 'sss'" ; OleDbDataReader p = con.ExecuteQueryReturnReader (strConnection, Sql); MessageBox.Show (p.FieldCount.ToString ());
But why is there the following error?
Additional information: Object reference not set to an instance of an object.
each execution to MessageBox.Show (p.FieldCount.ToString ()); always appear when this sentence! ! ! !
------ Solution ---------------------------------------- ----
OleDbDataReader p = con.ExecuteQueryReturnReader (Sql, strConnection) ;/ / not pass backwards?
------ Solution ------------------------------------ --------
finally
{
if (conn! = null)
conn.Close ();
}
connection has been closed before returning.
do not have to write this, there are already mature sqlhelper or oledbhelper.
------ Solution ----------------------- ---------------------
Well, just look at your reply to an SQL statement, I am very sure that your instance of OleDbDataReader P is a null your SQL statement error: select * form config where ID = 'sss' in the form wrong! Is from!!! Cause P is null, then you get back its property values is certainly not the object reference to an instance of!
------ For reference only -------------------------------------- -
own Kuangding Yeah, do not sink
------ For reference only ------------------------- --------------
I wrote backwards, but still the error
------ For reference only ------- --------------------------------
removed if (conn! = ; null)
conn.Close (); Nor I tried
------ For reference only -------------------------------- -------
trace debug what chant, watching what is going wrong.
------ For reference only ----------------- ----------------------
Upstairs 2 is to say if you modify the error still being given good, then I can think of is your database query problem! Are you sure you execute SQL returns an instance of OleDbDataReader P is not null?
------ For reference only -------------------------------------- -
Results posted to the sub, the word is misspelled! ! ! ! ! ! !
没有评论:
发表评论