PDA

View Full Version : یک کلاس برای کار با فایلهای اکسس



بابک زواری
چهارشنبه 26 بهمن 1384, 02:20 صبح
کار هر کسی هست دستش درد نکنه


Public Const dbconn = "Provider=Microsoft.Jet.OLEDB.4.0; User ID=Admin;Data Source=C:\dbnotes.mdb"
Public mainado As New clsado
mainado.strconn = dbconn
mainado.strsql = "SELECT * FROM tblnotes ORDER BY fldid"
mainado.strtable = "tblnotes"
mainado.Query()
ZMain_Load(mainado.ReturnedDR)


Public Sub ZMain_Load(ByVal DR As DataRow)
Try
If DR.IsNull("fldid") = False Then
frmMain.lblid.Text = CStr(DR("fldid"))
Else
frmMain.lblid.Text = ""
End If
Catch
'other code here
End Try
End Sub

***********************

Option Explicit On
Option Strict On
Imports System.Data.OleDb

Public Class clsado
'/////////////////////////////////////////
'// Written By: Jared Bossart
'// Written For: Newbies whom want a simple connection to an access database
'// Contact Me: cis1130_327@yahoo.com
'// Permissions: This is for educational use only.
'// If used in a professional project please notify me of its use
'// and I'll be more then happy to give you any updated versions.
'// Additional information: This class converts the code of ado.net to the feel
'// of ado for easier learning.
'/////////////////////////////////////////

Private conn As New System.Data.OleDb.OleDbConnection
Private pDA As New System.Data.OleDb.OleDbDataAdapter 'property
Private pDT As New DataTable 'property
Private pDS As New DataSet 'property
Private pstrsql As String 'property
Private pstrtable As String 'property
Private pstrconn As String 'property
Private pPosition As Integer 'property

ReadOnly Property ReturnedDR() As DataRow 'Works like a recordset
Get
Return pDT.Rows(pPosition)
End Get
End Property

ReadOnly Property Position() As Integer 'Returns the index of the row within the datatable/dataset
Get
Return pPosition
End Get
End Property
Property DT() As DataTable
Get
Return pDT
End Get
Set(ByVal Value As DataTable)
pDT = Value
End Set
End Property

Property strconn() As String 'the connection string
Get
Return pstrconn
End Get
Set(ByVal Value As String)
pstrconn = Value
End Set
End Property

Property strsql() As String 'the sql select querty to be executed
Get
Return pstrsql
End Get
Set(ByVal Value As String)
pstrsql = Value
End Set
End Property

Property strtable() As String 'the table name
Get
Return pstrtable
End Get
Set(ByVal Value As String)
pstrtable = Value
End Set
End Property

Public Sub New()
pDA = New OleDbDataAdapter
pDS = New DataSet
End Sub

Public Sub Query()
'Dim myCommand As OleDbCommandBuilder
conn.ConnectionString = pstrconn
Try
pDA = New OleDbDataAdapter(pstrsql, conn)
pDA.Fill(pDS, pstrtable)
pDT = pDS.Tables(pstrtable)
Catch
Console.WriteLine("Error Opening {0}", conn.DataSource)
End Try
'myCommand = New OleDbCommandBuilder(pDA)
End Sub

Public Sub Insert(ByVal newrow As DataRow)
pDT.Rows.Add(newrow)
pDA.Update(pDS, pstrtable)
pDS.Tables(pstrtable).AcceptChanges()
End Sub

Public Sub Update()
Dim newDataSet As DataSet
newDataSet = pDS.GetChanges()
pDA.Update(newDataSet, pstrtable)
pDS.Tables(pstrtable).AcceptChanges()
End Sub

Public Sub Delete(ByVal id As Integer)
DT.Rows(id).Delete()
pDA.Update(pDS, pstrtable)
pDS.Tables(pstrtable).AcceptChanges()
End Sub

Public Sub MoveNext()
pPosition += 1
If pPosition >= pDT.Rows.Count Then
Me.MoveFirst()
Exit Sub
End If
End Sub

Public Sub MovePrevious()
pPosition -= 1
If pPosition < 0 Then
Me.MoveLast()
Exit Sub
End If
End Sub

Public Sub MoveFirst()
pPosition = 0
End Sub

Public Sub MoveLast()
pPosition = (pDT.Rows.Count - 1)
End Sub

Public Function Search(ByVal strsearch As String) As DataRow
'returns the first record found and converts from
'a two dimensional array to a one dimensional array
Dim foundRows As DataRow()
foundRows = pDT.Select(strsearch)
If foundRows.Length <> 0 Then
Dim DR2 As DataRow = pDT.Rows(0)
Dim cntx As Integer
'converts from a datarow array
For cntx = 0 To (foundRows(0).ItemArray.Length - 1)
DR2.Item(cntx) = foundRows(0).Item(cntx)
Next
Return DR2
Else
Return Nothing
End If
End Function
End Class

niloufar
دوشنبه 15 اسفند 1384, 13:25 عصر
سلام
من کلا از ویزاردی کار کردن بدم میاد، یه کار کوچیک داشتم، حسم نگرفت مثل همیشه عمل کنم و گفتم از این کلاس استفاده کنم.
اما هنگام Update خطا رخ داد که Valid UpdateCommand (اگر میخواستم یه ردیف از DataGridViewای که به اون بایند بود را update کنم) یا Valid InsertCommand (اگه میخواستم یه ردیف به اون اضافه کنم) وجود نداره.
اگه فرد دیگری از این کلاس (که خیلی وقته آقای زواری زحمتشو کشیدن) استفاده کرده بگه ...

بابک زواری
دوشنبه 15 اسفند 1384, 14:45 عصر
خانم نیلوفر این کلاس عمومی هست و من از یک سایت گرفتم و کار خودم نیست .

niloufar
دوشنبه 15 اسفند 1384, 17:57 عصر
خانم نیلوفر این کلاس عمومی هست و من از یک سایت گرفتم و کار خودم نیست .
سلام
ممنون. متوجه بودم. ولی گفتم شاید خودتون یا دوستان دیگه بتونن کمکم کنن وگرنه تو همین برنامه ساده هم از همون کلاس به قول خودم "به درد به خور" که واسه خودم بود و ویزاردی هم نبود استفاده میکنم.

بازم ممنون

sajjad_kochekian
چهارشنبه 24 اسفند 1384, 15:04 عصر
:قلب: :شیطان: :متعجب: :گریه: :خجالت: