SqlClient ClassNemiro.Data.dll
SQL Server client class.
Inheritance Hierarchy

System Object
  Nemiro.Data.Sql SqlClient

Namespace: Nemiro.Data.Sql
Assembly: Nemiro.Data (in Nemiro.Data.dll) Version: 2.11.4.126 (2.11.4.126)
Syntax

public class SqlClient : IDisposable

The SqlClient type exposes the following members.

Constructors

  NameDescription
Public methodSqlClient 
Initializes a new instance of the SqlClient class.
Public methodSqlClient(Int32)
Initializes a new instance of the SqlClient class with caching duration.
Public methodSqlClient(String)
Initializes a new instance of the SqlClient class.
Public methodSqlClient(String, Int32)
Initializes a new instance of the SqlClient class.
Public methodSqlClient(String, String)
Initializes a new instance of the SqlClient class.
Public methodSqlClient(Type, Int32)
Initializes a new instance of the SqlClient class with custom cache object and caching duration.
Public methodSqlClient(Type,  Object )
Initializes a new instance of the SqlClient with custom cache object.
Public methodSqlClient(String, Type,  Object )
Initializes a new instance of the SqlClient class.
Public methodSqlClient(Type,  Object , Int32)
Initializes a new instance of the SqlClient class.
Public methodSqlClient(String, Int32, String, Type,  Object )
Initializes a new instance of the SqlClient class.
Public methodSqlClient(String, Int32, String, Type,  Object , Type)
Initializes a new instance of the SqlClient class.
Top
Methods

  NameDescription
Public methodConnect
Opens a database connection.
Public methodCopyDataToServer
Transferring the specified tables from the instance of DataSet to the database. Used SqlBulkCopy. The fastest and most economical way to move large amounts of data in the database.
Public methodCopyTableToServer
Transferring the specified table to the database. Used SqlBulkCopy. The fastest and most economical way to move large amounts of data in the database.
Public methodDisconnect
Closes the database connection.
Public methodDispose
Releases all resources used by the object. If necessary, closing all opened connections to the database.
Public methodEquals (Inherited from Object.)
Public methodExecuteNonQuery 
Executes a Transact-SQL statement against the connection and returns the number of rows affected. This method is used in conjunction with the properties CommandType, CommandText and Parameters.
Public methodExecuteNonQuery(SqlCommand)
Executes the specified SqlCommand and returns the number of rows affected.
Public methodExecuteNonQuery(String)
Executes a Transact-SQL statement against the connection and returns the number of rows affected.
Public methodExecuteScalar 
Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored. This method is used in conjunction with the properties CommandType, CommandText and Parameters.
Public methodExecuteScalar(SqlCommand)
Executes the specified SqlCommand, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.
Public methodExecuteScalar(String)
Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.
Protected methodFinalize (Inherited from Object.)
Public methodGetData 
Executes the query and returns DataSet. This method is used in conjunction with the properties CommandType, CommandText and Parameters.
Public methodGetData(SqlCommand)
Executes the specified SqlCommand and returns DataSet.
Public methodGetData(String)
Executes the specified SQL-statement and returns DataSet.
Public methodGetHashCode (Inherited from Object.)
Public methodGetRow 
Executes the query and returns data row. This method is used in conjunction with the properties CommandType, CommandText and Parameters.
Public methodGetRow(SqlCommand)
Executes the specified SqlCommand and returns строку данных.
Public methodGetRow(String)
Executes the query and returns data row.
Public methodGetTable 
Executes the query and returns DataTable. This method is used in conjunction with the properties CommandType, CommandText and Parameters.
Public methodGetTable(SqlCommand)
Executes the specified SqlCommand and returns DataTable.
Public methodGetTable(String)
Executes the query and returns DataTable.
Public methodGetType (Inherited from Object.)
Protected methodMemberwiseClone (Inherited from Object.)
Public methodToString (Inherited from Object.)
Top
Properties

  NameDescription
