用数据表中的数据填充对象属性. [英] Filling object attributes with data from a data table.
本文介绍了用数据表中的数据填充对象属性.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经完成了一个类和一个用类对象填充数据库的函数
以及删除对象的功能.现在需要一种用于修改对象的解决方案.这意味着从数据库修改对象返回对象并保存修改后的对象.这就是为什么需要一个函数来用数据库中的数据填充对象属性的原因.谢谢!
I have done a class and a function for populating database with objects of class
and also a function for deleting objects. Now a need a solution for modifying objects. that means returning object from database modifying it and save it modified. That''s why a need a function that fill object attributes with data from database. Thank you !
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Public Class Salariat
Dim m_cnp As String
Dim m_nume As String
Dim m_prenume As String
Dim m_adresa As String
Dim m_telefon As String
Dim m_calificare As String
Dim m_contBancar As String
Public Property cnp() As String
Get
Return m_cnp
End Get
Set(ByVal value As String)
If Len(value) = 13 Then
m_cnp = value
Else
Throw New Exception("CNP incorect!")
End If
End Set
End Property
Public Property nume() As String
Get
Return m_nume
End Get
Set(ByVal value As String)
If value <> "" Then
m_nume = value
Else
Throw New Exception("Completati Numele!")
End If
End Set
End Property
Public Property prenume() As String
Get
Return m_prenume
End Get
Set(ByVal value As String)
If value <> "" Then
m_prenume = value
Else
Throw New Exception("Completati Prenumele!")
End If
End Set
End Property
Public Property adresa() As String
Get
Return m_adresa
End Get
Set(ByVal value As String)
m_adresa = value
End Set
End Property
Public Property telefon() As String
Get
Return m_telefon
End Get
Set(ByVal value As String)
m_telefon = value
End Set
End Property
Public Property calificare() As String
Get
Return m_calificare
End Get
Set(ByVal value As String)
m_calificare = value
End Set
End Property
Public Property contBancar() As String
Get
Return m_contBancar
End Get
Set(ByVal value As String)
If Len(value) = 24 Then
m_contBancar = value
Else
Throw New Exception("Cont bancar incorect!")
End If
End Set
End Property
Public Sub Adauga()
Dim cmd As New SqlCommand
Dim sCnnection As String = "server=(local);Integrated Security=SSPI;database=crossal"
Dim cnn As New SqlConnection With {.ConnectionString = sCnnection}
cnn.Open()
cmd.Connection = cnn
cmd.CommandText = "INSERT INTO salariati (CNP, Nume, Prenume, Adresa, Telefon, Calificare, Contbancar) VALUES (@CNP, @Nume, @Prenume, @Adresa, @Telefon, @Calificare, @Contbancar)"
cmd.Parameters.AddWithValue("@CNP", Me.cnp)
cmd.Parameters.AddWithValue("@Nume", Me.nume)
cmd.Parameters.AddWithValue("@Prenume", Me.prenume)
cmd.Parameters.AddWithValue("@Adresa", Me.adresa)
cmd.Parameters.AddWithValue("@Telefon", Me.telefon)
cmd.Parameters.AddWithValue("@Calificare", Me.calificare)
cmd.Parameters.AddWithValue("@ContBancar", Me.contBancar)
cmd.ExecuteNonQuery()
End Sub
Public Sub Sterge(ByVal pcnp)
Dim qq
qq = MsgBox("Doriti sa stergeti salariatul cu CNP-ul " & pcnp & " ?", MsgBoxStyle.YesNo)
If qq = vbYes Then
Dim cmd As New SqlCommand
Dim sCnnection As String = "server=(local);Integrated Security=SSPI;database=crossal"
Dim cnn As New SqlConnection With {.ConnectionString = sCnnection}
cnn.Open()
cmd.Connection = cnn
cmd.CommandText = " delete from salariati where cnp=''" + pcnp + "''"
cmd.ExecuteNonQuery()
Else
End If
End Sub
Public Sub Incarca(ByVal pcnp)
Dim ccnp As New SqlCommand
Dim sConnection As String = "server=(local);Integrated Security=SSPI;database=Salarizare"
Dim c As New SqlConnection(sConnection)
c.Open()
ccnp.Connection = c
ccnp.CommandText = " select * from salariati where cnp=''" + pcnp + "''"
End Sub
End Class
推荐答案
要执行此操作,您将需要2种方法.一个返回正确的对象,另一个返回修改后的对象,如下所示:
To do this you will need 2 methods. One that returns the correct object, and another to save the object once it has been modified like this:
Public Function GetSalariat(ByVal pcnp) As Salariat
Dim sConnection As String = "server=(local);Integrated Security=SSPI;database=Salarizare"
Dim c As New SqlConnection(sConnection)
Try
Dim ccnp As New SqlCommand
c.Open()
ccnp.Connection = c
'don't use * in SQL Query - better to list all fields you need by name
ccnp.CommandText = "SELECT cnp, nume, prenume etc FROM salariati WHERE cnp = @cnp"
ccnp.Parameters.AddWithValue("@cnp", pcnp)
Dim drSalariats As SqlDataReader = ccnp.ExecuteReader()
Dim newSalariat As New Salariat()
While drSalariats.Read()
newSalariat.cnp = drSalariats.GetInt32(0)
newSalariat.nume = drSalariats.GetString(1)
newSalariat.prenume = drSalariats.GetString(2)
'add rest of members here
End While
Return newSalariat
Catch ex As SqlException
Throw ex 'should be handled better
Finally
If c.State = ConnectionState.Open Then
c.Close()
End If
End Try
End Function
并保存:
and to save:
Public Shared Function SaveSalariat(ByVal salariatToSave As Salariat) As Integer
Dim sCnnection As String = "server=(local);Integrated Security=SSPI;database=crossal"
Dim cnn As New SqlConnection With {.ConnectionString = sCnnection}
Try
Dim cmd As New SqlCommand
cnn.Open()
cmd.Connection = cnn
cmd.CommandText = "UPDATE tableName Set Nume = @Nume, PreNume = @Prenume, etc WHERE cnp = @Cnp"
cmd.Parameters.AddWithValue("@Nume", salariatToSave.nume)
cmd.Parameters.AddWithValue("@PreNume", salariatToSave.prenume)
'add rest of parameters here
Return cmd.ExecuteNonQuery()
Catch ex As SqlException
Throw ex 'should be handled better
Finally
If cnn.State = ConnectionState.Open Then
cnn.Close()
End If
End Try
End Function
希望对您有帮助
Hope this helps
这篇关于用数据表中的数据填充对象属性.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文