⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 database_base.atcs

📁 .net samples
💻 ATCS
字号:
<#@ Imports
using System;
using TwoLKit.nTierBuilder.Api.DbDom;
##>
<#@ InstanceMembers
	private IDatabase Database
	{
		get { return (IDatabase)Environment["Database"]; }
	}

	public override string RelativeFilePath
	{
		get { return GetClassName() + ".vb"; }
	}

	private string GetClassName()
	{
		return Database.CodeName + "_Base";
	}

	private string GetUserClassName()
	{
		return Database.CodeName;
	}
	
	private string GetTableClassName(ITable table)
	{
		return table.CodeName + "Collection";
	}
	
	private string GetTableFieldName(ITable table)
	{
		return SharedUtils.GetFieldName(table.CodeName);
	}

	private string GetNamespace()
	{
		return SharedUtils.GetDbTierNamespace(Database);
	}
##>
' <fileinfo name="<#= RelativeFilePath #>">
'		<copyright owner="<#= Environment["CopyrightOwner"] #>">
'			All Rights Reserved.
'		</copyright>
'		<remarks>
'			Do not change this source code manually. Changes to this file may 
'			cause incorrect behavior and will be lost if the code is regenerated.
'		</remarks>
'		<generator rewritefile="<#= RewriteExistingFile #>" infourl="http://www.2LKit.com">2LKit nTier Builder</generator>
'		<created><#= DateTime.Now.ToString("f") #></created>
' </fileinfo>

Option Strict Off
Option Explicit On

Imports System
Imports System.Data

'' <summary>
'' The base class for the <#= GetUserClassName() #> class that represents the <#= Database.CodeName #> database. 
'' Do not change this source code. Update the <#= GetUserClassName() #> class if you need
'' to add or change some functionality.
'' </summary>
Public MustInherit Class <#= GetClassName() #>
		Implements IDisposable
	Private _connection As IDbConnection
	Private _transaction As IDbTransaction

	' Table fields
