Easy to get data access layer [continued 2]

  'ClsDataAccessOper such that all of the data access of the father 

  'By YuJun 

  'Www.hahaIT.com 

  'Hahasoft@msn.com 

  Public Class clsDataAccessOper 

  'When Update, Delete, Add method of operation failed to return to False, wrong information recorded 

  Public Shared ModifyErrorString As String 

  Private Shared Keys As New Hashtable 

  'Database connection string 

  Public Shared Property ConnectionString () As String 

Get

  Return SqlHelper.cnnString.Trim 

  End Get 

  Set (ByVal Value As String) 

  SqlHelper.cnnString = Value.Trim 

  End Set 

  End Property 

  'Update did not update the main keys, including joint primary key 

  Public Shared Function Update (ByVal o As Object) As Boolean 

  ModifyErrorString = "" 

Try

  If CType (SqlHelper.ExecuteNonQuery (SqlHelper.cnnString, CommandType.Text, SQLBuilder.Exists (o)), Int64) = 0 Then 

  Throw New Exception ( "the record does not exist!") 

  End If 

  Catch ex As Exception 

  Throw ex 

  End Try 

Try

  SqlHelper.ExecuteNonQuery (SqlHelper.cnnString, CommandType.Text, SQLBuilder.Update (o)) 

  Catch ex As Exception 

  ModifyErrorString = ex.Message 

  Return False 

  End Try 

  Return True 

  End Function 

  'Delete will be neglected 

  Public Shared Function Delete (ByVal o As Object) As Boolean 

  ModifyErrorString = "" 

Try

  SqlHelper.ExecuteNonQuery (SqlHelper.cnnString, CommandType.Text, SQLBuilder.Delete (o)) 

  Catch ex As Exception 

  ModifyErrorString = ex.Message 

  Return False 

  End Try 

  Return True 

  End Function 

  'Add method will automatically ignore the added value of the primary key 

  Public Shared Function Add (ByVal o As Object) As Boolean 

  ModifyErrorString = "" 

Try

  SqlHelper.ExecuteNonQuery (SqlHelper.cnnString, CommandType.Text, SQLBuilder.Add (o)) 

  Catch ex As Exception 

  ModifyErrorString = ex.Message 

  Return False 

  End Try 

  Return True 

  End Function 

  'Universal Database Query Method 

  'Heavy methods specified for the operation of the database tables to name 

  'Otherwise ReturnType will describe the type of operation must be the name of the database table eg: ReturnType = "clsRooms" moral support TableName = "tbl_Rooms" 

  'This query methods will be added to the Keys for conditions (HashTable), and then call Select method returns a collection of objects 

  'When Keys contains special bond, will be dealing with the complex types of enquiries, see note SQLBuilder the ComplexSQL 

  'This method can develop data access methods for the fixed category 

  Public Overloads Shared Function [Select] (ByVal ReturnType As Type) As ArrayList 

  Dim tableName As String 

  TableName = ReturnType.Name 

  Dim i As Int16 

  I = tableName.IndexOf ( "cls") + 3 

  TableName = "tbl_" & tableName.Substring (i, tableName.Length - i) 

  Return [Select] (ReturnType, tableName) 

  End Function 

  Public Overloads Shared Function [Select] (ByVal ReturnType As Type, ByVal TableName As String) As ArrayList 

  Dim alOut As New ArrayList 

  Dim dsDB As New Data.DataSet 

  DsDB.ReadXml (clsPersistant.DBConfigPath) 

  Dim xxxH As New Hashtable 

  Dim eachRow As Data.DataRow 

  For Each eachRow In dsDB.Tables (TableName). Rows 

  If Keys.Contains (CType (eachRow.Item ( "name"), String). ToLower.Trim) Then 

  XxxH.Add (CType (eachRow.Item ( "dbname"), String). ToLower.Trim, Keys (CType (eachRow.Item ( "name"), String). Trim.ToLower)) 

  End If 

Next

  'Check the legality of Keys 

  Dim dsSelect As New Data.DataSet 

  If Keys.Count xxxH.Count Then <> 

  Keys.Clear () 

  Dim InvalidField As New Exception ( "Without you set the field:") 

  Throw InvalidField 

Else

  Keys.Clear () 

