1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
//------------------------------------------------------------------------------
// <copyright file="OleDbDataAdapter.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
// <owner current="true" primary="false">Microsoft</owner>
//------------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Data.Common;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
namespace System.Data.OleDb {
[
DefaultEvent("RowUpdated"),
ToolboxItem("Microsoft.VSDesigner.Data.VS.OleDbDataAdapterToolboxItem, " + AssemblyRef.MicrosoftVSDesigner),
Designer("Microsoft.VSDesigner.Data.VS.OleDbDataAdapterDesigner, " + AssemblyRef.MicrosoftVSDesigner),
]
public sealed class OleDbDataAdapter : DbDataAdapter, IDbDataAdapter, ICloneable {
static private readonly object EventRowUpdated = new object();
static private readonly object EventRowUpdating = new object();
private OleDbCommand _deleteCommand, _insertCommand, _selectCommand, _updateCommand;
public OleDbDataAdapter() : base() {
GC.SuppressFinalize(this);
}
public OleDbDataAdapter(OleDbCommand selectCommand) : this() {
SelectCommand = selectCommand;
}
public OleDbDataAdapter(string selectCommandText, string selectConnectionString) : this() {
OleDbConnection connection = new OleDbConnection(selectConnectionString);
SelectCommand = new OleDbCommand(selectCommandText, connection);
}
public OleDbDataAdapter(string selectCommandText, OleDbConnection selectConnection) : this() {
SelectCommand = new OleDbCommand(selectCommandText, selectConnection);
}
private OleDbDataAdapter(OleDbDataAdapter from) : base(from) {
GC.SuppressFinalize(this);
}
[
DefaultValue(null),
Editor("Microsoft.VSDesigner.Data.Design.DBCommandEditor, " + AssemblyRef.MicrosoftVSDesigner, "System.Drawing.Design.UITypeEditor, " + AssemblyRef.SystemDrawing),
ResCategoryAttribute(Res.DataCategory_Update),
ResDescriptionAttribute(Res.DbDataAdapter_DeleteCommand),
]
new public OleDbCommand DeleteCommand {
get { return _deleteCommand; }
set { _deleteCommand = value; }
}
IDbCommand IDbDataAdapter.DeleteCommand {
get { return _deleteCommand; }
set { _deleteCommand = (OleDbCommand)value; }
}
[
DefaultValue(null),
Editor("Microsoft.VSDesigner.Data.Design.DBCommandEditor, " + AssemblyRef.MicrosoftVSDesigner, "System.Drawing.Design.UITypeEditor, " + AssemblyRef.SystemDrawing),
ResCategoryAttribute(Res.DataCategory_Update),
ResDescriptionAttribute(Res.DbDataAdapter_InsertCommand),
]
new public OleDbCommand InsertCommand {
get { return _insertCommand; }
set { _insertCommand = value; }
}
IDbCommand IDbDataAdapter.InsertCommand {
get { return _insertCommand; }
set { _insertCommand = (OleDbCommand)value; }
}
[
DefaultValue(null),
Editor("Microsoft.VSDesigner.Data.Design.DBCommandEditor, " + AssemblyRef.MicrosoftVSDesigner, "System.Drawing.Design.UITypeEditor, " + AssemblyRef.SystemDrawing),
ResCategoryAttribute(Res.DataCategory_Fill),
ResDescriptionAttribute(Res.DbDataAdapter_SelectCommand),
]
new public OleDbCommand SelectCommand {
get { return _selectCommand; }
set { _selectCommand = value; }
}
IDbCommand IDbDataAdapter.SelectCommand {
get { return _selectCommand; }
set { _selectCommand = (OleDbCommand)value; }
}
[
DefaultValue(null),
Editor("Microsoft.VSDesigner.Data.Design.DBCommandEditor, " + AssemblyRef.MicrosoftVSDesigner, "System.Drawing.Design.UITypeEditor, " + AssemblyRef.SystemDrawing),
ResCategoryAttribute(Res.DataCategory_Update),
ResDescriptionAttribute(Res.DbDataAdapter_UpdateCommand),
]
new public OleDbCommand UpdateCommand {
get { return _updateCommand; }
set { _updateCommand = value; }
}
IDbCommand IDbDataAdapter.UpdateCommand {
get { return _updateCommand; }
set { _updateCommand = (OleDbCommand)value; }
}
[
ResCategoryAttribute(Res.DataCategory_Update),
ResDescriptionAttribute(Res.DbDataAdapter_RowUpdated),
]
public event OleDbRowUpdatedEventHandler RowUpdated {
add { Events.AddHandler(EventRowUpdated, value); }
remove { Events.RemoveHandler(EventRowUpdated, value); }
}
[
ResCategoryAttribute(Res.DataCategory_Update),
ResDescriptionAttribute(Res.DbDataAdapter_RowUpdating),
]
public event OleDbRowUpdatingEventHandler RowUpdating {
add {
OleDbRowUpdatingEventHandler handler = (OleDbRowUpdatingEventHandler) Events[EventRowUpdating];
// MDAC 58177, 64513
// prevent someone from registering two different command builders on the adapter by
// silently removing the old one
if ((null != handler) && (value.Target is DbCommandBuilder)) {
OleDbRowUpdatingEventHandler d = (OleDbRowUpdatingEventHandler) ADP.FindBuilder(handler);
if (null != d) {
Events.RemoveHandler(EventRowUpdating, d);
}
}
Events.AddHandler(EventRowUpdating, value);
}
remove { Events.RemoveHandler(EventRowUpdating, value); }
}
object ICloneable.Clone() {
return new OleDbDataAdapter(this);
}
override protected RowUpdatedEventArgs CreateRowUpdatedEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping) {
return new OleDbRowUpdatedEventArgs(dataRow, command, statementType, tableMapping);
}
override protected RowUpdatingEventArgs CreateRowUpdatingEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping) {
return new OleDbRowUpdatingEventArgs(dataRow, command, statementType, tableMapping);
}
internal static void FillDataTable(OleDbDataReader dataReader, params DataTable[] dataTables) {
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.Fill(dataTables, dataReader, 0, 0);
}
public int Fill(DataTable dataTable, object ADODBRecordSet) {
System.Security.PermissionSet permissionSet = new System.Security.PermissionSet(System.Security.Permissions.PermissionState.None);
permissionSet.AddPermission(OleDbConnection.ExecutePermission); // MDAC 77737
permissionSet.AddPermission(new System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode));
permissionSet.Demand();
IntPtr hscp;
Bid.ScopeEnter(out hscp, "<oledb.OleDbDataAdapter.Fill|API> %d#, dataTable, ADODBRecordSet\n", ObjectID);
try {
if (null == dataTable) {
throw ADP.ArgumentNull("dataTable");
}
if (null == ADODBRecordSet) {
throw ADP.ArgumentNull("adodb");
}
// user was required to have UnmanagedCode Permission just to create ADODB
// (new SecurityPermission(SecurityPermissionFlag.UnmanagedCode)).Demand();
return FillFromADODB((object)dataTable, ADODBRecordSet, null, false); // MDAC 59249
}
finally {
Bid.ScopeLeave(ref hscp);
}
}
public int Fill(DataSet dataSet, object ADODBRecordSet, string srcTable) {
System.Security.PermissionSet permissionSet = new System.Security.PermissionSet(System.Security.Permissions.PermissionState.None);
permissionSet.AddPermission(OleDbConnection.ExecutePermission); // MDAC 77737
permissionSet.AddPermission(new System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode));
permissionSet.Demand();
IntPtr hscp;
Bid.ScopeEnter(out hscp, "<oledb.OleDbDataAdapter.Fill|API> %d#, dataSet, ADODBRecordSet, srcTable='%ls'\n", ObjectID, srcTable);
try {
if (null == dataSet) {
throw ADP.ArgumentNull("dataSet");
}
if (null == ADODBRecordSet) {
throw ADP.ArgumentNull("adodb");
}
if (ADP.IsEmpty(srcTable)) {
throw ADP.FillRequiresSourceTableName("srcTable");
}
// user was required to have UnmanagedCode Permission just to create ADODB
// (new SecurityPermission(SecurityPermissionFlag.UnmanagedCode)).Demand();
return FillFromADODB((object)dataSet, ADODBRecordSet, srcTable, true);
}
finally {
Bid.ScopeLeave(ref hscp);
}
}
private int FillFromADODB(Object data, object adodb, string srcTable, bool multipleResults) {
Debug.Assert(null != data, "FillFromADODB: null data object");
Debug.Assert(null != adodb, "FillFromADODB: null ADODB");
Debug.Assert(!(adodb is DataTable), "call Fill( (DataTable) value)");
Debug.Assert(!(adodb is DataSet), "call Fill( (DataSet) value)");
/*
IntPtr adodbptr = ADP.PtrZero;
try { // generate a new COM Callable Wrapper around the user object so they can't ReleaseComObject on us.
adodbptr = Marshal.GetIUnknownForObject(adodb);
adodb = System.Runtime.Remoting.Services.EnterpriseServicesHelper.WrapIUnknownWithComObject(adodbptr);
}
finally {
if (ADP.PtrZero != adodbptr) {
Marshal.Release(adodbptr);
}
}
*/
bool closeRecordset = multipleResults; // MDAC 60332, 66668
Bid.Trace("<oledb.IUnknown.QueryInterface|API|OLEDB|ADODB> ADORecordsetConstruction\n");
UnsafeNativeMethods.ADORecordsetConstruction recordset = (adodb as UnsafeNativeMethods.ADORecordsetConstruction);
UnsafeNativeMethods.ADORecordConstruction record = null;
if (null != recordset) { // MDAC 78415
if (multipleResults) {
// The NextRecordset method is not available on a disconnected Recordset object, where ActiveConnection has been set to NULL
object activeConnection;
Bid.Trace("<oledb.Recordset15.get_ActiveConnection|API|ADODB>\n");
activeConnection = ((UnsafeNativeMethods.Recordset15)adodb).get_ActiveConnection();
if (null == activeConnection) {
multipleResults = false;
}
}
}
else {
Bid.Trace("<oledb.IUnknown.QueryInterface|API|OLEDB|ADODB> ADORecordConstruction\n");
record = (adodb as UnsafeNativeMethods.ADORecordConstruction);
if (null != record) { // MDAC 78415
multipleResults = false; // IRow implies CommandBehavior.SingleRow which implies CommandBehavior.SingleResult
}
}
// else throw ODB.Fill_NotADODB("adodb"); /* throw later, less code here*/
int results = 0;
if (null != recordset) {
int resultCount = 0;
bool incrementResultCount; // MDAC 59632
object[] value = new object[1];
do {
string tmp = null;
if (data is DataSet) {
tmp = GetSourceTableName(srcTable, resultCount);
}
results += FillFromRecordset(data, recordset, tmp, out incrementResultCount);
if (multipleResults) {
value[0] = DBNull.Value;
object recordsAffected;
object nextresult;
Bid.Trace("<oledb.Recordset15.NextRecordset|API|ADODB>\n");
OleDbHResult hr = ((UnsafeNativeMethods.Recordset15)adodb).NextRecordset(out recordsAffected, out nextresult); // MDAC 78415
Bid.Trace("<oledb.Recordset15.NextRecordset|API|ADODB|RET> %08X{HRESULT}\n", hr);
if (0 > hr) {
// Current provider does not support returning multiple recordsets from a single execution.
if (ODB.ADODB_NextResultError != (int)hr) {
UnsafeNativeMethods.IErrorInfo errorInfo = null;
UnsafeNativeMethods.GetErrorInfo(0, out errorInfo);
string message = String.Empty;
if (null != errorInfo) {
OleDbHResult hresult = ODB.GetErrorDescription(errorInfo, hr, out message);
}
throw new COMException(message, (int)hr);
}
break;
}
adodb = nextresult;
if (null != adodb) {
Bid.Trace("<oledb.IUnknown.QueryInterface|API|OLEDB|ADODB> ADORecordsetConstruction\n");
recordset = (UnsafeNativeMethods.ADORecordsetConstruction) adodb;
if (incrementResultCount) {
resultCount++;
}
continue;
}
}
break;
} while(null != recordset);
if ((null != recordset) && (closeRecordset || (null == adodb))) { // MDAC 59746, 60902
FillClose(true, recordset);
}
}
else if (null != record) {
results = FillFromRecord(data, record, srcTable);
if (closeRecordset) { // MDAC 66668
FillClose(false, record); // MDAC 60848
}
}
else {
throw ODB.Fill_NotADODB("adodb");
}
return results;
}
//override protected int Fill(DataTable dataTable, IDataReader dataReader) { // MDAC 65506
// return base.Fill(dataTable, dataReader);
//}
private int FillFromRecordset(Object data, UnsafeNativeMethods.ADORecordsetConstruction recordset, string srcTable, out bool incrementResultCount) {
incrementResultCount = false;
IntPtr chapter; /*ODB.DB_NULL_HCHAPTER*/
object result = null;
try {
Bid.Trace("<oledb.ADORecordsetConstruction.get_Rowset|API|ADODB>\n");
result = recordset.get_Rowset(); // MDAC 83342
Bid.Trace("<oledb.ADORecordsetConstruction.get_Rowset|API|ADODB|RET> %08X{HRESULT}\n", 0);
Bid.Trace("<oledb.ADORecordsetConstruction.get_Chapter|API|ADODB>\n");
chapter = recordset.get_Chapter(); // MDAC 83342
Bid.Trace("<oledb.ADORecordsetConstruction.get_Chapter|API|ADODB|RET> %08X{HRESULT}\n", 0);
}
catch (Exception e) {
//
if (!ADP.IsCatchableExceptionType(e)) {
throw;
}
throw ODB.Fill_EmptyRecordSet("ADODBRecordSet", e);
}
if (null != result) {
CommandBehavior behavior = (MissingSchemaAction.AddWithKey != MissingSchemaAction) ? 0 : CommandBehavior.KeyInfo;
behavior |= CommandBehavior.SequentialAccess;
OleDbDataReader dataReader = null;
try {
// intialized with chapter only since we don't want ReleaseChapter called for this chapter handle
ChapterHandle chapterHandle = ChapterHandle.CreateChapterHandle(chapter);
dataReader = new OleDbDataReader(null, null, 0, behavior);
dataReader.InitializeIRowset(result, chapterHandle, ADP.RecordsUnaffected);
dataReader.BuildMetaInfo();
incrementResultCount = (0 < dataReader.FieldCount); // MDAC 59632
if (incrementResultCount) {
if (data is DataTable) {
return base.Fill((DataTable) data, dataReader); // MDAC 65506
}
else {
return base.Fill((DataSet) data, srcTable, dataReader, 0, 0);
}
}
}
finally {
if (null != dataReader) {
dataReader.Close();
}
}
}
return 0;
}
private int FillFromRecord(Object data, UnsafeNativeMethods.ADORecordConstruction record, string srcTable) {
object result = null;
try {
Bid.Trace("<oledb.ADORecordConstruction.get_Row|API|ADODB>\n");
result = record.get_Row(); // MDAC 83342
Bid.Trace("<oledb.ADORecordConstruction.get_Row|API|ADODB|RET> %08X{HRESULT}\n", 0);
}
catch(Exception e) {
//
if (!ADP.IsCatchableExceptionType(e)) {
throw;
}
throw ODB.Fill_EmptyRecord("adodb", e);
}
if (null != result) {
CommandBehavior behavior = (MissingSchemaAction.AddWithKey != MissingSchemaAction) ? 0 : CommandBehavior.KeyInfo;
behavior |= CommandBehavior.SequentialAccess | CommandBehavior.SingleRow;
OleDbDataReader dataReader = null;
try {
dataReader = new OleDbDataReader(null, null, 0, behavior);
dataReader.InitializeIRow(result, ADP.RecordsUnaffected);
dataReader.BuildMetaInfo();
if (data is DataTable) {
return base.Fill((DataTable) data, dataReader); // MDAC 65506
}
else {
return base.Fill((DataSet) data, srcTable, dataReader, 0, 0);
}
}
finally {
if (null != dataReader) {
dataReader.Close();
}
}
}
return 0;
}
private void FillClose(bool isrecordset, object value) {
OleDbHResult hr;
if (isrecordset) {
Bid.Trace("<oledb.Recordset15.Close|API|ADODB>\n");
hr = ((UnsafeNativeMethods.Recordset15)value).Close(); // MDAC 78415
Bid.Trace("<oledb.Recordset15.Close|API|ADODB|RET> %08X{HRESULT}\n", hr);
}
else {
Bid.Trace("<oledb._ADORecord.Close|API|ADODB>\n");
hr = ((UnsafeNativeMethods._ADORecord)value).Close(); // MDAC 78415
Bid.Trace("<oledb._ADORecord.Close|API|ADODB|RET> %08X{HRESULT}\n", hr);
}
if ((0 < (int)hr) && (ODB.ADODB_AlreadyClosedError != (int)hr)) {
UnsafeNativeMethods.IErrorInfo errorInfo = null;
UnsafeNativeMethods.GetErrorInfo(0, out errorInfo);
string message = String.Empty;
if (null != errorInfo) {
OleDbHResult hresult = ODB.GetErrorDescription(errorInfo, hr, out message);
}
throw new COMException(message, (int)hr);
}
}
override protected void OnRowUpdated(RowUpdatedEventArgs value) {
OleDbRowUpdatedEventHandler handler = (OleDbRowUpdatedEventHandler) Events[EventRowUpdated];
if ((null != handler) && (value is OleDbRowUpdatedEventArgs)) {
handler(this, (OleDbRowUpdatedEventArgs) value);
}
base.OnRowUpdated(value);
}
override protected void OnRowUpdating(RowUpdatingEventArgs value) {
OleDbRowUpdatingEventHandler handler = (OleDbRowUpdatingEventHandler) Events[EventRowUpdating];
if ((null != handler) && (value is OleDbRowUpdatingEventArgs)) {
handler(this, (OleDbRowUpdatingEventArgs) value);
}
base.OnRowUpdating(value);
}
static private string GetSourceTableName(string srcTable, int index) {
//if ((null != srcTable) && (0 <= index) && (index < srcTable.Length)) {
if (0 == index) {
return srcTable; //[index];
}
return srcTable + index.ToString(System.Globalization.CultureInfo.InvariantCulture);
}
}
}
|