2 // Mono.Data.Tds.Protocol.Tds42.cs
5 // Tim Coleman (tim@timcoleman.com)
7 // Copyright (C) 2002 Tim Coleman
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 namespace Mono
.Data
.Tds
.Protocol
{
34 public class Tds42
: Tds
38 public static readonly TdsVersion Version
= TdsVersion
.tds42
;
44 public Tds42 (string server
, int port
)
45 : this (server
, port
, 512, 15)
49 public Tds42 (string server
, int port
, int packetSize
, int timeout
)
50 : base (server
, port
, packetSize
, timeout
, Version
)
54 #endregion // Constructors
58 public override bool Connect (TdsConnectionParameters connectionParameters
)
61 throw new InvalidOperationException ("The connection is already open.");
63 SetCharset (connectionParameters
.Charset
);
64 SetLanguage (connectionParameters
.Language
);
67 byte[] empty
= new byte[0];
69 Comm
.StartPacket (TdsPacketType
.Logon
);
71 // hostname (offset 0)
72 byte[] tmp
= Comm
.Append (connectionParameters
.Hostname
, 30, pad
);
73 Comm
.Append ((byte) (tmp
.Length
< 30 ? tmp
.Length
: 30));
75 // username (offset 31 0x1f)
76 tmp
= Comm
.Append (connectionParameters
.User
, 30, pad
);
77 Comm
.Append ((byte) (tmp
.Length
< 30 ? tmp
.Length
: 30));
79 // password (offset 62 0x3e)
80 tmp
= Comm
.Append (connectionParameters
.Password
, 30, pad
);
81 Comm
.Append ((byte) (tmp
.Length
< 30 ? tmp
.Length
: 30));
83 // hostproc (offset 93 0x5d)
84 Comm
.Append ("00000116", 8, pad
);
86 // unused (offset 109 0x6d)
87 Comm
.Append (empty
, (30-14), pad
);
90 Comm
.Append ((byte) 0x0);
91 Comm
.Append ((byte) 0xa0);
92 Comm
.Append ((byte) 0x24);
93 Comm
.Append ((byte) 0xcc);
94 Comm
.Append ((byte) 0x50);
95 Comm
.Append ((byte) 0x12);
98 Comm
.Append ((byte) 8);
100 // Byte order of 2 byte ints
101 // 2 = <MSB, LSB>, 3 = <LSB, MSB>
102 Comm
.Append ((byte) 3);
104 // Byte order of 4 byte ints
105 // 0 = <MSB, LSB>, 1 = <LSB, MSB>
106 Comm
.Append ((byte) 1);
108 // Character representation
109 // (6 = ASCII, 7 = EBCDIC)
110 Comm
.Append ((byte) 6);
112 // Eight byte floating point representation
113 // 4 = IEEE <MSB, ..., LSB>
115 // 10 = IEEE <LSB, ..., MSB>
117 Comm
.Append ((byte) 10);
119 // Eight byte date format
120 // 8 = <MSB, ..., LSB>
121 Comm
.Append ((byte) 9);
124 Comm
.Append ((byte) 1);
126 // disallow dump/load and bulk insert
127 Comm
.Append ((byte) 1);
129 // sql interface type
130 Comm
.Append ((byte) 0);
132 // type of network connection
133 Comm
.Append ((byte) 0);
137 Comm
.Append (empty
, 7, pad
);
139 tmp
= Comm
.Append (connectionParameters
.ApplicationName
, 30, pad
);
140 Comm
.Append ((byte) (tmp
.Length
< 30 ? tmp
.Length
: 30));
143 tmp
= Comm
.Append (DataSource
, 30, pad
);
144 Comm
.Append ((byte) (tmp
.Length
< 30 ? tmp
.Length
: 30));
147 Comm
.Append (empty
, 2, pad
);
148 tmp
= Comm
.Append (connectionParameters
.Password
, 253, pad
);
149 Comm
.Append ((byte) (tmp
.Length
< 253 ? tmp
.Length
+ 2 : 253 + 2));
152 Comm
.Append ((byte) (((byte) Version
) / 10));
153 Comm
.Append ((byte) (((byte) Version
) % 10));
154 Comm
.Append ((byte) 0);
155 Comm
.Append ((byte) 0);
158 tmp
= Comm
.Append (connectionParameters
.ProgName
, 10, pad
);
159 Comm
.Append ((byte) (tmp
.Length
< 10 ? tmp
.Length
: 10));
162 Comm
.Append ((byte) 6);
164 // Tell the server we can handle SQLServer version 6
165 Comm
.Append ((byte) 0);
167 // Send zero to tell the server we can't handle any other version
168 Comm
.Append ((byte) 0);
169 Comm
.Append ((byte) 0);
171 // auto convert short
172 Comm
.Append ((byte) 0);
175 Comm
.Append ((byte) 0x0d);
178 Comm
.Append ((byte) 0x11);
181 tmp
= Comm
.Append (Language
, 30, pad
);
182 Comm
.Append ((byte) (tmp
.Length
< 30 ? tmp
.Length
: 30));
184 // notify on lang change
185 Comm
.Append ((byte) 1);
187 // security label hierarchy
188 Comm
.Append ((short) 0);
190 // security components
191 Comm
.Append (empty
, 8, pad
);
194 Comm
.Append ((short) 0);
196 // security login role
197 Comm
.Append ((byte) 0);
200 tmp
= Comm
.Append (Charset
, 30, pad
);
201 Comm
.Append ((byte) (tmp
.Length
< 30 ? tmp
.Length
: 30));
203 // notify on charset change
204 Comm
.Append ((byte) 1);
206 // length of tds packets
207 tmp
= Comm
.Append (PacketSize
.ToString (), 6, pad
);
208 Comm
.Append ((byte) 3);
210 // pad out to a longword
211 Comm
.Append (empty
, 8, pad
);
221 protected override TdsDataColumnCollection
ProcessColumnInfo ()
225 int totalLength
= Comm
.GetTdsShort ();
228 TdsDataColumnCollection result
= new TdsDataColumnCollection ();
230 while (bytesRead
< totalLength
) {
235 byte[] flagData
= new byte[4];
236 for (int i
= 0; i
< 4; i
+= 1) {
237 flagData
[i
] = Comm
.GetByte ();
240 bool nullable
= (flagData
[2] & 0x01) > 0;
241 bool caseSensitive
= (flagData
[2] & 0x02) > 0;
242 bool writable
= (flagData
[2] & 0x0c) > 0;
243 bool autoIncrement
= (flagData
[2] & 0x10) > 0;
245 string tableName
= String
.Empty
;
246 TdsColumnType columnType
= (TdsColumnType
) Comm
.GetByte ();
250 if (columnType
== TdsColumnType
.Text
|| columnType
== TdsColumnType
.Image
) {
254 int tableNameLength
= Comm
.GetTdsShort ();
256 tableName
= Comm
.GetString (tableNameLength
);
257 bytesRead
+= tableNameLength
;
258 bufLength
= 2 << 31 - 1;
260 else if (columnType
== TdsColumnType
.Decimal
|| columnType
== TdsColumnType
.Numeric
) {
261 bufLength
= Comm
.GetByte ();
263 precision
= Comm
.GetByte ();
265 scale
= Comm
.GetByte ();
268 else if (IsFixedSizeColumn (columnType
))
269 bufLength
= LookupBufferSize (columnType
);
271 bufLength
= (int) Comm
.GetByte () & 0xff;
275 int index
= result
.Add (new TdsDataColumn ());
276 result
[index
]["NumericPrecision"] = precision
;
277 result
[index
]["NumericScale"] = scale
;
278 result
[index
]["ColumnSize"] = bufLength
;
279 result
[index
]["ColumnName"] = ColumnNames
[index
];
280 result
[index
]["ColumnType"] = columnType
;
281 result
[index
]["BaseTableName"] = tableName
;
282 result
[index
]["AllowDBNull"] = nullable
;
283 result
[index
]["IsReadOnly"] = !writable
;
289 #endregion // Methods