comparison Source/Data/DataProvider/OleDbDataProvider.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Data;
3 using System.Data.Common;
4 using System.Data.OleDb;
5
6 namespace BLToolkit.Data.DataProvider
7 {
8 using Sql.SqlProvider;
9
10 /// <summary>
11 /// Implements access to the Data Provider for OLE DB.
12 /// </summary>
13 /// <remarks>
14 /// See the <see cref="DbManager.AddDataProvider(DataProviderBase)"/> method to find an example.
15 /// </remarks>
16 /// <seealso cref="DbManager.AddDataProvider(DataProviderBase)">AddDataManager Method</seealso>
17 public class OleDbDataProvider : DataProviderBase
18 {
19 /// <summary>
20 /// Creates the database connection object.
21 /// </summary>
22 /// <remarks>
23 /// See the <see cref="DbManager.AddDataProvider(DataProviderBase)"/> method to find an example.
24 /// </remarks>
25 /// <seealso cref="DbManager.AddDataProvider(DataProviderBase)">AddDataManager Method</seealso>
26 /// <returns>The database connection object.</returns>
27 public override IDbConnection CreateConnectionObject()
28 {
29 return new OleDbConnection();
30 }
31
32 /// <summary>
33 /// Creates the data adapter object.
34 /// </summary>
35 /// <remarks>
36 /// See the <see cref="DbManager.AddDataProvider(DataProviderBase)"/> method to find an example.
37 /// </remarks>
38 /// <seealso cref="DbManager.AddDataProvider(DataProviderBase)">AddDataManager Method</seealso>
39 /// <returns>A data adapter object.</returns>
40 public override DbDataAdapter CreateDataAdapterObject()
41 {
42 return new OleDbDataAdapter();
43 }
44
45 /// <summary>
46 /// Populates the specified <see cref="IDbCommand"/> object's Parameters collection with
47 /// parameter information for the stored procedure specified in the <see cref="IDbCommand"/>.
48 /// </summary>
49 /// <remarks>
50 /// See the <see cref="DbManager.AddDataProvider(DataProviderBase)"/> method to find an example.
51 /// </remarks>
52 /// <seealso cref="DbManager.AddDataProvider(DataProviderBase)">AddDataManager Method</seealso>
53 /// <param name="command">The <see cref="IDbCommand"/> referencing the stored procedure for which
54 /// the parameter information is to be derived. The derived parameters will be
55 /// populated into the Parameters of this command.</param>
56 public override bool DeriveParameters(IDbCommand command)
57 {
58 OleDbCommandBuilder.DeriveParameters((OleDbCommand)command);
59 return true;
60 }
61
62 public override object Convert(object value, ConvertType convertType)
63 {
64 switch (convertType)
65 {
66 case ConvertType.NameToQueryParameter:
67 case ConvertType.NameToCommandParameter:
68 case ConvertType.NameToSprocParameter:
69 return "@" + value;
70
71 case ConvertType.NameToQueryField:
72 case ConvertType.NameToQueryFieldAlias:
73 case ConvertType.NameToQueryTableAlias:
74 {
75 var name = value.ToString();
76
77 if (name.Length > 0 && name[0] == '[')
78 return value;
79 }
80
81 return "[" + value + "]";
82
83 case ConvertType.NameToDatabase:
84 case ConvertType.NameToOwner:
85 case ConvertType.NameToQueryTable:
86 {
87 var name = value.ToString();
88
89 if (name.Length > 0 && name[0] == '[')
90 return value;
91
92 if (name.IndexOf('.') > 0)
93 value = string.Join("].[", name.Split('.'));
94 }
95
96 return "[" + value + "]";
97
98 case ConvertType.SprocParameterToName:
99 if (value != null)
100 {
101 var str = value.ToString();
102 return str.Length > 0 && str[0] == '@'? str.Substring(1): str;
103 }
104
105 break;
106
107 case ConvertType.ExceptionToErrorNumber:
108 if (value is OleDbException)
109 {
110 var ex = (OleDbException)value;
111 if (ex.Errors.Count > 0)
112 return ex.Errors[0].NativeError;
113 }
114
115 break;
116 }
117
118 return value;
119 }
120
121 public override ISqlProvider CreateSqlProvider()
122 {
123 throw new NotSupportedException();
124 }
125
126 /// <summary>
127 /// Returns connection type.
128 /// </summary>
129 /// <remarks>
130 /// See the <see cref="DbManager.AddDataProvider(DataProviderBase)"/> method to find an example.
131 /// </remarks>
132 /// <seealso cref="DbManager.AddDataProvider(DataProviderBase)">AddDataManager Method</seealso>
133 /// <value>An instance of the <see cref="Type"/> class.</value>
134 public override Type ConnectionType
135 {
136 get { return typeof(OleDbConnection); }
137 }
138
139 public const string NameString = DataProvider.ProviderName.OleDb;
140
141 /// <summary>
142 /// Returns the data provider name.
143 /// </summary>
144 /// <remarks>
145 /// See the <see cref="DbManager.AddDataProvider(DataProviderBase)"/> method to find an example.
146 /// </remarks>
147 /// <seealso cref="DbManager.AddDataProvider(DataProviderBase)">AddDataProvider Method</seealso>
148 /// <value>Data provider name.</value>
149 public override string Name
150 {
151 get { return NameString; }
152 }
153 }
154 }