Beispiel #1
0
        public WayDataSet SelectDataSet(string sql, params object[] parames)
        {
            bool needToClose = false;

            if (this.Connection.State != System.Data.ConnectionState.Open)
            {
                needToClose = true;
                this.Connection.Open();
            }
            try
            {
                using (var command = this.CreateCommand(sql, parames))
                {
                    var dataset = new WayDataSet();
                    using (var datareader = command.ExecuteReader())
                    {
                        do
                        {
                            if (datareader.HasRows)
                            {
                                var datatable = new WayDataTable();
                                for (int i = 0; i < datareader.FieldCount; i++)
                                {
                                    string name = datareader.GetName(i);
                                    datatable.Columns.Add(new WayDataColumn(name, datareader.GetFieldType(i).FullName));
                                }
                                dataset.Tables.Add(datatable);
                                while (datareader.Read())
                                {
                                    var row = new WayDataRow();
                                    datatable.Rows.Add(row);

                                    for (int i = 0; i < datareader.FieldCount; i++)
                                    {
                                        string name = datareader.GetName(i);
                                        row[name] = datareader[i];
                                    }
                                }
                            }
                        }while (datareader.NextResult());
                    }
                    return(dataset);
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                if (needToClose)
                {
                    this.Connection.Close();
                }
            }
        }
Beispiel #2
0
        public WayDataTable SelectTable(string sql, params object[] parames)
        {
            bool needToClose = false;

            if (this.Connection.State != System.Data.ConnectionState.Open)
            {
                needToClose = true;
                this.Connection.Open();
            }
            try
            {
                using (var command = this.CreateCommand(sql, parames))
                {
                    var dtable = new WayDataTable();
                    using (var reader = command.ExecuteReader())
                    {
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            string name = reader.GetName(i);
                            dtable.Columns.Add(new WayDataColumn(name, reader.GetFieldType(i).FullName));
                        }

                        while (reader.Read())
                        {
                            var row = new WayDataRow();
                            dtable.Rows.Add(row);
                            for (int i = 0; i < reader.FieldCount; i++)
                            {
                                string name = reader.GetName(i);
                                row[name] = reader[i];
                                if (reader[i] != null)
                                {
                                    string typename = reader.GetFieldType(i).FullName;
                                    if (typename != dtable.Columns[i].DataType)
                                    {
                                        dtable.Columns[i].DataType = typename;
                                    }
                                }
                            }
                        }
                    }
                    return(dtable);
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                if (needToClose)
                {
                    this.Connection.Close();
                }
            }
        }