none
DataSet1.xsd数据集连接问题 RRS feed

  • 问题

  • 环境描述:我的应用程序为C/S架构,服务器数据库为Server SQL 2008R2版本,我的程序里面实现了简单的三层架构,通过创建类库A来生成数据      集,然后在类库B中对类库A中的数据集方法进行封装,接着在我的程序里面调用类库B中的方法获取数据。

    问题描述:我的应用程序中存在多线程环境,当我在线程A中第一次通过类库B访问数据的时候,是没有问题的,其次我把这个线程结束,其次在主线程中当我再次访问数据的时候,就会产生错误,错误为服务器拒绝了我的连接请求。我上网查资料,是因为在类库A里面的数据集中连接只在初始化被加载一次,因此只要我断开,再次连接就连接不上,需要重启软件(即重新初始化即可),所以我想请问一下这种数据集的连接能通过代码控制吗?即我每次再调用数据集中的方法时都新建连接,用完则关闭。

    2014年4月2日 2:54

答案

  • 这种的话应该不能控制他在整个app生命周期里都是在控件的后台自身代码里控制的,不能改变:
    public virtual int Insert(int Id, string Name) {
                this.Adapter.InsertCommand.Parameters[0].Value = ((int)(Id));
                if ((Name == null)) {
                    this.Adapter.InsertCommand.Parameters[1].Value = global::System.DBNull.Value;
                }
                else {
                    this.Adapter.InsertCommand.Parameters[1].Value = ((string)(Name));
                }
                global::System.Data.ConnectionState previousConnectionState = this.Adapter.InsertCommand.Connection.State;
                if (((this.Adapter.InsertCommand.Connection.State & global::System.Data.ConnectionState.Open) 
                            != global::System.Data.ConnectionState.Open)) {
                    this.Adapter.InsertCommand.Connection.Open();
                }
                try {
                    int returnValue = this.Adapter.InsertCommand.ExecuteNonQuery();
                    return returnValue;
                }
                finally {
                    if ((previousConnectionState == global::System.Data.ConnectionState.Closed)) {
                        this.Adapter.InsertCommand.Connection.Close();
                    }
                }
            }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年4月14日 11:25
    版主

全部回复

  • >>会产生错误,错误为服务器拒绝了我的连接请求

    楼主你的连接时数据库连接吗,如果是DataSet1.xsd数据集, 在我的理解中,我会创建一个适配器与数据库连接好了,这样不管那个现成都可以访问到数据:

    class Program
        {
            static void Main(string[] args)
            {
                ThreadStart threadDelegate = new ThreadStart(Work.DoWork);
                Thread newThread = new Thread(threadDelegate);
                newThread.Start();
    
                ClassLibrary1.DataSet1TableAdapters.OrderTableAdapter OrderTableAdapter = new ClassLibrary1.DataSet1TableAdapters.OrderTableAdapter();
    
                ClassLibrary1.DataSet1.OrderDataTable OT = OrderTableAdapter.GetData();
    
                Console.WriteLine(OT);
            }
        }
    
        class Work
        {
            public static void DoWork()
            {
                ClassLibrary1.DataSet1TableAdapters.OrderTableAdapter OrderTableAdapter = new ClassLibrary1.DataSet1TableAdapters.OrderTableAdapter();
    
                ClassLibrary1.DataSet1.OrderDataTable OT = OrderTableAdapter.GetData();
    
                Console.WriteLine(OT);
            }
          
        }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年4月3日 3:29
    版主
  • 我连接数据库是采用的DataSet1.xsd数据集方式,请问这种方式如何控制连接数据库和断开数据库!感谢你的回答
    2014年4月11日 2:30
  • 这种的话应该不能控制他在整个app生命周期里都是在控件的后台自身代码里控制的,不能改变:
    public virtual int Insert(int Id, string Name) {
                this.Adapter.InsertCommand.Parameters[0].Value = ((int)(Id));
                if ((Name == null)) {
                    this.Adapter.InsertCommand.Parameters[1].Value = global::System.DBNull.Value;
                }
                else {
                    this.Adapter.InsertCommand.Parameters[1].Value = ((string)(Name));
                }
                global::System.Data.ConnectionState previousConnectionState = this.Adapter.InsertCommand.Connection.State;
                if (((this.Adapter.InsertCommand.Connection.State & global::System.Data.ConnectionState.Open) 
                            != global::System.Data.ConnectionState.Open)) {
                    this.Adapter.InsertCommand.Connection.Open();
                }
                try {
                    int returnValue = this.Adapter.InsertCommand.ExecuteNonQuery();
                    return returnValue;
                }
                finally {
                    if ((previousConnectionState == global::System.Data.ConnectionState.Closed)) {
                        this.Adapter.InsertCommand.Connection.Close();
                    }
                }
            }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年4月14日 11:25
    版主