📄 productsresultset.cs
字号:
namespace MobileDevelopersHandbook.ProductsResultSetResultSets
{
using System;
using System.Data;
using System.Data.SqlServerCe;
partial class ProductResultSet : IDisposable
{
const string databaseFilename = "MyDatabase.sdf";
const string tableName = "Product";
/// <summary>
/// Construct the ResultSet instance initializing the ResultSetOptions and connection string
/// optionally opening the table. Calling this method with an openTable value of true provides
/// the same behavior as using the default constructor.
/// </summary>
/// <param name="openTable"></param>
public ProductResultSet(bool openTable)
{
InitializeResultSetOptions();
InitializeResultSetConnectionString();
if (openTable)
this.Open();
}
/// <summary>
/// Construct the ResultSet instance initializing the ResultSetOptions and connection string
/// optionally opening the table. Calling this method with an openTable value of true provides
/// the same behavior as using the default constructor.
/// </summary>
/// <param name="openTable"></param>
/// <param name="connString">Connection string to the database</param>
public ProductResultSet(bool openTable, string connString)
{
InitializeResultSetOptions();
resultSetConnectionString = connString;
if (openTable)
this.Open();
}
/// <summary>
/// Use only with TableDirect mode. ResultSet will contain those records
/// </summary>
public void OpenEx()
{
using (SqlCeCommand sqlCeSelectCommand = CreateConnectionAndCommand())
{
sqlCeSelectCommand.CommandText = tableName;
sqlCeSelectCommand.CommandType = System.Data.CommandType.TableDirect;
sqlCeSelectCommand.ExecuteResultSet(ResultSetOptions, this);
}
}
/// <summary>
/// Make connection string value publicly visible
/// </summary>
public string ResultSetConnectionString
{
get { return resultSetConnectionString; }
}
/// <summary>
/// Make options publicly visible
/// </summary>
public ResultSetOptions ResultSetOptions
{
get { return resultSetOptions; }
}
#region InitializeResultSetConnectionString, InitializeResultSetOptions, CreateConnectionAndCommand
/// <summary>
/// Set the resultSetConnectionString member variable to the default connection string.
/// </summary>
protected void InitializeResultSetConnectionString()
{
if (resultSetConnectionString == null)
{
resultSetConnectionString = ("Data Source ="
+ (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
+ "\\" + databaseFilename + ";"));
}
}
/// <summary>
/// Set the resultSetOptions member variable to the default values. By default it is set
/// to support scrolling, updates and be sensitive to changes in the datasource.
/// </summary>
protected void InitializeResultSetOptions()
{
resultSetOptions = System.Data.SqlServerCe.ResultSetOptions.Scrollable;
resultSetOptions = (resultSetOptions | System.Data.SqlServerCe.ResultSetOptions.Sensitive);
resultSetOptions = (resultSetOptions | System.Data.SqlServerCe.ResultSetOptions.Updatable);
}
/// <summary>
/// Creates a connection, opens it and factories a command instance from the connection
/// </summary>
/// <returns></returns>
protected SqlCeCommand CreateConnectionAndCommand()
{
sqlCeConnection = new System.Data.SqlServerCe.SqlCeConnection(ResultSetConnectionString);
sqlCeConnection.Open();
return sqlCeConnection.CreateCommand();
}
#endregion
#region IDisposable Members
public new void Dispose()
{
if (this.Connection != null)
{
this.Connection.Dispose();
}
base.Dispose();
}
#endregion
}
partial class ProductCategoryResultSet : IDisposable
{
const string databaseFilename = "MyDatabase.sdf";
const string tableName = "ProductCategory";
/// <summary>
/// Construct the ResultSet instance initializing the ResultSetOptions and connection string
/// optionally opening the table. Calling this method with an openTable value of true provides
/// the same behavior as using the default constructor.
/// </summary>
/// <param name="openTable"></param>
public ProductCategoryResultSet(bool openTable)
{
InitializeResultSetOptions();
InitializeResultSetConnectionString();
if (openTable)
this.Open();
}
/// <summary>
/// Construct the ResultSet instance initializing the ResultSetOptions and connection string
/// optionally opening the table. Calling this method with an openTable value of true provides
/// the same behavior as using the default constructor.
/// </summary>
/// <param name="openTable"></param>
/// <param name="connString">Connection string to the database</param>
public ProductCategoryResultSet(bool openTable, string connString)
{
InitializeResultSetOptions();
resultSetConnectionString = connString;
if (openTable)
this.Open();
}
/// <summary>
/// Use only with TableDirect mode. ResultSet will contain those records
/// </summary>
public void OpenEx()
{
using (SqlCeCommand sqlCeSelectCommand = CreateConnectionAndCommand())
{
sqlCeSelectCommand.CommandText = tableName;
sqlCeSelectCommand.CommandType = System.Data.CommandType.TableDirect;
sqlCeSelectCommand.ExecuteResultSet(ResultSetOptions, this);
}
}
/// <summary>
/// Make connection string value publicly visible
/// </summary>
public string ResultSetConnectionString
{
get { return resultSetConnectionString; }
}
/// <summary>
/// Make options publicly visible
/// </summary>
public ResultSetOptions ResultSetOptions
{
get { return resultSetOptions; }
}
#region InitializeResultSetConnectionString, InitializeResultSetOptions, CreateConnectionAndCommand
/// <summary>
/// Set the resultSetConnectionString member variable to the default connection string.
/// </summary>
protected void InitializeResultSetConnectionString()
{
if (resultSetConnectionString == null)
{
resultSetConnectionString = ("Data Source ="
+ (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
+ "\\" + databaseFilename + ";"));
}
}
/// <summary>
/// Set the resultSetOptions member variable to the default values. By default it is set
/// to support scrolling, updates and be sensitive to changes in the datasource.
/// </summary>
protected void InitializeResultSetOptions()
{
resultSetOptions = System.Data.SqlServerCe.ResultSetOptions.Scrollable;
resultSetOptions = (resultSetOptions | System.Data.SqlServerCe.ResultSetOptions.Sensitive);
resultSetOptions = (resultSetOptions | System.Data.SqlServerCe.ResultSetOptions.Updatable);
}
/// <summary>
/// Creates a connection, opens it and factories a command instance from the connection
/// </summary>
/// <returns></returns>
protected SqlCeCommand CreateConnectionAndCommand()
{
sqlCeConnection = new System.Data.SqlServerCe.SqlCeConnection(ResultSetConnectionString);
sqlCeConnection.Open();
return sqlCeConnection.CreateCommand();
}
#endregion
#region IDisposable Members
public new void Dispose()
{
if (this.Connection != null)
{
this.Connection.Dispose();
}
base.Dispose();
}
#endregion
}
}
partial class ProductsResultSet
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -