2013年9月12日星期四

OleDbConnection connection problems

I made a Forms application executed by the button
  OleDbConnection conn;
        OleDbDataReader reader;       
       
        /// <summary>
        /// 打开数据连接
        /// </summary>
        /// <param name="sql">数据库连接语句</param>
        public void open(String sql)
        {


            conn = new OleDbConnection(sql);          
            conn.Open();
        }

I mean, this time after the implementation of the software does not need to perform close before closing the connection is closed
Then I run the code behind
   public bool sql(String sql)
        {
            OleDbCommand comm = new OleDbCommand();
            comm.CommandText = sql;
            comm.Connection = conn;            
            reader=comm.ExecuteReader();
            rd = reader.Read();
            reader.Close();
            return rd;

        }


   public void inquire(Object text)
        {
               
            object lockojb = new object();
            //lock锁
            lock (lockojb)
            {
                //去掉条形码ID的条码号
               // da.open(Class1.strconn);
                String t=text.ToString().Substring(1);
                String ID=text.ToString().Substring(0,1);
                String select_one = "select * from tables where 条码号='" +t+ "'";

                    
                if (da.sql(select_one)==false)
                {
//省略了具体的处理过程
}

I created a thread pool to meet certain conditions are executed inquire ();
ThreadPool.QueueUserWorkItem (new WaitCallback (ts.inquire), text);

questions are as follows :
executed via the button in my open () after arguably conn before closing all together in the software 's why when I run the sql () method to the specific implementation of sql statement is displayed "ExecuteReader: Connection ; property has not been initialized " ; I use breakpoints to see conn is null values.
Jiqiu ah thank you
------ Solution --------------------------- -----------------
conn = new OleDbConnection (sql);
connection object should pass the connection string , how to pass sql.
------ For reference only -------------------------------------- -

@ "provider = Microsoft.Jet.OLEDB.4.0; Data Source = C: \ Users \ admin \ Desktop \ saomiaoqiang \ abc.mdb"
I preach the connection string is to write a bit non-standard use sql statement showing links
Why is my conn later it would become null
I did not put him close ah
------ For reference only ---------------------------- -----------
where you call the open method .
------ For reference only -------------------------------------- -

I'm in the UI layer
  private void button5_Click(object sender, EventArgs e)
        {
            cl.openD();
            timer1.Enabled = true;
            timer2.Enabled = true;
            timer3.Enabled = true;
        }

by clicking on the button bar with a BLL layer openD () method
The BLL layer so a corresponding method
 public void openD()
        {
            da.open(Class1.strconn);
        }


da.open is DAL layer conn.open (); Class1.strconn is a static string stored on a database connection statement
------ For reference only ------ ---------------------------------
described under 3 above that I would be no timer control testing of nothing else
------ For reference only -------------------------------- -------
in conn = new OleDbConnection (sql); next breakpoint
single-step execution of your program , look in the end there is no execution to this line,
and performing over conn.Open () is triggered anything unusual .
------ For reference only -------------------------------------- -

this is my conn.Open () and
 public bool sql(String sql)
        {
            OleDbCommand comm = new OleDbCommand();
            comm.CommandText = sql;
            comm.Connection = conn;            
            reader=comm.ExecuteReader();
            rd = reader.Read();
            reader.Close();
            return rd;

        }
have made a break when I press the button code into the conn.open () point to open to see the state of conn is open said it had opened the wrong one I press F5 breakpoint execution to OleDbCommand comm = new OleDbCommand () is what I see on mouse over conn conn is empty null then I think the next to conn = new OleDbConnection (sql); modification as conn.ConnectionString = sql; put new OleDbConnection () placed clss methods below rather than within new instance of his
When I execute the breakpoint again when conn has not empty but his start status is displayed as close of a closed
------ For reference only ------- --------------------------------
means that in the beginning I open method is executed via the button is already conn opened but do not know why when I run to the state it was in when there comm is close to
------ For reference only --------------- ------------------------
OleDbConnection conn;
written
static OleDbConnection conn;
------ For reference only ---------------------------- -----------

Thank problem seems to be gone I just have a question why you need to set it to static stands to reason that there is no need to do so the ah
------ For reference only ------------------------------------ ---

Thank problem seems to be gone I just have a question why you need to set it to static stands to reason that there is no need to do so , ah  

you might new of different objects.

没有评论:

发表评论