vb.net日志處理類 vb運行怎么輸出日志

使用VB.NET的五個技巧之處理數(shù)據(jù)行

處理數(shù)據(jù)行(DataRow)

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供丹棱網(wǎng)站建設、丹棱做網(wǎng)站、丹棱網(wǎng)站設計、丹棱網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、丹棱企業(yè)網(wǎng)站模板建站服務,十載丹棱做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。

Windows窗體中的數(shù)據(jù)綁定列表框和組合框很節(jié)省時間 典型的代碼如下(假定已經(jīng)建立了SqlDataAdapter或者其它部件獲取數(shù)據(jù))

Dim ds As New DataSet() SqlDataAdapter Fill(ds Customers ) ListBox DataSource = ds Tables( Customers ) ListBox DisplayMember = CompanyName ListBox ValueMember = CustomerID

在這種情況下 代碼使用Northwind數(shù)據(jù)庫的顧客記錄工作 DisplayMember屬性設置為你希望用戶在列表框中看到的記錄字段 它是customers表的CompanyName 通常ValueMember屬性設置為數(shù)據(jù)表中的一個鍵字段 對于customer來說是CustomerID 一旦用戶選擇了列表框中的一行 很容易使用列表框的SelectedValue屬性獲得鍵字段

MsgBox(ListBox SelectedValue)

但是有可能需要一個與被選擇項相關的整個數(shù)據(jù)行對象的引用 例如 如果被選擇的行需要被刪除 就不知道鍵了 你需要一個數(shù)據(jù)行的引用以使用Delete方法

典型的Visual Basic開發(fā)者通常這樣想 我已經(jīng)得到了該行的鍵了 我將編寫一些邏輯來查找使用該鍵的行 這樣可以實現(xiàn) 但是有更好的實現(xiàn)方法 可以使用一行代碼獲取與列表框中選項關聯(lián)的數(shù)據(jù)行

Dim dr As DataRow = CType(ListBox SelectedItem DataRowView) Row

通常該邏輯不會憑直覺出現(xiàn) 即使對經(jīng)驗豐富的開發(fā)者 為了解釋這是怎樣實現(xiàn)的 我把上面的一行拆成幾行 下面的代碼與上面代碼的功能相同

Dim drv As DataRowView drv = CType(ListBox SelectedItem DataRowView) Dim dr As DataRow dr = drv Row

DataRowView類是數(shù)據(jù)行的包裝 它被多個Windows窗體控件使用 它使得顯示與控件中的數(shù)據(jù)行相關的數(shù)據(jù)更加容易 當列表框被數(shù)據(jù)綁定到數(shù)據(jù)表時(假定列表框中的有些行當前被選定了) 列表框的SelectedItem屬性保存了一個DataRowView對象

這意味著我們能把列表框的SelectedItem屬性轉換到DataRowView對象 這就是上面代碼中的第二行實現(xiàn)的 接著DataRowView暴露一個Row屬性 它指向被包裝的數(shù)據(jù)行 上面的代碼聲明了一個數(shù)據(jù)行并設置了Row屬性

轉換對象的類型以訪問它的接口的技術在Visual Basic 中不是經(jīng)常使用 但是在Visual Basic NET中這是經(jīng)常的 有了上面的例子后 大多數(shù)有經(jīng)驗的開發(fā)者迅速跟上了這種技術

數(shù)據(jù)行的引用(dr)可用于用任何方式維護行 訪問數(shù)據(jù)行中的任何特定字段是可行的 行中的數(shù)據(jù)可以被改變 能使數(shù)據(jù)行的Delete方法把該行標識為刪除 或者從數(shù)據(jù)表的行集合中刪除該行 下面的代碼標識刪除了一行

dr Delete()

lishixinzhi/Article/program/net/201311/12974

請教高手,如何用vb實現(xiàn)“使用日志”?

很多種方法,最常用的一是用文本文件或數(shù)據(jù)庫記錄,二是調(diào)用API函數(shù)直接寫入系統(tǒng)日志或應用程序日志

vb.net編譯時生成log文件夾

在程序目錄下生成log目錄,用于保存日志文件。

自動在log目錄中,生成日志文件,文件命名用年月日定義,如20100119_log.log。

在日志文件中,每行記錄一個時間點的運行內(nèi)容,時間點的精度應該達到毫秒。

在vb.net 中,記錄系統(tǒng)錯誤日志這個功能怎么實現(xiàn)

Public Sub ShowError(strModule As String, strProcedure As String, lngErrorNumber As Long, strErrorDescription As String, showMsg As String)

