using Nhgdb;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
namespace SqlSugar
{
///
/// 数据填充器
///
public class NhgdbDataAdapter : IDataAdapter
{
private NhgdbCommand command;
private string sql;
private NhgdbConnection _sqlConnection;
///
/// SqlDataAdapter
///
///
public NhgdbDataAdapter(NhgdbCommand command)
{
this.command = command;
}
public NhgdbDataAdapter()
{
}
///
/// SqlDataAdapter
///
///
///
public NhgdbDataAdapter(string sql, NhgdbConnection _sqlConnection)
{
this.sql = sql;
this._sqlConnection = _sqlConnection;
}
///
/// SelectCommand
///
public NhgdbCommand SelectCommand
{
get
{
if (this.command == null)
{
this.command = new NhgdbCommand(this.sql, this._sqlConnection);
}
return this.command;
}
set
{
this.command = value;
}
}
///
/// Fill
///
///
public void Fill(DataTable dt)
{
if (dt == null)
{
dt = new DataTable();
}
var columns = dt.Columns;
var rows = dt.Rows;
using (NhgdbDataReader dr = command.ExecuteReader())
{
for (int i = 0; i < dr.FieldCount; i++)
{
string name = dr.GetName(i).Trim();
if (!columns.Contains(name))
columns.Add(new DataColumn(name, dr.GetFieldType(i)));
else
{
columns.Add(new DataColumn(name + i, dr.GetFieldType(i)));
}
}
while (dr.Read())
{
DataRow daRow = dt.NewRow();
for (int i = 0; i < columns.Count; i++)
{
daRow[columns[i].ColumnName] = dr.GetValue(i);
}
dt.Rows.Add(daRow);
}
}
dt.AcceptChanges();
}
///
/// Fill
///
///
public void Fill(DataSet ds)
{
if (ds == null)
{
ds = new DataSet();
}
using (NhgdbDataReader dr = command.ExecuteReader())
{
do
{
var dt = new DataTable();
var columns = dt.Columns;
var rows = dt.Rows;
for (int i = 0; i < dr.FieldCount; i++)
{
string name = dr.GetName(i).Trim();
if (!columns.Contains(name))
columns.Add(new DataColumn(name, dr.GetFieldType(i)));
else
{
columns.Add(new DataColumn(name + i, dr.GetFieldType(i)));
}
}
while (dr.Read())
{
DataRow daRow = dt.NewRow();
for (int i = 0; i < columns.Count; i++)
{
daRow[columns[i].ColumnName] = dr.GetValue(i);
}
dt.Rows.Add(daRow);
}
dt.AcceptChanges();
ds.Tables.Add(dt);
} while (dr.NextResult());
}
}
}
}