本地与全局ado.net对象 [英] Local vs global ado.net objects

查看:100
本文介绍了本地与全局ado.net对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有一个困扰我的问题.我不确定哪种方法是声明sqlcommnad和其他ado.net对象的正确方法.... plz在下面查看我的代码...

Hello all, i have a question that has been bugging me. I''m not sure which way is the right way of declaring the sqlcommnad and other ado.net objects....plz view my code below...

public class DataOperations
    {
        string strConnString =  ConfigurationManager.ConnectionStrings["dbconn"].ToString();
        private static DataOperations singleInstance = null;
        private SqlConnection sqlConn;
        public SqlCommand sqlCmd;
        public SqlDataAdapter sqlAda;

        DataOperations()
        {
            sqlConn=new SqlConnection(strConnString);
            sqlCmd = new SqlCommand();
            sqlCmd.Connection = sqlConn;
            sqlCmd.CommandType = System.Data.CommandType.Text;
            sqlAda = new SqlDataAdapter();

        }



这就是我通常如何声明sqlcommand obj和其他对象的方法..我在如下方法中使用它的实例...



this is how i usuall declare a sqlcommand obj and others....and i use the instance of it in methods as follows...

public bool InsertAlumniDetails(.....some values)

        {
            bool bRetVal = false;
            
            
            string strSqlInsert = @"some sql";

            try
            {
                OpenConnection();
                sqlCmd.Parameters.Clear();
                sqlCmd.Parameters.AddWithValue("name", strName);
                sqlCmd.Parameters.AddWithValue("gender", strGender);
                sqlCmd.CommandText = strSqlInsert;
                sqlCmd.ExecuteNonQuery();



我的问题是我有时会收到错误消息无法使用与其基础RCW分离的COM对象."
如果我使用本地sqlcommand实例,则错误已解决.为什么会这样?...有人可以清楚地解释吗?
预先感谢,
mlimbu



my problem is i sometimes get error "COM object that has been separated from its underlying RCW cannot be used."
The error is solved if i use a local sqlcommand instance...why is it so?...can anyone explain cleary???
Thanks in advance,
mlimbu

推荐答案

很难确定发生了什么.但这听起来像是在实例化一个COM对象,然后尝试在另一个线程中使用它.

这里的部分问题是您应该在本地使用这些对象.实际上,最好使用
It is hard to tell what is happening for sure. But this sounds like you are instantiating a COM object and then trying to use it in another thread.

Part of the problem here is you SHOULD be using those object locally. It is actually best to use those object in a

语句在

 statement.

If you are writing a Data Access Layer the only thing that should be ''global'' is the connection to the database. You don''t want to open and close the connection constantly. You do however only want to use a SqlCommand/SqlDataReader pretty quickly and for a specific purpose, then you want to get rid of (Dispose) it.


谢谢专家快来了快速回复,现在我将在本地实例化对象.我一直认为,在全局范围内声明这些对象会更快,代码更少...但是我想不是那样的...如果我对这个问题有一个很好的解释,那么我一定会发布它.
Thank you expert coming for the swift reply, for now i''ll be instantiating the objects locally. I always thought that declaring these objects globally would be quicker and less codes...but i guess that''s not the case...if i find a good explanation to this problem i have then, i''ll surely post it.


这篇关于本地与全局ado.net对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