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.
30 using Mono.Security.Protocol.Ntlm;
32 using System.Collections;
33 using System.ComponentModel;
34 using System.Diagnostics;
35 using System.Net.Sockets;
38 namespace Mono.Data.Tds.Protocol {
39 public class TdsBulkCopy
48 public TdsBulkCopy (Tds tds)
56 public bool SendColumnMetaData (string colMetaData)
58 tds.Comm.StartPacket (TdsPacketType.Query);
59 tds.Comm.Append (colMetaData);
60 tds.ExecBulkCopyMetaData (30, false);
64 public bool BulkCopyStart (TdsMetaParameterCollection parameters)
66 tds.Comm.StartPacket (TdsPacketType.Bulk);
67 tds.Comm.Append ((byte) TdsPacketSubType.ColumnMetadata);
69 foreach (TdsMetaParameter param in parameters) {
70 if (param.Value != null)
74 tds.Comm.Append (count);
75 if (parameters != null) {
76 foreach (TdsMetaParameter param in parameters) {
77 if (param.Value != null)
79 tds.Comm.Append ((short) 0x00);
80 tds.Comm.Append ((short) 0x0a);
81 WriteParameterInfo (param);
82 tds.Comm.Append ((byte) param.ParameterName.Length);
83 tds.Comm.Append (param.ParameterName);
89 public bool BulkCopyData (object o, int size, bool isNewRow)
92 tds.Comm.Append ((byte) TdsPacketSubType.Row);
95 tds.Comm.Append ((short) size);
101 public bool BulkCopyEnd ()
103 tds.Comm.Append ((short) TdsPacketSubType.Done);
104 tds.ExecBulkCopy (30, false);
108 private void WriteParameterInfo (TdsMetaParameter param)
111 Ms.net send non-nullable datatypes as nullable and allows setting null values
112 to int/float etc.. So, using Nullable form of type for all data
114 param.IsNullable = true;
115 TdsColumnType colType = param.GetMetaType ();
116 param.IsNullable = false;
118 tds.Comm.Append ((byte)colType); // type
122 size = param.GetActualSize ();
127 If column type is SqlDbType.NVarChar the size of parameter is multiplied by 2
128 FIXME: Need to check for other types
130 if (colType == TdsColumnType.BigNVarChar)
132 if (tds.IsLargeType (colType))
133 tds.Comm.Append ((short)size); // Parameter size passed in SqlParameter
134 else if (tds.IsBlobType (colType))
135 tds.Comm.Append (size); // Parameter size passed in SqlParameter
137 tds.Comm.Append ((byte)size);
139 // Precision and Scale are non-zero for only decimal/numeric
140 if ( param.TypeName == "decimal" || param.TypeName == "numeric") {
141 tds.Comm.Append ((param.Precision!=0)?param.Precision:(byte)28);
142 tds.Comm.Append (param.Scale);