次に例を示します。
class Class1 { private readonly DbProviderFactory factory; private readonly string connectionString; public Class1(DbProviderFactory factory, string connectionString) { this.factory = factory; this.connectionString = connectionString; } public List<object> Method1() { using (var conn = factory.CreateConnection()) using (var command = factory.CreateCommand()) { conn.ConnectionString = connectionString; conn.Open(); command.Connection = conn; command.CommandText = "select * from Table1 where ID = @ID"; var param = factory.CreateParameter(); param.ParameterName = "ID"; param.Value = "ID1"; command.Parameters.Add(param); var reader = command.ExecuteReader(); var result = new List<object>(); while (reader.Read()) { var arr = new object[reader.FieldCount]; reader.GetValues(arr); result.Add(arr); } return result; } }
実際にはSQLもRDBに合わせてコーディングしないといけないので、DbProviderFactoryだけでは不十分と思います。
0 件のコメント:
コメントを投稿