自己写的一些类代码
以前在写类代码时候,改写了一些自定义的类代码,自己在做CManage时,一些传统的类老是要重复地写。有点烦,在参考同学的类代码中。自己也写了一些类。以下就是这个作业的类的代码。
这里是一个类,存储字段的类
Public Class Parm
Public Class Parm
'返回sqldatareader(数据读取器)(用于查找数据)
Public Function ReturnReader(ByVal myParm As Parm) As SqlDataReader
Dim Cmd As SqlCommand = ReturnCmd(myParm)
Dim MyDtr As SqlDataReader = Cmd.ExecuteReader(CommandBehavior.CloseConnection)
Return MyDtr
End Function
Public Function ReturnCmd(ByVal myParm As Parm) As SqlCommand
con.Open()
Dim Cmd As SqlCommand
Cmd = New SqlCommand(myParm.str1, con)
Cmd.CommandType = CommandType.StoredProcedure
Return Cmd
End Function
'数据控件的数据绑定(通用于Dropdownlist,datagrid,datalist)(数据源是Datareader)
Public Sub DataControlDataReaderBinding(ByVal myp As Parm)
myp.objCtr.DataSource = ReturnReader(myp)
If TypeOf (myp.objCtr) Is DataList Or TypeOf (myp.objCtr) Is DataGrid Then
If myp.DataKeyField <> "" Then
myp.objCtr.DataKeyField = myp.DataKeyField
End If
myp.objCtr.DataBind()
ElseIf TypeOf (myp.objCtr) Is DropDownList Then
myp.objCtr.DataTextField = myp.DataTextField
myp.objCtr.DataValueField = myp.DataValueField
myp.objCtr.DataBind()
End If
con.Close()
End Sub
Public Function ReturnReader(ByVal myParm As Parm) As SqlDataReader
Dim Cmd As SqlCommand = ReturnCmd(myParm)
Dim MyDtr As SqlDataReader = Cmd.ExecuteReader(CommandBehavior.CloseConnection)
Return MyDtr
End Function
Public Function ReturnCmd(ByVal myParm As Parm) As SqlCommand
con.Open()
Dim Cmd As SqlCommand
Cmd = New SqlCommand(myParm.str1, con)
Cmd.CommandType = CommandType.StoredProcedure
Return Cmd
End Function
'数据控件的数据绑定(通用于Dropdownlist,datagrid,datalist)(数据源是Datareader)
Public Sub DataControlDataReaderBinding(ByVal myp As Parm)
myp.objCtr.DataSource = ReturnReader(myp)
If TypeOf (myp.objCtr) Is DataList Or TypeOf (myp.objCtr) Is DataGrid Then
If myp.DataKeyField <> "" Then
myp.objCtr.DataKeyField = myp.DataKeyField
End If
myp.objCtr.DataBind()
ElseIf TypeOf (myp.objCtr) Is DropDownList Then
myp.objCtr.DataTextField = myp.DataTextField
myp.objCtr.DataValueField = myp.DataValueField
myp.objCtr.DataBind()
End If
con.Close()
End Sub
Dim myTest As New Test
Private Sub ddlBindClass()
Dim myParm As New Parm
myParm.str1 = "sp_SelectNewsClass"
myParm.objCtr = ddlBindNewsClass
myParm.DataTextField = "ClassName"
myTest.DataControlDataReaderBinding(myParm)
End Sub
Private Sub ddlBindClass()
Dim myParm As New Parm
myParm.str1 = "sp_SelectNewsClass"
myParm.objCtr = ddlBindNewsClass
myParm.DataTextField = "ClassName"
myTest.DataControlDataReaderBinding(myParm)
End Sub