请让我明白 [英] Please make me understand
问题描述
Option Strict On
Imports System.Data.OleDb
Imports System.IO
Imports System.Data.SqlClient
Public Class DataAccess
'For Local DB
Protected SQL_CONNECTION_STRING As String
Dim cnSQL As SqlConnection
Dim trSQL As SqlTransaction
Protected DidPreviouslyConnect As Boolean = False
Public Event ConnectionStatusChange(ByVal status As String)
Public Event ConnectionFailure(ByVal reason As String)
Public Event ConnectionCompleted(ByVal success As Boolean)
Public Function CreateSQLDataSet(ByVal strSQL As String) As DataSet
Dim RetDS As New DataSet
Dim GeneralDA As SqlDataAdapter
' Raise a status event saying that the user is attempting to connect.
' This only needs to be done the very first time a connection is
' attempted. After we've determined that MSDE or SQL Server is
' installed, this message no longer needs to be displayed.
If Not DidPreviouslyConnect Then
RaiseEvent ConnectionStatusChange("Connecting to SQL Server")
End If
Dim IsConnecting As Boolean = True
While IsConnecting
Try
' The SqlConnection class allows you to communicate with SQL Server.
' The constructor accepts a connection string as an argument. This
' connection string uses Integrated Security, which means that you
' must have a login in SQL Server, or be part of the Administrators
' group for this to work.
'cnSQL As New SqlConnection(SQL_CONNECTION_STRING)
' A SqlCommand object is used to execute the SQL commands.
Dim scmd As New SqlCommand(strSQL, cnSQL)
' A SqlDataAdapter uses the SqlCommand object to fill a DataSet.
GeneralDA = New SqlDataAdapter(scmd)
' A SqlCommandBuilder automatically generates the SQL commands needed
' to update the database later.
Dim scb As New SqlCommandBuilder(GeneralDA)
' Create a new DataSet and fill its first DataTable.
RetDS = New DataSet()
GeneralDA.Fill(RetDS)
IsConnecting = False
DidPreviouslyConnect = True
Catch exp As Exception
' Couldn't connect to SQL Server. Now try MSDE.
'RaiseEvent ConnectionFailure("Unable To Connect SQL Server")
Throw New ApplicationException("Unable To Connect SQL Server")
End Try
End While
RaiseEvent ConnectionCompleted(True)
Return RetDS
End Function
Public Function SQLDBTransaction(ByVal strQry As String) As Integer
Dim l_intRowsEffected As Integer
Dim cmSQL As SqlCommand
If Not DidPreviouslyConnect Then
RaiseEvent ConnectionStatusChange("Connecting to SQL Server")
End If
Dim IsConnecting As Boolean = True
Try
cmSQL = New SqlCommand(strQry, cnSQL, trSQL)
cmSQL.CommandType = CommandType.Text
'l_intRowsEffected = cmSQL.ExecuteNonQuery
l_intRowsEffected = CInt(cmSQL.ExecuteScalar())
If l_intRowsEffected = 0 Then
File.AppendAllText("DBErrorLog.dat", Now.ToString & " Insert Fiil " & strQry & vbNewLine)
End If
' Close and Clean up objects
cmSQL.Dispose()
cmSQL = Nothing
SQLDBTransaction = l_intRowsEffected
Catch ex As Exception
File.AppendAllText("DBErrorLog.dat", Now.ToString & " - " & ex.Message & " -" & strQry & vbNewLine)
'trSQL.Rollback()
SQLDBTransaction = 0
Throw New ApplicationException(ex.Message)
End Try
RaiseEvent ConnectionCompleted(True)
Return SQLDBTransaction
End Function
Public Sub New(ByVal ApplicationName As String)
Try
'
'SQL_CONNECTION_STRING = "Data Source=testserver\sql2k8test;Initial Catalog=HRMS;User ID=abc;Password=abc;application name=HRMS;packet size=4096;persist security info=True;"
'SQL_CONNECTION_STRING = System.IO.File.ReadAllText("Connect.dat")
cnSQL = New SqlConnection(SQL_CONNECTION_STRING)
cnSQL.Open()
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Sub
Public Sub Close()
cnSQL.Close()
cnSQL.Dispose()
cnSQL = Nothing
If trSQL IsNot Nothing Then
trSQL.Dispose()
trSQL = Nothing
End If
End Sub
Public Sub BeginTrans()
trSQL = cnSQL.BeginTransaction
End Sub
Public Sub CommitTrans()
If trSQL IsNot Nothing Then
trSQL.Commit()
End If
End Sub
Public Sub RollBackTrans()
If trSQL IsNot Nothing Then
trSQL.Rollback()
End If
End Sub
End Class
推荐答案
您知道逐行解释代码的工作量是多少吗?
每一行都需要一段说明!例如:
Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
Dim next as Integer = r.Next()
创建一个名为"next"的新变量,该变量可以容纳一个整数值.在先前声明的Random实例"r"中,调用"Next"方法以获取新的随机数,并将其分配给"next"变量.
您能想象我们要花多长时间逐行解释一个非常短的代码片段(与您的示例不同)吗?
不,这不会发生.如果您有特定的问题,请提出一个问题.但是先想想-您是否想坐下45分钟并在没有充分理由的情况下逐行输入说明?
Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.
Can you imagine how long it would take us to explain even a very short code fragment (unlike your example), line by line?
No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?
这篇关于请让我明白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!