2 // Mono.Data.Tds.Protocol.TdsBulkCopy.cs
5 // Nagappan A (anagappan@novell.com)
7 // Copyright (C) 2007 Novell Inc
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 namespace Mono
.Data
.Tds
.Protocol
{
33 public class TdsBulkCopy
42 public TdsBulkCopy (Tds tds
)
50 public bool SendColumnMetaData (string colMetaData
)
52 tds
.Comm
.StartPacket (TdsPacketType
.Query
);
53 tds
.Comm
.Append (colMetaData
);
54 tds
.ExecBulkCopyMetaData (30, false);
58 public bool BulkCopyStart (TdsMetaParameterCollection parameters
)
60 tds
.Comm
.StartPacket (TdsPacketType
.Bulk
);
61 tds
.Comm
.Append ((byte) TdsPacketSubType
.ColumnMetadata
);
63 foreach (TdsMetaParameter param
in parameters
) {
64 if (param
.Value
!= null)
68 tds
.Comm
.Append (count
);
69 if (parameters
!= null) {
70 foreach (TdsMetaParameter param
in parameters
) {
71 if (param
.Value
!= null)
73 tds
.Comm
.Append ((short) 0x00);
74 tds
.Comm
.Append ((short) 0x0a);
75 WriteParameterInfo (param
);
76 tds
.Comm
.Append ((byte) param
.ParameterName
.Length
);
77 tds
.Comm
.Append (param
.ParameterName
);
83 public bool BulkCopyData (object o
, int size
, bool isNewRow
)
86 tds
.Comm
.Append ((byte) TdsPacketSubType
.Row
);
89 tds
.Comm
.Append ((short) size
);
95 public bool BulkCopyEnd ()
97 tds
.Comm
.Append ((short) TdsPacketSubType
.Done
);
98 tds
.ExecBulkCopy (30, false);
102 private void WriteParameterInfo (TdsMetaParameter param
)
105 Ms.net send non-nullable datatypes as nullable and allows setting null values
106 to int/float etc.. So, using Nullable form of type for all data
108 param
.IsNullable
= true;
109 TdsColumnType colType
= param
.GetMetaType ();
110 param
.IsNullable
= false;
112 tds
.Comm
.Append ((byte)colType
); // type
116 size
= param
.GetActualSize ();
121 If column type is SqlDbType.NVarChar the size of parameter is multiplied by 2
122 FIXME: Need to check for other types
124 if (colType
== TdsColumnType
.BigNVarChar
)
126 if (tds
.IsLargeType (colType
))
127 tds
.Comm
.Append ((short)size
); // Parameter size passed in SqlParameter
128 else if (tds
.IsBlobType (colType
))
129 tds
.Comm
.Append (size
); // Parameter size passed in SqlParameter
131 tds
.Comm
.Append ((byte)size
);
133 // Precision and Scale are non-zero for only decimal/numeric
134 if ( param
.TypeName
== "decimal" || param
.TypeName
== "numeric") {
135 tds
.Comm
.Append ((param
.Precision
!=0)?param
.Precision
:(byte)29);
136 tds
.Comm
.Append (param
.Scale
);