This commit is contained in:
402
source/Data/Data/Sql/SqlCommandWrapper.cs
Normal file
402
source/Data/Data/Sql/SqlCommandWrapper.cs
Normal file
@@ -0,0 +1,402 @@
|
||||
//===============================================================================
|
||||
// Microsoft patterns & practices Enterprise Library
|
||||
// Data Access Application Block
|
||||
//========================= ======================================================
|
||||
// Copyright © Microsoft Corporation. All rights reserved.
|
||||
// Adapted from ACA.NET with permission from Avanade Inc.
|
||||
// ACA.NET copyright © Avanade Inc. All rights reserved.
|
||||
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
|
||||
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
|
||||
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//===============================================================================
|
||||
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
|
||||
namespace GZTW.Data.Sql
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>Represents a SQL statement or stored procedure to execute against a Sql Server database.</para>
|
||||
/// </summary>
|
||||
public class SqlCommandWrapper : DBCommandWrapper
|
||||
{
|
||||
private SqlCommand command;
|
||||
private int rowsAffected;
|
||||
private object[] parameterValues;
|
||||
private bool needsParameters = false;
|
||||
private char parameterToken;
|
||||
|
||||
/// <summary>
|
||||
/// <para>Initialize a new instance of the <see cref="SqlCommandWrapper"/> class with the text of a query and the command type.</para>
|
||||
/// </summary>
|
||||
/// <param name="commandText"><para>The stored procedure name or SQL sting the command represents.</para></param>
|
||||
/// <param name="commandType"><para>One of the <see crer="CommandType"/> values.</para></param>
|
||||
/// <param name="parameterToken"><para>The parameter delimeter for database commands.</para></param>
|
||||
internal SqlCommandWrapper(string commandText, CommandType commandType, char parameterToken)
|
||||
{
|
||||
this.parameterToken = parameterToken;
|
||||
this.command = CreateCommand(commandText, commandType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Initialize a new instance of the <see cref="SqlCommandWrapper"/> class with the text of a query the command type, and the parameter values.</para>
|
||||
/// </summary>
|
||||
/// <param name="commandText"><para>The stored procedure name or SQL sting the command represents.</para></param>
|
||||
/// <param name="commandType"><para>One of the <see crer="CommandType"/> values.</para></param>
|
||||
/// <param name="parameterToken"><para>The parameter delimeter for database commands.</para></param>
|
||||
/// <param name="parameterValues"><para>The parameter values to assign in positional order.</para></param>
|
||||
internal SqlCommandWrapper(string commandText, CommandType commandType, char parameterToken, object[] parameterValues) : this(commandText, commandType, parameterToken)
|
||||
{
|
||||
//this.command = CreateCommand(commandText, commandType);
|
||||
this.parameterValues = parameterValues;
|
||||
if (commandType == CommandType.StoredProcedure)
|
||||
{
|
||||
this.needsParameters = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Gets the underlying <see cref="IDbCommand"/>.</para>
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <para>The underlying <see cref="IDbCommand"/>. The default is <see langword="null"/>.</para>
|
||||
/// </value>
|
||||
/// <remarks>
|
||||
/// <para>This command is a <see cref="SqlCommand"/></para>
|
||||
/// </remarks>
|
||||
/// <seealso cref="SqlCommand"/>
|
||||
public override IDbCommand Command
|
||||
{
|
||||
get { return this.command; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Gets or sets the rows affected by this command.</para>
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <para>The rows affected by this command.</para>
|
||||
/// </value>
|
||||
public override int RowsAffected
|
||||
{
|
||||
get { return this.rowsAffected; }
|
||||
set { this.rowsAffected = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Gets or sets the wait time before terminating the attempt to execute a command and generating an error.</para>
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <para>The wait time before terminating the attempt to execute a command and generating an error.</para>
|
||||
/// </value>
|
||||
public override int CommandTimeout
|
||||
{
|
||||
get { return this.command.CommandTimeout; }
|
||||
set { this.command.CommandTimeout = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Adds a new instance of an <see cref="SqlParameter"/> object to the command.</para>
|
||||
/// </summary>
|
||||
/// <param name="name"><para>The name of the parameter.</para></param>
|
||||
/// <param name="dbType"><para>One of the <see cref="DbType"/> values.</para></param>
|
||||
/// <param name="size"><para>The maximum size of the data within the column.</para></param>
|
||||
/// <param name="direction"><para>One of the <see cref="ParameterDirection"/> values.</para></param>
|
||||
/// <param name="nullable"><para>Avalue indicating whether the parameter accepts null values.</para></param>
|
||||
/// <param name="precision"><para>The maximum number of digits used to represent the <paramref name="value"/>.</para></param>
|
||||
/// <param name="scale"><para>The number of decimal places to which <paramref name="value"/> is resolved.</para></param>
|
||||
/// <param name="sourceColumn"><para>The name of the source column mapped to the DataSet and used for loading or returning the <paramref name="value"/>.</para></param>
|
||||
/// <param name="sourceVersion"><para>One of the <see cref="DataRowVersion"/> values.</para></param>
|
||||
/// <param name="value"><para>The value of the parameter.</para></param>
|
||||
public override void AddParameter(string name, DbType dbType, int size, ParameterDirection direction, bool nullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value)
|
||||
{
|
||||
this.command.Parameters.Add(CreateParameter(name, dbType, size, direction, nullable, precision, scale, sourceColumn, sourceVersion, value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Adds a new instance of an <see cref="SqlParameter"/> object to the command.</para>
|
||||
/// </summary>
|
||||
/// <param name="name"><para>The name of the parameter.</para></param>
|
||||
/// <param name="sqlType"><para>One of the <see cref="SqlDbType"/> values.</para></param>
|
||||
/// <param name="size"><para>The maximum size of the data within the column.</para></param>
|
||||
/// <param name="direction"><para>One of the <see cref="ParameterDirection"/> values.</para></param>
|
||||
/// <param name="nullable"><para>Avalue indicating whether the parameter accepts null values.</para></param>
|
||||
/// <param name="precision"><para>The maximum number of digits used to represent the <paramref name="value"/>.</para></param>
|
||||
/// <param name="scale"><para>The number of decimal places to which <paramref name="value"/> is resolved.</para></param>
|
||||
/// <param name="sourceColumn"><para>The name of the source column mapped to the DataSet and used for loading or returning the <paramref name="value"/>.</para></param>
|
||||
/// <param name="sourceVersion"><para>One of the <see cref="DataRowVersion"/> values.</para></param>
|
||||
/// <param name="value"><para>The value of the parameter.</para></param>
|
||||
public void AddParameter(string name, SqlDbType sqlType, int size, ParameterDirection direction, bool nullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value)
|
||||
{
|
||||
SqlParameter param = CreateParameter(name, DbType.String, size, direction, nullable, precision, scale, sourceColumn, sourceVersion, value);
|
||||
param.SqlDbType = sqlType;
|
||||
this.command.Parameters.Add(param);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Adds a new instance of an <see cref="SqlParameter"/> object to the command.</para>
|
||||
/// </summary>
|
||||
/// <param name="name"><para>The name of the parameter.</para></param>
|
||||
/// <param name="dbType"><para>One of the <see cref="DbType"/> values.</para></param>
|
||||
/// <param name="direction"><para>One of the <see cref="ParameterDirection"/> values.</para></param>
|
||||
/// <param name="sourceColumn"><para>The name of the source column mapped to the DataSet and used for loading or returning the <paramref name="value"/>.</para></param>
|
||||
/// <param name="sourceVersion"><para>One of the <see cref="DataRowVersion"/> values.</para></param>
|
||||
/// <param name="value"><para>The value of the parameter.</para></param>
|
||||
public override void AddParameter(string name, DbType dbType, ParameterDirection direction, string sourceColumn, DataRowVersion sourceVersion, object value)
|
||||
{
|
||||
SqlParameter param = CreateParameter(name, dbType, 0, direction, false, 0, 0, sourceColumn, sourceVersion, value);
|
||||
this.command.Parameters.Add(param);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Adds a new instance of an <see cref="SqlParameter"/> object to the command set as <see cref="ParameterDirection"/> value of Output.</para>
|
||||
/// </summary>
|
||||
/// <param name="name"><para>The name of the parameter.</para></param>
|
||||
/// <param name="dbType"><para>One of the <see cref="DbType"/> values.</para></param>
|
||||
/// <param name="size"><para>The maximum size of the data within the column.</para></param>
|
||||
public override void AddOutParameter(string name, DbType dbType, int size)
|
||||
{
|
||||
AddParameter(name, dbType, size, ParameterDirection.Output, true, 0, 0, String.Empty, DataRowVersion.Default, DBNull.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Adds a new instance of an <see cref="SqlParameter"/> object to the command set as <see cref="ParameterDirection"/> value of Input.</para>
|
||||
/// </summary>
|
||||
/// <param name="name"><para>The name of the parameter.</para></param>
|
||||
/// <param name="dbType"><para>One of the <see cref="DbType"/> values.</para></param>
|
||||
/// <remarks>
|
||||
/// <para>This version of the method is used when you can have
|
||||
/// the same parameter object multiple times with different values.</para>
|
||||
/// </remarks>
|
||||
public override void AddInParameter(string name, DbType dbType)
|
||||
{
|
||||
AddParameter(name, dbType, ParameterDirection.Input, String.Empty, DataRowVersion.Default, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Adds a new instance of an <see cref="SqlParameter"/> object to the command set as <see cref="ParameterDirection"/> value of Input.</para>
|
||||
/// </summary>
|
||||
/// <param name="name"><para>The name of the parameter.</para></param>
|
||||
/// <param name="dbType"><para>One of the <see cref="DbType"/> values.</para></param>
|
||||
/// <param name="value"><para>The value of the parameter.</para></param>
|
||||
public override void AddInParameter(string name, DbType dbType, object value)
|
||||
{
|
||||
AddParameter(name, dbType, ParameterDirection.Input, String.Empty, DataRowVersion.Default, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used for special handling of large string values
|
||||
/// This is used for efficiency in MSSQL so that text type fields
|
||||
/// are ensured to not point to an address on an empty string
|
||||
/// <para>Adds a new instance of an <see cref="SqlParameter"/> object to the command set as <see cref="ParameterDirection"/> value of Input.</para>
|
||||
/// </summary>
|
||||
/// <param name="name"><para>The name of the parameter.</para></param>
|
||||
/// <param name="value"><para>The value of the parameter.</para></param>
|
||||
public override void AddLargeStringInParameter(string name, object value)
|
||||
{
|
||||
object o=value;
|
||||
if(o!=null && (string)o=="")
|
||||
o=null;
|
||||
AddParameter(name, DbType.String, ParameterDirection.Input, String.Empty, DataRowVersion.Default, o);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Adds a new instance of an <see cref="SqlParameter"/> object to the command set as <see cref="ParameterDirection"/> value of Input.</para>
|
||||
/// </summary>
|
||||
/// <param name="name"><para>The name of the parameter.</para></param>
|
||||
/// <param name="dbType"><para>One of the <see cref="DbType"/> values.</para></param>
|
||||
/// <param name="sourceColumn"><para>The name of the source column mapped to the DataSet and used for loading or returning the value.</para></param>
|
||||
/// <param name="sourceVersion"><para>One of the <see cref="DataRowVersion"/> values.</para></param>
|
||||
public override void AddInParameter(string name, DbType dbType, string sourceColumn, DataRowVersion sourceVersion)
|
||||
{
|
||||
AddParameter(name, dbType, 0, ParameterDirection.Input, true, 0, 0, sourceColumn, sourceVersion, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Returns the value of the parameter for the given <paramref name="name"/>.</para>
|
||||
/// </summary>
|
||||
/// <param name="name"><para>The name of the parameter to get the value.</para></param>
|
||||
/// <returns><para>The value of the parameter.</para></returns>
|
||||
public override object GetParameterValue(string name)
|
||||
{
|
||||
return this.command.Parameters[BuildParameterName(name)].Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Sets the value of a parameter for the given <paramref name="name"/>.</para>
|
||||
/// </summary>
|
||||
/// <param name="name"><para>The name of the parameter to set the value.</para></param>
|
||||
/// <param name="value"><para>The new value of the parameter.</para></param>
|
||||
public override void SetParameterValue(string name, object value)
|
||||
{
|
||||
this.command.Parameters[BuildParameterName(name)].Value = (value == null) ? DBNull.Value : value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// set parameter value by index for performance
|
||||
/// </summary>
|
||||
/// <param name="index"></param>
|
||||
/// <param name="value"></param>
|
||||
public override void SetParameterValue(int index, object value)
|
||||
{
|
||||
this.command.Parameters[index].Value = (value == null) ? DBNull.Value : value;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// <para>Clean up resources.</para>
|
||||
/// </summary>
|
||||
public override void Dispose()
|
||||
{
|
||||
this.command.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Dicover the parameters for a stored procedure using a separate connection and command.</para>
|
||||
/// </summary>
|
||||
/// <param name="parameterToken"><para>The parameter delimeter for database commands.</para></param>
|
||||
protected override void DoDiscoverParameters(char parameterToken)
|
||||
{
|
||||
this.parameterToken = parameterToken;
|
||||
using (SqlCommand newCommand = CreateNewCommandAndConnectionForDiscovery())
|
||||
{
|
||||
SqlCommandBuilder.DeriveParameters(newCommand);
|
||||
|
||||
foreach (IDataParameter parameter in newCommand.Parameters)
|
||||
{
|
||||
IDataParameter cloneParameter = (IDataParameter)((ICloneable)parameter).Clone();
|
||||
cloneParameter.ParameterName = BuildParameterName(cloneParameter.ParameterName);
|
||||
this.command.Parameters.Add(cloneParameter);
|
||||
}
|
||||
newCommand.Connection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Assign the values provided by a user to the command parameters discovered in positional order.</para>
|
||||
/// </summary>
|
||||
/// <exception cref="InvalidOperationException">
|
||||
/// <para>The number of parameters does not match number of values for stored procedure.</para>
|
||||
/// </exception>
|
||||
protected override void DoAssignParameterValues()
|
||||
{
|
||||
if (SameNumberOfParametersAndValues() == false)
|
||||
{
|
||||
throw new InvalidOperationException(SR.ExceptionMessageParameterMatchFailure);
|
||||
}
|
||||
|
||||
int returnParameter = 1;
|
||||
for (int i = 0; i < this.parameterValues.Length; i++)
|
||||
{
|
||||
IDataParameter parameter = this.command.Parameters[i + returnParameter];
|
||||
|
||||
// There used to be code here that checked to see if the parameter was input or input/output
|
||||
// before assigning the value to it. We took it out because of an operational bug with
|
||||
// deriving parameters for a stored procedure. It turns out that output parameters are set
|
||||
// to input/output after discovery, so any direction checking was unneeded. Should it ever
|
||||
// be needed, it should go here, and check that a parameter is input or input/output before
|
||||
// assigning a value to it.
|
||||
SetParameterValue(parameter.ParameterName, this.parameterValues[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Determine if a stored procedure is using parameter discovery.</para>
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// <para><see langword="true"/> if further preparation is needed.</para>
|
||||
/// </returns>
|
||||
protected override bool DoIsFurtherPreparationNeeded()
|
||||
{
|
||||
return this.needsParameters;
|
||||
}
|
||||
|
||||
/// <devdoc>
|
||||
/// Create a parameter.
|
||||
/// </devdoc>
|
||||
private SqlParameter CreateParameter(string name, DbType type, int size, ParameterDirection direction, bool nullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value)
|
||||
{
|
||||
SqlParameter param = this.command.CreateParameter();
|
||||
param.ParameterName = BuildParameterName(name);
|
||||
|
||||
param.DbType = type;
|
||||
param.Size = size;
|
||||
param.Direction = direction;
|
||||
param.IsNullable = nullable;
|
||||
param.Precision = precision;
|
||||
param.Scale = scale;
|
||||
param.SourceColumn = sourceColumn;
|
||||
param.SourceVersion = sourceVersion;
|
||||
param.Value = (value == null) ? DBNull.Value : value;
|
||||
|
||||
if ((type.Equals(DbType.Object)) && (value is byte[]))
|
||||
{
|
||||
param.SqlDbType = SqlDbType.Image;
|
||||
}
|
||||
else if((type.Equals(DbType.Guid)) && value!=null && ((Guid)value==Guid.Empty))
|
||||
{
|
||||
//Force Guid empty to save as dbnull instead
|
||||
//so that we can use referential integrity properly
|
||||
//on related tables with optional guid foriegn key values
|
||||
param.Value=DBNull.Value;
|
||||
}
|
||||
|
||||
|
||||
return param;
|
||||
}
|
||||
|
||||
private bool SameNumberOfParametersAndValues()
|
||||
{
|
||||
int returnParameterCount = 1;
|
||||
int numberOfParametersToStoredProcedure = this.command.Parameters.Count - returnParameterCount;
|
||||
int numberOfValuesProvidedForStoredProcedure = this.parameterValues.Length;
|
||||
return numberOfParametersToStoredProcedure == numberOfValuesProvidedForStoredProcedure;
|
||||
}
|
||||
|
||||
/// <devdoc>
|
||||
/// Discovery has to be done on its own connection to allow for the case of the
|
||||
/// connection being used being enrolled in a transaction. The SqlCommandBuilder.DeriveParameters
|
||||
/// method creates a new SqlCommand internally to communicate to the database, and it
|
||||
/// reuses the same connection that is passed in on the command object. If this command
|
||||
/// object has a connection that is enrolled in a transaction, the DeriveParameters method does not
|
||||
/// honor that transaction, and the call fails. To avoid this, create your own connection and
|
||||
/// command, and use them.
|
||||
///
|
||||
/// You then have to clone each of the IDataParameter objects before it can be transferred to
|
||||
/// the original command, or another exception is thrown.
|
||||
/// </devdoc>
|
||||
private SqlCommand CreateNewCommandAndConnectionForDiscovery()
|
||||
{
|
||||
SqlConnection clonedConnection = (SqlConnection)((ICloneable)this.command.Connection).Clone();
|
||||
clonedConnection.Open();
|
||||
SqlCommand newCommand = CreateCommand(this.command.CommandText, this.command.CommandType);
|
||||
newCommand.Connection = clonedConnection;
|
||||
|
||||
return newCommand;
|
||||
}
|
||||
|
||||
private static SqlCommand CreateCommand(string commandText, CommandType commandType)
|
||||
{
|
||||
SqlCommand newCommand = new SqlCommand();
|
||||
newCommand.CommandText = commandText;
|
||||
newCommand.CommandType = commandType;
|
||||
|
||||
return newCommand;
|
||||
}
|
||||
|
||||
private string BuildParameterName(string name)
|
||||
{
|
||||
//Since we *always* specify parameters with the MSSQL @ token, for
|
||||
//sqlcommandwrapper this is pointless so just return it.
|
||||
return name;
|
||||
|
||||
// //System.Diagnostics.Debug.Assert(parameterToken != 0x0000);
|
||||
// if (name[0] != this.parameterToken)
|
||||
// {
|
||||
// return name.Insert(0, new string(this.parameterToken, 1));
|
||||
// }
|
||||
// return name;
|
||||
}
|
||||
}
|
||||
}
|
||||
306
source/Data/Data/Sql/SqlDatabase.cs
Normal file
306
source/Data/Data/Sql/SqlDatabase.cs
Normal file
@@ -0,0 +1,306 @@
|
||||
//===============================================================================
|
||||
// Microsoft patterns & practices Enterprise Library
|
||||
// Data Access Application Block
|
||||
//===============================================================================
|
||||
// Copyright © Microsoft Corporation. All rights reserved.
|
||||
// Adapted from ACA.NET with permission from Avanade Inc.
|
||||
// ACA.NET copyright © Avanade Inc. All rights reserved.
|
||||
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
|
||||
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
|
||||
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//===============================================================================
|
||||
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Data.SqlClient;
|
||||
using System.Xml;
|
||||
//using GZTW.Common;
|
||||
|
||||
namespace GZTW.Data.Sql
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>Represents a Sql Server Database.</para>
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Internally uses Sql Server .NET Managed Provider from Microsoft (System.Data.SqlClient) to connect to the database.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public class SqlDatabase : GZTWDatabase
|
||||
{
|
||||
/// <summary>
|
||||
/// Initialize a new instance of the <see cref="SqlDatabase"/> class.
|
||||
/// </summary>
|
||||
public SqlDatabase() : base()
|
||||
{
|
||||
}
|
||||
|
||||
public override void ClearPool()
|
||||
{
|
||||
//not implemented, for firebird only for now
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the DataBaseType enum for the active db type
|
||||
/// Used by higher layers when some DB specific code needs to be
|
||||
/// executed
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <para>DataBaseType enumeration value</para>
|
||||
/// </value>
|
||||
public override DataBaseType DBType
|
||||
{
|
||||
get
|
||||
{
|
||||
return DataBaseType.MSSQL;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the schema fingerprint for database
|
||||
/// </summary>
|
||||
public override string SchemaFingerPrint
|
||||
{
|
||||
get
|
||||
{
|
||||
string _fingerprint = "";
|
||||
using (SqlConnection cn = new SqlConnection(ConnectionString))
|
||||
{
|
||||
try
|
||||
{
|
||||
cn.Open();
|
||||
System.Text.StringBuilder sb = new System.Text.StringBuilder();
|
||||
|
||||
DataTable t = new DataTable();
|
||||
new SqlDataAdapter("select name from sys.tables order by name", cn).Fill(t);
|
||||
foreach (DataRow drTable in t.Rows)
|
||||
{
|
||||
//My development database has this table in it for some reason
|
||||
//should be ignored
|
||||
if (drTable[0].ToString() == "sysdiagrams" || drTable[0].ToString().ToLowerInvariant().Contains("dtproperties")) continue;
|
||||
|
||||
sb.Append(drTable[0].ToString());
|
||||
sb.Append("\r\n");
|
||||
|
||||
DataTable tColumns = new DataTable();
|
||||
new SqlDataAdapter("SELECT c.name AS column_name, t.name AS type_name " +
|
||||
"FROM sys.columns AS c " +
|
||||
"JOIN sys.types AS t ON c.user_type_id=t.user_type_id " +
|
||||
"WHERE c.object_id = OBJECT_ID('"+ drTable[0].ToString()+"') " +
|
||||
"ORDER BY c.name; ", cn).Fill(tColumns);
|
||||
|
||||
foreach (DataRow drColumn in tColumns.Rows)
|
||||
{
|
||||
sb.Append("\t");
|
||||
sb.Append(drColumn["column_name"].ToString());
|
||||
//sb.Append(" ");
|
||||
//sb.Append(drColumn["type_name"].ToString());
|
||||
sb.Append("\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
_fingerprint = sb.ToString().Trim();
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (cn != null)
|
||||
cn.Close();
|
||||
}
|
||||
}
|
||||
|
||||
return _fingerprint;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Gets the parameter token used to delimit parameters for the Sql Database.</para>
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <para>The '@' symbol.</para>
|
||||
/// </value>
|
||||
protected override char ParameterToken
|
||||
{
|
||||
get { return '@'; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Get the connection for this database.</para>
|
||||
/// <seealso cref="IDbConnection"/>
|
||||
/// <seealso cref="SqlConnection"/>
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// <para>The <see cref="SqlConnection"/> for this database.</para>
|
||||
/// </returns>
|
||||
public override IDbConnection GetConnection()
|
||||
{
|
||||
return new SqlConnection(ConnectionString);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Create a <see cref="SqlCommandWrapper"/> for a stored procedure.</para>
|
||||
/// </summary>
|
||||
/// <param name="storedProcedureName"><para>The name of the stored procedure.</para></param>
|
||||
/// <returns><para>The <see cref="SqlCommandWrapper"/> for the stored procedure.</para></returns>
|
||||
/// <exception cref="ArgumentNullException">
|
||||
/// <para><paramref name="storedProcedureName"/> can not be <see langword="null"/> (Nothing in Visual Basic).</para>
|
||||
/// </exception>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// <para><paramref name="storedProcedureName"/> hast not been initialized.</para>
|
||||
/// </exception>
|
||||
// public override DBCommandWrapper GetStoredProcCommandWrapper(string storedProcedureName)
|
||||
// {
|
||||
// ArgumentValidation.CheckForNullReference(storedProcedureName, "storedProcedureName");
|
||||
// ArgumentValidation.CheckForEmptyString(storedProcedureName, "storedProcedureName");
|
||||
//
|
||||
// return new SqlCommandWrapper(storedProcedureName, CommandType.StoredProcedure, ParameterToken);
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// <para>Create an <see cref="SqlCommandWrapper"/> for a stored procedure.</para>
|
||||
/// </summary>
|
||||
/// <param name="storedProcedureName"><para>The name of the stored procedure.</para></param>
|
||||
/// <param name="parameterValues"><para>The list of parameters for the procedure.</para></param>
|
||||
/// <returns><para>The <see cref="SqlCommandWrapper"/> for the stored procedure.</para></returns>
|
||||
/// <remarks>
|
||||
/// <para>The parameters for the stored procedure will be discovered and the values are assigned in positional order.</para>
|
||||
/// </remarks>
|
||||
/// <exception cref="ArgumentNullException">
|
||||
/// <para><paramref name="storedProcedureName"/> can not be <see langword="null"/> (Nothing in Visual Basic).</para>
|
||||
/// <para>- or -</para>
|
||||
/// <para><paramref name="parameterValues"/> can not be <see langword="null"/> (Nothing in Visual Basic).</para>
|
||||
/// </exception>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// <para><paramref name="storedProcedureName"/> hast not been initialized.</para>
|
||||
/// </exception>
|
||||
// public override DBCommandWrapper GetStoredProcCommandWrapper(string storedProcedureName, params object[] parameterValues)
|
||||
// {
|
||||
// ArgumentValidation.CheckForNullReference(storedProcedureName, "storedProcedureName");
|
||||
// ArgumentValidation.CheckForEmptyString(storedProcedureName, "storedProcedureName");
|
||||
// ArgumentValidation.CheckForNullReference(parameterValues, "parameterValues");
|
||||
//
|
||||
// return new SqlCommandWrapper(storedProcedureName, CommandType.StoredProcedure, ParameterToken, parameterValues);
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// <para>Create an <see cref="SqlCommandWrapper"/> for a SQL query.</para>
|
||||
/// </summary>
|
||||
/// <param name="query"><para>The text of the query.</para></param>
|
||||
/// <returns><para>The <see cref="SqlCommandWrapper"/> for the SQL query.</para></returns>
|
||||
/// <exception cref="ArgumentNullException">
|
||||
/// <para><paramref name="query"/> can not be <see langword="null"/> (Nothing in Visual Basic).</para>
|
||||
/// </exception>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// <para><paramref name="query"/> hast not been initialized.</para>
|
||||
/// </exception>
|
||||
public override DBCommandWrapper GetSqlStringCommandWrapper(string query)
|
||||
{
|
||||
ArgumentValidation.CheckForNullReference(query, "query");
|
||||
ArgumentValidation.CheckForEmptyString(query, "query");
|
||||
|
||||
return new SqlCommandWrapper(query, CommandType.Text, ParameterToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Create a <see cref="SqlDataAdapter"/> with the given update behavior and connection.</para>
|
||||
/// </summary>
|
||||
/// <param name="updateBehavior">
|
||||
/// <para>One of the <see cref="UpdateBehavior"/> values.</para>
|
||||
/// </param>
|
||||
/// <param name="connection">
|
||||
/// <para>The open connection to the database.</para>
|
||||
/// </param>
|
||||
/// <returns>An <see cref="SqlDataAdapter"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException">
|
||||
/// <para><paramref name="connection"/> can not be <see langword="null"/> (Nothing in Visual Basic).</para>
|
||||
/// </exception>
|
||||
protected override DbDataAdapter GetDataAdapter(UpdateBehavior updateBehavior, IDbConnection connection)
|
||||
{
|
||||
string queryStringToBeFilledInLater = String.Empty;
|
||||
SqlDataAdapter adapter = new SqlDataAdapter(queryStringToBeFilledInLater, (SqlConnection)connection);
|
||||
|
||||
if (updateBehavior == UpdateBehavior.Continue)
|
||||
{
|
||||
adapter.RowUpdated += new SqlRowUpdatedEventHandler(OnSqlRowUpdated);
|
||||
}
|
||||
return adapter;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Executes the <see cref="SqlCommandWrapper"/> and returns an <see cref="XmlReader"/>.</para>
|
||||
/// </summary>
|
||||
/// <param name="command">
|
||||
/// <para>The <see cref="SqlCommandWrapper"/> to execute.</para>
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// <para>An <see cref="XmlReader"/> object.</para>
|
||||
/// </returns>
|
||||
public XmlReader ExecuteXmlReader(SqlCommandWrapper command)
|
||||
{
|
||||
IDbConnection connection = OpenConnection();
|
||||
PrepareCommand(command, connection);
|
||||
SqlCommand sqlCommand = command.Command as SqlCommand;
|
||||
return DoExecuteXmlReader(sqlCommand);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Executes the <see cref="SqlCommandWrapper"/> in a transaction and returns an <see cref="XmlReader"/>.</para>
|
||||
/// </summary>
|
||||
/// <param name="command">
|
||||
/// <para>The <see cref="SqlCommandWrapper"/> to execute.</para>
|
||||
/// </param>
|
||||
/// <param name="transaction">
|
||||
/// <para>The <see cref="IDbTransaction"/> to execute the command within.</para>
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// <para>An <see cref="XmlReader"/> object.</para>
|
||||
/// </returns>
|
||||
public XmlReader ExecuteXmlReader(SqlCommandWrapper command, IDbTransaction transaction)
|
||||
{
|
||||
PrepareCommand(command, transaction);
|
||||
|
||||
SqlCommand sqlCommand = command.Command as SqlCommand;
|
||||
return DoExecuteXmlReader(sqlCommand);
|
||||
}
|
||||
|
||||
/// <devdoc>
|
||||
/// Execute the actual Xml Reader call.
|
||||
/// </devdoc>
|
||||
private XmlReader DoExecuteXmlReader(SqlCommand sqlCommand)
|
||||
{
|
||||
try
|
||||
{
|
||||
DateTime startTime = DateTime.Now;
|
||||
XmlReader reader = sqlCommand.ExecuteXmlReader();
|
||||
//Instrumentation.CommandExecuted(startTime);
|
||||
return reader;
|
||||
}
|
||||
catch
|
||||
{
|
||||
//Instrumentation.CommandFailed(sqlCommand.CommandText, ConnectionStringNoCredentials);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <devdoc>
|
||||
/// Listens for the RowUpdate event on a dataadapter to support UpdateBehavior.Continue
|
||||
/// </devdoc>
|
||||
private void OnSqlRowUpdated(object sender, SqlRowUpdatedEventArgs rowThatCouldNotBeWritten)
|
||||
{
|
||||
if (rowThatCouldNotBeWritten.RecordsAffected == 0)
|
||||
{
|
||||
if (rowThatCouldNotBeWritten.Errors != null)
|
||||
{
|
||||
rowThatCouldNotBeWritten.Row.RowError = SR.ExceptionMessageUpdateDataSetRowFailure;
|
||||
rowThatCouldNotBeWritten.Status = UpdateStatus.SkipCurrentRow;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user