using System.Net.Sockets;
using System.Text;
using System.Xml;
-#if NET_2_0
using System.Collections.Generic;
-#endif
+using System.Security;
namespace System.Data.SqlClient
{
[DefaultEvent ("InfoMessage")]
-#if NET_2_0
public sealed class SqlConnection : DbConnection, IDbConnection, ICloneable
-#else
- public sealed class SqlConnection : Component, IDbConnection, ICloneable
-#endif // NET_2_0
{
#region Fields
// The set of SQL connection pools
static TdsConnectionPoolManager sqlConnectionPools = new TdsConnectionPoolManager (TdsVersion.tds80);
-#if NET_2_0
- const int DEFAULT_PACKETSIZE = 32768;
+ const int DEFAULT_PACKETSIZE = 8000;
const int MAX_PACKETSIZE = 32768;
-#else
- const int DEFAULT_PACKETSIZE = 32767;
- const int MAX_PACKETSIZE = 32767;
-#endif
const int MIN_PACKETSIZE = 512;
const int DEFAULT_CONNECTIONTIMEOUT = 15;
+ const int DEFAULT_CONNECTIONLIFETIME = 0;
const int DEFAULT_MAXPOOLSIZE = 100;
const int MIN_MAXPOOLSIZE = 1;
const int DEFAULT_MINPOOLSIZE = 0;
// The connection string that identifies this connection
string connectionString;
+ // The connection credentials
+ SqlCredential credentials;
+
// The transaction object for the current transaction
SqlTransaction transaction;
TdsConnectionParameters parms;
bool connectionReset;
+ int connectionLifeTime;
bool pooling;
string dataSource;
int connectionTimeout;
ConnectionString = connectionString;
}
+ public SqlConnection (string connectionString, SqlCredential cred)
+ {
+ ConnectionString = connectionString;
+ Credentials = cred;
+ }
+
#endregion // Constructors
#region Properties
}
}
-#if !NET_2_0
- [DataSysDescription ("Current connection timeout value, 'Connect Timeout=X' in the ConnectionString.")]
-#endif
+ public SqlCredential Credentials {
+ get {
+ return credentials;
+ }
+ set {
+ credentials = value;
+ }
+ }
+
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public
-#if NET_2_0
override
-#endif // NET_2_0
int ConnectionTimeout {
get { return connectionTimeout; }
}
-#if !NET_2_0
- [DataSysDescription ("Current SQL Server database, 'Initial Catalog=X' in the connection string.")]
-#endif
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public
-#if NET_2_0
override
-#endif // NET_2_0
string Database {
get {
if (State == ConnectionState.Open)
set { dataReader = value; }
}
-#if !NET_2_0
- [DataSysDescription ("Current SqlServer that the connection is opened to, 'Data Source=X' in the connection string. ")]
-#else
[Browsable(true)]
-#endif
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public
-#if NET_2_0
override
-#endif // NET_2_0
string DataSource {
get { return dataSource; }
}
-#if !NET_2_0
- [DataSysDescription ("Network packet size, 'Packet Size=x' in the connection string.")]
-#endif
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public int PacketSize {
get {
}
[Browsable (false)]
-#if !NET_2_0
- [DataSysDescription ("Version of the SQL Server accessed by the SqlConnection.")]
-#endif
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public
-#if NET_2_0
override
-#endif // NET_2_0
string ServerVersion {
get {
if (state == ConnectionState.Closed)
}
[Browsable (false)]
-#if !NET_2_0
- [DataSysDescription ("The ConnectionState indicating whether the connection is open or closed.")]
-#endif
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public
-#if NET_2_0
override
-#endif // NET_2_0
ConnectionState State {
get { return state; }
}
set { transaction = value; }
}
-#if !NET_2_0
- [DataSysDescription ("Workstation Id, 'Workstation ID=x' in the connection string.")]
-#endif
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public string WorkstationId {
get { return parms.Hostname; }
set { xmlReader = value; }
}
-#if NET_2_0
public bool FireInfoMessageEventOnUserErrors {
get { return fireInfoMessageEventOnUserErrors; }
set { fireInfoMessageEventOnUserErrors = value; }
get { return statisticsEnabled; }
set { statisticsEnabled = value; }
}
-#endif
+
+ protected override DbProviderFactory DbProviderFactory {
+ get {
+ return SqlClientFactory.Instance;
+ }
+ }
+
#endregion // Properties
#region Events
-#if !NET_2_0
- [DataSysDescription ("Event triggered when messages arrive from the DataSource.")]
-#endif
public event SqlInfoMessageEventHandler InfoMessage;
-#if !NET_2_0
- [DataSysDescription ("Event triggered when the connection changes state.")]
- public new event StateChangeEventHandler StateChange;
-#endif
#endregion // Events
case IsolationLevel.ReadCommitted:
isolevel = "READ COMMITTED";
break;
-#if NET_2_0
case IsolationLevel.Snapshot:
isolevel = "SNAPSHOT";
break;
"value, {0}, is not supported by " +
"the .Net Framework SqlClient " +
"Data Provider.", (int) iso));
-#endif
default:
-#if NET_2_0
throw new ArgumentOutOfRangeException ("IsolationLevel",
string.Format (CultureInfo.CurrentCulture,
"The IsolationLevel enumeration value, {0}, is invalid.",
(int) iso));
-#else
- throw new ArgumentException ("Invalid IsolationLevel parameter: must be ReadCommitted, ReadUncommitted, RepeatableRead, or Serializable.");
-#endif
}
tds.Execute (String.Format ("SET TRANSACTION ISOLATION LEVEL {0};BEGIN TRANSACTION {1}", isolevel, transactionName));
}
public
-#if NET_2_0
override
-#endif // NET_2_0
void ChangeDatabase (string database)
{
if (!IsValidDatabaseName (database))
}
public
-#if NET_2_0
override
-#endif // NET_2_0
void Close ()
{
if (transaction != null && transaction.IsOpen)
return new SqlConnection (ConnectionString);
}
-#if NET_2_0
protected override DbTransaction BeginDbTransaction (IsolationLevel isolationLevel)
{
return BeginTransaction (isolationLevel);
{
return CreateCommand ();
}
-#else
- IDbTransaction IDbConnection.BeginTransaction ()
- {
- return BeginTransaction ();
- }
-
- IDbTransaction IDbConnection.BeginTransaction (IsolationLevel iso)
- {
- return BeginTransaction (iso);
- }
-
- IDbCommand IDbConnection.CreateCommand ()
- {
- return CreateCommand ();
- }
-#endif
public
-#if NET_2_0
override
-#endif // NET_2_0
void Open ()
{
string serverName = string.Empty;
if (!pooling) {
if(!ParseDataSource (dataSource, out port, out serverName))
throw new SqlException(20, 0, "SQL Server does not exist or access denied.", 17, "ConnectionOpen (Connect()).", dataSource, parms.ApplicationName, 0);
- tds = new Tds80 (serverName, port, PacketSize, ConnectionTimeout);
+ tds = new Tds80 (serverName, port, PacketSize, ConnectionTimeout, 0);
tds.Pooling = false;
}
else {
if(!ParseDataSource (dataSource, out port, out serverName))
throw new SqlException(20, 0, "SQL Server does not exist or access denied.", 17, "ConnectionOpen (Connect()).", dataSource, parms.ApplicationName, 0);
- TdsConnectionInfo info = new TdsConnectionInfo (serverName, port, packetSize, ConnectionTimeout, minPoolSize, maxPoolSize);
+ TdsConnectionInfo info = new TdsConnectionInfo (serverName, port, packetSize, ConnectionTimeout, minPoolSize, maxPoolSize, connectionLifeTime);
pool = sqlConnectionPools.GetConnectionPool (connectionString, info);
tds = pool.GetConnection ();
}
if (!tds.IsConnected) {
try {
+ if (Credentials != null) {
+ if (parms.User != String.Empty)
+ throw new ArgumentException("UserID already specified");
+ if (parms.PasswordSet)
+ throw new ArgumentException("Password already specified");
+ if (parms.DomainLogin != false)
+ throw new ArgumentException("Cannot use credentials with DomainLogin");
+ parms.User = Credentials.UserId;
+ parms.Password = Credentials.Password;
+ }
tds.Connect (parms);
} catch {
if (pooling)
parms.Reset ();
dataSource = string.Empty;
connectionTimeout = DEFAULT_CONNECTIONTIMEOUT;
+ connectionLifeTime = DEFAULT_CONNECTIONLIFETIME;
connectionReset = true;
pooling = true;
maxPoolSize = DEFAULT_MAXPOOLSIZE;
minPoolSize = DEFAULT_MINPOOLSIZE;
packetSize = DEFAULT_PACKETSIZE;
port = DEFAULT_PORT;
- #if NET_2_0
async = false;
- #endif
}
private void SetProperties (string name , string value)
connectionTimeout = tmpTimeout;
break;
case "connection lifetime" :
+ connectionLifeTime = ConvertToInt32 ("connection lifetime", value, DEFAULT_CONNECTIONLIFETIME);
break;
case "connection reset" :
connectionReset = ConvertToBoolean ("connection reset", value, true);
else
minPoolSize = tmpMinPoolSize;
break;
-#if NET_2_0
case "multipleactiveresultsets":
// FIXME: not implemented
ConvertToBoolean (name, value, false);
case "async" :
async = ConvertToBoolean (name, value, false);
break;
-#endif
case "net" :
case "network" :
case "network library" :
break;
case "password" :
case "pwd" :
- parms.Password = value;
+ parms.Password = new SecureString();
+ foreach (char c in value)
+ parms.Password.AppendChar(c);
+ parms.PasswordSet = true;
break;
case "persistsecurityinfo" :
case "persist security info" :
case "workstation id" :
parms.Hostname = value;
break;
-#if NET_2_0
case "user instance":
userInstance = ConvertToBoolean (name, value, false);
break;
-#endif
default :
throw new ArgumentException("Keyword not supported : '" + name + "'.");
}
InfoMessage (this, value);
}
-#if !NET_2_0
- private new void OnStateChange (StateChangeEventArgs value)
- {
- if (StateChange != null)
- StateChange (this, value);
- }
-#endif
private sealed class SqlMonitorSocket : UdpClient
{
if (Client.Available <= 0)
return -1; // Error
-#if NET_2_0
IPEndPoint endpoint = new IPEndPoint (Dns.GetHostEntry ("localhost").AddressList [0], 0);
-#else
- IPEndPoint endpoint = new IPEndPoint (Dns.GetHostByName ("localhost").AddressList [0], 0);
-#endif
Byte [] rawrs;
rawrs = Receive (ref endpoint);
}
}
-#if NET_2_0
struct ColumnInfo
{
public string name;
}
}
-#endif // NET_2_0
#endregion // Methods
-#if NET_2_0
#region Fields Net 2
bool async;
#endregion // Properties Net 2
-#endif // NET_2_0
}
}