C#のDAO 調べ中
C#のDAOのベースクラス書いているけど、わけわかー。
DataReaderをDataTableにする必要があるらしい。
http://bbs.wankuma.com/index.cgi?mode=al2&namber=15561&KLOG=32
バインド変数はタコ。
コネクションプーリングとトランザクション処理の記述をだおうすればいいのだろう・・・?
using System; using System.Collections.Generic; using System.Text; using System.Data.OleDb; using System.Data; using System.Data.SqlClient; namespace ABCS.DAOs { public class MyDAOBase { private readonly static string conString = @"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=.\\App_Data\\abc.mdb"; private OleDbConnection con_ = null; public class SqlParam { public string paramName_ = null; public OleDbType paramType_ = OleDbType.Empty; public object value_ = null; public SqlParam(string paramName, OleDbType paramType, object val) { this.paramName_ = paramName; this.paramType_ = paramType; this.value_ = val; } } private Listparameters_ = new List (); protected OleDbConnection Con { get { return this.con_; } } protected List Params { get { return this.parameters_; } } public void AddParam(string paramName, OleDbType paramType, object val) { this.Params.Add(new SqlParam(paramName, paramType, val)); } public MyDAOBase() { this.con_ = new OleDbConnection(MyDAOBase.conString); } public MyDAOBase(OleDbConnection poolingCon) { if (poolingCon != null) { this.con_ = poolingCon; } else { throw new ArgumentNullException("MyDAOBase: supplied null for pooing sql connection object."); } } protected void ExecuteNonQuery(string sql, List parameters) { this.con_.Open(); OleDbCommand cmd = new OleDbCommand(sql, this.con_); cmd.Parameters.Clear(); foreach (SqlParam item in parameters) { cmd.Parameters.Add(item.paramName_, item.paramType_).Value = item.value_; } cmd.ExecuteNonQuery(); con_.Close(); } protected void ExecuteNonQuery(string sql) { this.con_.Open(); OleDbCommand cmd = new OleDbCommand(sql, this.con_); cmd.Parameters.Clear(); foreach (SqlParam item in this.parameters_) { cmd.Parameters.Add(item.paramName_, item.paramType_).Value = item.value_; } cmd.ExecuteNonQuery(); con_.Close(); } protected DataTable ExecuteReader(string sql, List parameters) { OleDbDataReader sRead = null; this.con_.Open(); OleDbCommand cmd = new OleDbCommand(sql, this.con_); cmd.Parameters.Clear(); foreach (SqlParam item in parameters) { cmd.Parameters.Add(item.paramName_, item.paramType_).Value = item.value_; } cmd.ExecuteNonQuery(); sRead = cmd.ExecuteReader(); DataTable tbl = this.getDataTableFromDataReader(sRead); con_.Close(); return tbl; } protected DataTable ExecuteReader(string sql) { OleDbDataReader sRead = null; this.con_.Open(); OleDbCommand cmd = new OleDbCommand(sql, this.con_); cmd.Parameters.Clear(); foreach (SqlParam item in this.parameters_) { cmd.Parameters.Add(item.paramName_, item.paramType_).Value = item.value_; } cmd.ExecuteNonQuery(); sRead=cmd.ExecuteReader(); DataTable tbl = this.getDataTableFromDataReader(sRead); con_.Close(); return tbl; } private DataTable getDataTableFromDataReader(OleDbDataReader reader) { DataTable dt = new DataTable(); for (int i = 0; i < reader.GetSchemaTable().Rows.Count; i++) { dt.Columns.Add(reader.GetSchemaTable().Rows[i]["ColumnName"].ToString() ,typeof(string)); } while( reader.Read() ) { DataRow row = dt.NewRow(); for( int j = 0; j < reader.FieldCount; j++) { row[j] = reader.GetValue(j); } dt.Rows.Add(row); } return dt; } } }