'

'錯誤處理中心過程,寫數(shù)據(jù)庫日志表或寫日志文件

'

'strModule '模塊名稱

'strProcedure '過程名稱

'lngErrorNumber '錯誤ID號

'strErrorDescription '錯誤描述

'showMsg '是否顯示本過程內(nèi)錯誤顯示信息(值:"Y" or "N")

'Error表結構(f001 (Date)發(fā)生時間, f002 (nvarchar50)模塊名稱, f003 (nvarchar50)過程名稱, f004 (nvarchar50)錯誤ID號, _

f005 (nvarchar300)錯誤描述,f006 (nvarchar50)版 本 號, f007 (nvarchar50)用戶名稱, f008 (nvarchar50)網(wǎng)卡地址

'ErrorCode表結構 f001 (nvarchar20)錯誤代碼, f002 (nvarchar255)錯誤信息, f003 (numeric9)錯誤級別

' 級別說明: '10'以下,一般錯誤,不影響操作

' '11-20',嚴重錯誤,不能操作,程序執(zhí)行退出

On Error GoTo ErrorHandle

Dim strMessage As String

Dim strCaption As String

Dim sVer As String

Dim intLogFile As Integer

Dim Res As New ADODB.Recordset

Dim ResErrorCode As New ADODB.Recordset

Dim strSQL As String

'對應錯誤號,從ErrorCode表中找到對應的錯誤信息,0-1000 錯誤號保留給VB

DBOpen ResErrorCode, "select * from errorcode where f001='" lngErrorNumber "'"

If Not (ResErrorCode.EOF Or ResErrorCode.BOF) Then

strMessage = ResErrorCode.Fields("f002")

If ResErrorCode.Fields("f003") 10 Then

MsgBox "產(chǎn)生一個嚴重錯誤,可能影響到系統(tǒng)的可操作性,請立即聯(lián)系本系統(tǒng)開發(fā)人員!", vbCritical, "嚴重錯誤"

End If

End If

'寫錯誤入文件----------------------------

intLogFile = FreeFile

Open App.Path "\" strIni.LogFile For Append As #intLogFile

Print #intLogFile, "***錯誤"; VBA.Now "*** " "Version:" _

str$(App.Major) "." str$(App.Minor) "." Format(App.Revision, "0000")

Print #intLogFile, "Error: " lngErrorNumber

Print #intLogFile, "Description: " strErrorDescription

Print #intLogFile, "Module: " strModule

Print #intLogFile, "Procedure: " strProcedure

Print #intLogFile, ""

Close #intLogFile

If Len(strMessage) 2 Then strErrorDescription = strMessage

strMessage = "錯誤: " "(" lngErrorNumber ")" strErrorDescription vbCrLf vbCrLf _

"模塊:" strModule "; 過程:" strProcedure

sVer = Trim(str$(App.Major) "." str$(App.Minor) "." _

Format(App.Revision, "0000"))

strCaption = "錯誤 Version: " sVer

'寫錯誤入數(shù)據(jù)庫表--------------------------

strSQL = "insert into error(f001,f002,f003,f004,f005,f006,f007,f008) values(" _

DateFmtB VBA.Now DateFmtE "," _

IIf(Len(Trim(strModule)) = 0, "null", "'" strModule "'") "," _

IIf(Len(Trim(strProcedure)) = 0, "null", "'" strProcedure "'") "," _

IIf(Len(Trim(lngErrorNumber)) = 0, "null", "'" lngErrorNumber "'") "," _

IIf(Len(Trim(strErrorDescription)) = 0, "null", "'" Replace(strErrorDescription, "'", "") "'") "," _

IIf(Len(Trim(sVer)) = 0, "null", "'" sVer "'") "," _

IIf(Len(Trim(sUserName)) = 0, "null", "'" sUserName "'") "," _

IIf(Len(Trim(sVer)) = 0, "null", "'" EthernetNO "'") ")"

Cn.Execute strSQL

'是否顯示未知錯誤信息

If Trim(UCase(showMsg)) = "Y" Then MsgBox strMessage, vbCritical, strCaption

PROC_EXIT:

Set Res = Nothing

Set ResErrorCode = Nothing

Exit Sub

ErrorHandle:

Resume Next

新聞標題:vb.net日志處理類 vb運行怎么輸出日志
本文URL:http://muchs.cn/article36/dospjpg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信公眾號、ChatGPT企業(yè)網(wǎng)站制作、虛擬主機網(wǎng)站制作、網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設