Try

  DsSelect = SqlHelper.ExecuteDataset (SqlHelper.cnnString, CommandType.Text, SQLBuilder.Select (xxxH, TableName)) 

  Catch ex As Exception 

  Throw ex 

  End Try 

  End If 

  Dim eachSelect As Data.DataRow 

  Dim fieldName As String 

  Dim DBfieldName As String 

  For Each eachSelect In dsSelect.Tables (0). Rows 

  Dim newObject As Object = System.Activator.CreateInstance (ReturnType) 

  For Each eachRow In dsDB.Tables (TableName). Rows 

  FieldName = CType (eachRow.Item ( "name"), String). Trim 

  DBfieldName = CType (eachRow.Item ( "dbname"), String). Trim 

  CallByName (newObject, fieldName, CallType.Set, CType (eachSelect.Item (DBfieldName), String). Trim) 

Next

  AlOut.Add (newObject) 

  Nothing newObject = 

Next

  Return alOut 

  End Function 

  Public Shared WriteOnly Property SelectKeys (ByVal KeyName As String) 

  Set (ByVal Value As Object) 

  Keys.Add (KeyName.Trim.ToLower, Value) 

  End Set 

  End Property 

  'Below four methods used to record movement 

  'Mobile record the size of the primary key sequence mobile, only to have only one primary key and the operating table 

  'The combination of the keys, to return to Nothing 

  'When records or mobile end Noting return at the end, when the table is empty, First, Last Nothing return all 

  Public Shared Function First (ByVal o As Object) As Object 

  Return Move ( "first" o) 

  End Function 

  Public Shared Function Last (ByVal o As Object) As Object 

  Return Move ( "last" o) 

  End Function 

  Public Shared Function Previous (ByVal o As Object) As Object 

  Return Move ( "previous" o) 

  End Function 

  Public Shared Function [Next] (ByVal o As Object) As Object 

  Return Move ( "next", o) 

  End Function 

  'Go back to the table the number of the main keys, keyName, keyDBName record is the last one primary key 

  Private Shared Function getKey (ByRef keyName As String, ByRef keyDBName As String, ByVal TableName As String) As Int16 

  Dim keyNum As Int16 = 0 

  Dim dsDB As New DataSet 

  DsDB.ReadXml (clsPersistant.DBConfigPath) 

  Dim row As Data.DataRow 

  For Each row In dsDB.Tables (TableName). Rows 

  If row.Item (the "key") = "1" Then 

  KeyNum keyNum + 1 = 

  KeyName = CType (row.Item ( "name"), String). Trim 

  KeyDBName = CType (row.Item ( "dbname"), String). Trim 

  Exit For 

  End If 

Next

  Return keyNum 

  End Function 

  'For the First, Previous, Next, Last provide a common function 

  Private Shared Function Move (ByVal Type As String, ByVal o As Object) As Object 

  Dim moveSQL As String 

  Select Case Type.Trim.ToLower 

  Case "first" 

  MoveSQL = SQLBuilder.First (o) 

  Case "last" 

  MoveSQL = SQLBuilder.Last (o) 

  Case "previous" 

  MoveSQL = SQLBuilder.Previous (o) 

  Case "next" 

  MoveSQL = SQLBuilder.Next (o) 

  End Select 

  Dim typeString As String = o.GetType.ToString 

  Dim i As Int16 

  I = typeString.IndexOf ( "cls") + 3 

  TypeString = "tbl_" & typeString.Substring (i, typeString.Length - i) 

  Dim TableName As String = typeString 

  Dim keyName As String 

  Dim keyDBName As String 

  Dim tmpString As String 

  If getKey (keyName, keyDBName, TableName) = 1 Then 

  Keys.Clear () 

  Dim ds As New Data.DataSet 

  Ds = SqlHelper.ExecuteDataset (SqlHelper.cnnString, CommandType.Text, moveSQL) 

  If ds.Tables (0). Rows.Count = 0 Then 

  Return Nothing 

Else

  TmpString = CType (ds.Tables (0). Rows (0). Item (keyDBName), String). Trim 

  Keys.Add (keyName.Trim.ToLower, tmpString) 

  Dim al As New ArrayList 

  Al = [Select] (o.GetType) 

  If al.Count = 1 Then 

  Return al.Item (0) 

Else

  Return Nothing 

  End If 

  End If 

Else

  Return Nothing 

  End If 

  End Function 

  End Class 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Releated Articles

  • Popuklar Articles

0 Comments to “Easy to get data access layer [continued 2]”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.