快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

vb.net日志处理类 vb运行怎么输出日志

使用VB.NET的五个技巧之处理数据行

处理数据行(DataRow)

成都创新互联专业为企业提供丹棱网站建设、丹棱做网站、丹棱网站设计、丹棱网站制作等企业网站建设、网页设计与制作、丹棱企业网站模板建站服务,十载丹棱做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

Windows窗体中的数据绑定列表框和组合框很节省时间 典型的代码如下(假定已经建立了SqlDataAdapter或者其它部件获取数据)

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

在这种情况下 代码使用Northwind数据库的顾客记录工作 DisplayMember属性设置为你希望用户在列表框中看到的记录字段 它是customers表的CompanyName 通常ValueMember属性设置为数据表中的一个键字段 对于customer来说是CustomerID 一旦用户选择了列表框中的一行 很容易使用列表框的SelectedValue属性获得键字段

MsgBox(ListBox SelectedValue)

但是有可能需要一个与被选择项相关的整个数据行对象的引用 例如 如果被选择的行需要被删除 就不知道键了 你需要一个数据行的引用以使用Delete方法

典型的Visual Basic开发者通常这样想 我已经得到了该行的键了 我将编写一些逻辑来查找使用该键的行 这样可以实现 但是有更好的实现方法 可以使用一行代码获取与列表框中选项关联的数据行

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

通常该逻辑不会凭直觉出现 即使对经验丰富的开发者 为了解释这是怎样实现的 我把上面的一行拆成几行 下面的代码与上面代码的功能相同

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

DataRowView类是数据行的包装 它被多个Windows窗体控件使用 它使得显示与控件中的数据行相关的数据更加容易 当列表框被数据绑定到数据表时(假定列表框中的有些行当前被选定了) 列表框的SelectedItem属性保存了一个DataRowView对象

这意味着我们能把列表框的SelectedItem属性转换到DataRowView对象 这就是上面代码中的第二行实现的 接着DataRowView暴露一个Row属性 它指向被包装的数据行 上面的代码声明了一个数据行并设置了Row属性

转换对象的类型以访问它的接口的技术在Visual Basic 中不是经常使用 但是在Visual Basic NET中这是经常的 有了上面的例子后 大多数有经验的开发者迅速跟上了这种技术

数据行的引用(dr)可用于用任何方式维护行 访问数据行中的任何特定字段是可行的 行中的数据可以被改变 能使数据行的Delete方法把该行标识为删除 或者从数据表的行集合中删除该行 下面的代码标识删除了一行

dr Delete()

lishixinzhi/Article/program/net/201311/12974

请教高手,如何用vb实现“使用日志”?

很多种方法,最常用的一是用文本文件或数据库记录,二是调用API函数直接写入系统日志或应用程序日志

vb.net编译时生成log文件夹

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

自动在log目录中,生成日志文件,文件命名用年月日定义,如20100119_log.log。

在日志文件中,每行记录一个时间点的运行内容,时间点的精度应该达到毫秒。

在vb.net 中,记录系统错误日志这个功能怎么实现

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

'

'错误处理中心过程,写数据库日志表或写日志文件

'

'strModule '模块名称

'strProcedure '过程名称

'lngErrorNumber '错误ID号

'strErrorDescription '错误描述

'showMsg '是否显示本过程内错误显示信息(值:"Y" or "N")

'Error表结构(f001 (Date)发生时间, f002 (nvarchar50)模块名称, f003 (nvarchar50)过程名称, f004 (nvarchar50)错误ID号, _

f005 (nvarchar300)错误描述,f006 (nvarchar50)版 本 号, f007 (nvarchar50)用户名称, f008 (nvarchar50)网卡地址

'ErrorCode表结构 f001 (nvarchar20)错误代码, f002 (nvarchar255)错误信息, f003 (numeric9)错误级别

' 级别说明: '10'以下,一般错误,不影响操作

' '11-20',严重错误,不能操作,程序执行退出

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 "产生一个严重错误,可能影响到系统的可操作性,请立即联系本系统开发人员!", 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

'写错误入数据库表--------------------------

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运行怎么输出日志
网页地址:http://6mz.cn/article/dospjpg.html

其他资讯