Protected propertyCache
Provides access to parameters caching.
Public propertyCacheAccessTimeout
Waiting time gain access to the entry of file cache (File). After this time, if the file entry are not available, an exception is thrown.
Public propertyCacheBinder
Type serializer of custom objects.
Public propertyCacheBufferAccessTimeout
Waiting time gain access to the buffer of file cache (File).
Public propertyCacheBufferSize
File cache (File) memory buffer size (Kb). Default value is 2048 Kb (2 Mb).
Public propertyCacheCustom
Custom cache type. Used with CacheType and CacheCustomArgs.
Public propertyCacheCustomArgs
Array of additional parameters for initialization custom cache. Used with CacheType and CacheCustom.
Public propertyCacheDuration
Duration of storage in the cache entry (in seconds).
Public propertyCacheName
Name of the key cache entry (optional).
Public propertyCachePath
The path of storage a cache files. Using only with CacheType = File.
Public propertyCacheType
Cache type. Default value is Auto, unless otherwise specified in the parameter NeData:Sql:CacheType of configuration files.
Public propertyCommandText
The text of the SQL statements or stored procedure name, you must perform.
Public propertyCommandTimeout
Timeout command (in seconds). The default value is 30 seconds.
Public propertyCommandType
Command type for CommandText. Default value Auto, unless otherwise specified in the parameter NeData:Sql:CommandType of configuration file.
Public propertyConnectionMode
Connection mode. Default value Auto, unless otherwise specified in the parameter NeData:Sql:ConnectionMode of configuration file.
Public propertyConnectionString
Connection string. Default value: LocalSqlServer.
Public propertyConnectionTimeout
Timeout connecting to the server (in seconds) before an exception. Default value: zero - used the server configuration.
Public propertyLastQueryResultsFromCache
Is True, if the last query result was obtained from the cache.
Public propertyLastQueryTime
Elapsed time of the last query to the database.
Public propertyParameters
Collection of query parameters.
Top
Remarks

By default, the class initialization parameters are used in the configuration file. For web-projects - web.config; for applications Windows - app.config.

Following parameters are supported:

  • NeData:Sql:ConnectionMode
  • NeData:Sql:CommandType
  • NeData:Sql:CacheType
  • NeData:Sql:CacheDuration (not recommended)
  • NeData:Sql:CachePath
  • NeData:Sql:CacheBinder
  • NeData:Sql:CacheBufferSize
  • NeData:Sql:CacheBufferAccessTimeout (for example: 0.00:00:00.500 - 500 ms)
  • NeData:Sql:CacheAccessTimeout (for example: 0.00:00:00.500 - 500 ms)
  • NeData:Sql:CacheCustom
  • NeData:Sql:CacheCustomArgs

For Windows applications, the parameter names should be recorded without the colons (case sensitive). For example:

  • NeDataSqlCacheTyp
  • NeDataSqlAutoTrimmingStrings
  • NeDataSqlCachePath
  • etc.

The default connection string is LocalSqlServer. To use the LocalSqlServer in web applications with your database, you need remove default connection string and add your connection string value:

<remove name="LocalSqlServer"/> 
<add 
  name="LocalSqlServer" 
  connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=example;Trusted_Connection=True;" 
/>
Examples

The following example creates an instance of the SqlClient class using connection string by default (LocalSqlServer).

using (SqlClient client = new SqlClient())
{
  DataRow row = client.GetRow("SELECT * FROM table WHERE id = 1000");
  if(row == null)
  {
    Console.WriteLine("No results...");
  }
  else
  {
    Console.WriteLine("id = {0}", row["id"]);
  }
}

The following example creates an instance of the SqlClient class with an explicit connection string.

using (SqlClient client = new SqlClient(@"Data Source=.\SQLEXPRESS;Initial Catalog=example;Trusted_Connection=True;"))
{
  DataRow row = client.GetRow("SELECT * FROM table WHERE id = 1000");
  if(row == null)
  {
    Console.WriteLine("No results...");
  }
  else
  {
    Console.WriteLine("id = {0}", row["id"]);
  }
}

The following example creates an instance of the SqlClient class with the parameter name in the configuration file, containing the connection string.

using (SqlClient client = new SqlClient("MyConnectionString"))
{
  DataRow row = client.GetRow("SELECT * FROM table WHERE id = 1000");
  if(row == null)
  {
    Console.WriteLine("No results...");
  }
  else
  {
    Console.WriteLine("id = {0}", row["id"]);
  }
}

The following example demonstrates the use of the built-in caching mechanism.

using (SqlClient client = new SqlClient())
{
  // cache the query results via file cache for 30 sec.
  client.CacheDuration = 30;
  client.CacheType = CachingType.File;
  // query building
  client.CommandText = "SELECT * FROM table";
  // query execution
  DataTable table = client.GetTable();
  // output
  Console.WriteLine("Total rows: {0}", table.Rows.Count);
  Console.WriteLine("Query execution time: {0}", client.LastQueryTime);
  Console.WriteLine("From cache: {0}", client.LastQueryResultsFromCache);
  // repeated execution of the same query
  table = client.GetTable();
  Console.WriteLine("Total rows: {0}", table.Rows.Count);
  Console.WriteLine("Query execution time: {0}", client.LastQueryTime);
  Console.WriteLine("From cache: {0}", client.LastQueryResultsFromCache);
}
See Also