DataAdapter填充多张表

1.多次填充,适用于所有数据库

        private DataSet BindDataSet()
        {
            DataSet ds = new DataSet();

            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("Data Source=10.119.47.177;Initial Catalog=d;Persist Security Info=True;User ID=sa;Password=sa");
            try
            {
                conn.Open();

                System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter("select top 10 * from bumen", conn);
                adapter.Fill(ds, "bumen");
                adapter.SelectCommand.CommandText = "select top 10 * from hz";
                adapter.Fill(ds, "hz");
                adapter.SelectCommand.CommandText = "select top 10 * from manageuser";
                adapter.Fill(ds, "admin");
            }
            finally
            {
                conn.Close();
            }

            return ds;
        }

2.一次填充,Access数据库不支持。表名称会是:Table,Table1,Table2....或者是指定的XX,XX1,XX2...

        private DataSet BindDataSet1()
        {
            DataSet ds = new DataSet();

            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("Data Source=10.119.47.177;Initial Catalog=d;Persist Security Info=True;User ID=sa;Password=sa");
            try
            {
                conn.Open();

                System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter("select top 10 * from bumen;select top 10 * from hz;select top 10 * from manageuser", conn);
                adapter.Fill(ds, "Table");
            }
            finally
            {
                conn.Close();
            }

            return ds;
        }

你可能感兴趣的:(数据库,Security,table,Access,dataset)