Esempio n. 1
0
		public void Open( MySqlConnectionString settings )
		{
			// connect to one of our specified hosts
			try 
			{
				StreamCreator sc = new StreamCreator( settings.Server, settings.Port, settings.PipeName );
				stream = sc.GetStream( settings.ConnectionTimeout );
			}
			catch (Exception ex)
			{
				throw new MySqlException("Unable to connect to any of the specified MySQL hosts", ex);
			}

			if (stream == null) 
				throw new MySqlException("Unable to connect to any of the specified MySQL hosts");

			writer = new BufferedStream( stream );
			// read off the welcome packet and parse out it's values
			Packet packet = ReadPacket();
			protocol = packet.ReadByte();
			versionString = packet.ReadString();
			serverVersion = DBVersion.Parse( versionString );
			threadID = (uint)packet.ReadInteger(4);
			encryptionSeed = packet.ReadString();

			// read in Server capabilities if they are provided
			serverCaps = 0;
			if (packet.HasMoreData)
				serverCaps = (int)packet.ReadInteger(2);

			Authenticate( settings.UserId, settings.Password, settings.UseCompression );

			// if we are using compression, then we use our CompressedStream class
			// to hide the ugliness of managing the compression
			if (settings.UseCompression)
			{
				stream = new CompressedStream( stream );
				writer = new BufferedStream( stream );
			}

			isOpen = true;
		}
Esempio n. 2
0
		public void SetValueData( byte[] buf, long index, long len, DBVersion version )
		{
			if (len == -1)
			{
				value = DBNull.Value;
				buffer = null;
				return;
			}

			buffer = buf;
			bufIndex = index;
			bufLength = len;

			// if it is a blob and binary, then GetBytes is the way to go
			if ( IsBlob() && IsBinary() ) 
			{
				dbType = DbType.Binary;
				value = buffer;
				return;
			}

			string sValue = encoding.GetString( buf, (int)index, (int)len );

			switch(colType)
			{
				case MySqlDbType.Byte:
					if (IsUnsigned())
						value = Byte.Parse( sValue );
					else
						value = SByte.Parse( sValue );
					break;

				case MySqlDbType.Short:
					if (IsUnsigned())
						value = UInt16.Parse( sValue );
					else
						value = Int16.Parse( sValue );
					break;
					
				case MySqlDbType.Int : 
				case MySqlDbType.Int24:
					if (IsUnsigned())
						value = UInt32.Parse( sValue );
					else
						value = Int32.Parse( sValue );
					break;

				case MySqlDbType.BigInt:
					if (IsUnsigned())
						value = UInt64.Parse( sValue );
					else
						value = Int64.Parse( sValue );
					break;

				case MySqlDbType.Decimal:
					value = Decimal.Parse( sValue , numberFormat );
					break;

				case MySqlDbType.Float:
					value = Convert.ToSingle( sValue, numberFormat );
					break;

				case MySqlDbType.Double:
					value = Convert.ToDouble( sValue, numberFormat );
					break;

				case MySqlDbType.Year:
					value = Int32.Parse( sValue );
					break;

				case MySqlDbType.Date:
					ParseDateValue( "0000-00-00", "yyyy-MM-dd", sValue );
					break;

				case MySqlDbType.Datetime:
					ParseDateValue( "0000-00-00 00:00:00", "yyyy-MM-dd HH:mm:ss", sValue );
					break;

				case MySqlDbType.Time:
					if (sValue.Equals("00:00:00"))
						value = DBNull.Value;
					else
						value = TimeSpan.Parse(sValue);
					break;

				case MySqlDbType.Timestamp:
					// MySql 4.1.0 and later use DateTime format for timestamp
					if (version.isAtLeast(4,1,0))  
					{
						ParseDateValue( "0000-00-00 00:00:00", "yyyy-MM-dd HH:mm:ss", sValue );
						return;
					}

					string pattern;
					string null_value = "00000000000000";
					switch (ColumnLength) 
					{
						case 2:  pattern = "yy"; break;
						case 4:  pattern = "yyMM"; break;
						case 6:  pattern = "yyMMdd"; break;
						case 8:  pattern = "yyyyMMdd"; break;
						case 10: pattern = "yyMMddHHmm"; break;
						case 12: pattern = "yyMMddHHmmss"; break;
						case 14: 
						default: pattern = "yyyyMMddHHmmss"; break;
					}

					if (ColumnLength > 2 && sValue.Equals( null_value.Substring(0, ColumnLength)))
						value = DBNull.Value;
					else
						value = DateTime.ParseExact( sValue, pattern, new System.Globalization.DateTimeFormatInfo());
					break;

				case MySqlDbType.String:
				case MySqlDbType.VarChar:
				case MySqlDbType.Blob:
				case MySqlDbType.TinyBlob:
				case MySqlDbType.LongBlob:
				case MySqlDbType.MediumBlob: 
					value = sValue;
					break;

				default:
					throw new NotSupportedException();
			}
		}