<#	foreach(ITable table in Database.Tables)
	{ ##>
	Private <#= GetTableFieldName(table) #> As <#= GetTableClassName(table) #>
<#	} ##>

	'' <summary>
	'' Initializes a new instance of the <#= GetClassName() #> class.
	'' </summary>
	Protected Sub New()
		_connection = CreateConnection()
		_connection.Open()
	End Sub

	'' <summary>
	'' Creates a new connection to the database.
	'' </summary>
	'' <returns>An IDbConnection object.</returns>
	Protected MustOverride Function CreateConnection() As IDbConnection

	'' <summary>
	'' Returns a data provider specific SQL statement parameter name. For example it 
	'' returns ? for OleDb provider, or @paramName for MS SQL provider.
	'' </summary>
	'' <param name="paramName">The data provider neutral SQL parameter name.</param>
	Protected Friend MustOverride Function CreateSqlParameterName(paramName As String) As String

	'' <summary>
	'' Adds a new parameter to the specified command.
	'' </summary>
	'' <param name="cmd">An IDbCommand object to add the parameter.</param>
	'' <param name="paramName">The name of the parameter.</param>
	'' <param name="dbType">One of the DbType values. </param>
	'' <param name="value">The value of the parameter.</param>
	'' <returns>A reference to the added parameter.</returns>
	Friend Function AddParameter(cmd As IDbCommand, paramName As String, _
									dbType As DbType, value As Object) As IDataParameter
		Dim parameter As IDataParameter = cmd.CreateParameter()
		parameter.ParameterName = CreateCollectionParameterName(paramName)
		parameter.DbType = dbType
        If value Is Nothing Then
            parameter.Value = DBNull.Value
        Else
            parameter.Value = value
        End If
		cmd.Parameters.Add(parameter)
		return parameter
	End Function
	
	'' <summary>
	'' Creates a .Net data provider specific name that is used by the 
	'' AddParameter method.
	'' </summary>
	'' <param name="baseParamName">The base name of the parameter.</param>
	'' <returns>The full data provider specific parameter name.</returns>
	Protected MustOverride Function CreateCollectionParameterName(baseParamName As String) As String

	'' <summary>
	'' Gets IDbConnection associated with this object.
	'' </summary>
	'' <value>An IDbConnection object.</value>
	Public ReadOnly Property Connection As IDbConnection
		Get
			Return _connection
		End Get
	End Property

<#	foreach(ITable table in Database.Tables)
	{ ##>
	'' <summary>
	'' Gets an object that represents the '<#= table.Name #>' table.
	'' </summary>
	'' <value>An <#= GetTableClassName(table) #> object.</value>
	Public ReadOnly Property <#= GetTableClassName(table) #> As <#= GetTableClassName(table) #>
		Get
			If <#= GetTableFieldName(table) #> Is Nothing Then
				<#= GetTableFieldName(table) #> = new <#= GetTableClassName(table) #>(CType(Me, <#= Database.CodeName #>))
			End If
			Return <#= GetTableFieldName(table) #>
		End Get
	End Property

<#	} ##>
	'' <summary>
	'' Begins a database transaction.
	'' </summary>
	'' <returns>An object representing the new transaction.</returns>
	Public Function BeginTransaction() As IDbTransaction
		CheckTransactionState(false)
		_transaction = _connection.BeginTransaction()
		Return _transaction
	End Function

	'' <summary>
	'' Begins a database transaction with the specified 
	'' transaction isolation level.
	'' </summary>
	'' <param name="isolationLevel">The transaction isolation level.</param>
	'' <returns>An object representing the new transaction.</returns>
	Public Function BeginTransaction(isolationLevel As IsolationLevel) As IDbTransaction
		CheckTransactionState(false)
		_transaction = _connection.BeginTransaction(isolationLevel)
		Return _transaction
	End Function

	'' <summary>
	'' Commits the current database transaction.
	'' </summary>
	Public Sub CommitTransaction()
		CheckTransactionState(true)
		_transaction.Commit()
		_transaction = Nothing
	End Sub

	'' <summary>
	'' Rolls back the current transaction from a pending state.
	'' </summary>
	Public Sub RollbackTransaction()
		CheckTransactionState(true)
		_transaction.Rollback()
		_transaction = Nothing
	End Sub

	' Checks the state of the current transaction
	Private Sub CheckTransactionState(mustBeOpen As Boolean)
		If mustBeOpen Then
			If _transaction Is Nothing Then
				Throw New InvalidOperationException("Transaction is not open.")
			End If
		Else
			If Not (_transaction Is Nothing) Then
				Throw New InvalidOperationException("Transaction is already open.")
			End If
		End If
	End Sub

	'' <summary>
	'' Creates and returns a new IDbCommand object.
	'' </summary>
	'' <param name="sqlText">The text of the query.</param>
	'' <returns>An IDbCommand object.</returns>
	Friend Function CreateCommand(sqlText As String) As IDbCommand
		Return CreateCommand(sqlText, false)
	End Function

	'' <summary>
	'' Creates and returns a new IDbCommand object.
	'' </summary>
	'' <param name="sqlText">The text of the query.</param>
	'' <param name="procedure">Specifies if the sqlText parameter is 
	'' the name of a stored procedure.</param>
	'' <returns>An IDbCommand object.</returns>
	Friend Function CreateCommand(sqlText As String, procedure As Boolean) As IDbCommand
		Dim cmd As IDbCommand = _connection.CreateCommand()
		cmd.CommandText = sqlText
		If procedure Then
			cmd.CommandType = CommandType.StoredProcedure
		End If
		Return cmd
	End Function

	'' <summary>
	'' Rolls back any pending transactions and closes the DB connection.
	'' An application can call Close more than one time without 
	'' generating an exception.
	'' </summary>
	Public Overridable Sub Close()
		Dispose()
	End Sub

	'' <summary>
	'' Rolls back any pending transactions and closes the DB connection.
	'' </summary>
	Public Overridable Sub Dispose() Implements IDisposable.Dispose
		_connection.Close()
	End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -