read database, create dataset

manared

Well-known member
Joined
Jun 1, 2006
Messages
84
Programming Experience
1-3
I am using vb.net 2005 and have created a windows form. I have a database with entries and I want to cycle through every entry in the database. My SQL statement has 3 different filters on it, but if one matches, I want to put it in the dataset. SQL statment:
VB.NET:
Dim strSQL As String = "SELECT * FROM FreeSchedule WHERE (schedule_event_date <= '" & lblDate.Text & "') AND (schedule_central_time <= '" & lblTime.Text & "') AND (schedule_reminder_ampm = '" & lblampm.Text & "')"
If another matches, I want to add a row to the dataset. How do I go about doing this? After the dataset is full, or there are no more entries in the database that match the criteria, I want to read the database and for every entry, do an action (send an email).

I'm not exactly sure how to start doing this because of the new version. I don't know if I should hard code or if I can do it through the gooey tools. Any ideas as to what is the best way to do this and how to do it? Thank you!
 
In 2003 i'd use a datareader for this, it's fast and will do what you want. I would think that the implementation is the same in 2005.

VB.NET:
[COLOR=blue]Public[/COLOR] [COLOR=blue]Sub[/COLOR] ReadMyData([COLOR=blue]ByVal[/COLOR] myConnString [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR])
 
[FONT='Courier New']        [COLOR=blue]Dim[/COLOR] mySelectQuery [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] = [/FONT][FONT='Courier New'] "SELECT * FROM FreeSchedule WHERE (schedule_event_date <= '" & lblDate.Text & "') AND (schedule_central_time <= '" & lblTime.Text & "') AND (schedule_reminder_ampm = '" & lblampm.Text & "')"[/FONT]
[FONT='Courier New'][/FONT] 
[FONT='Courier New']       [COLOR=blue]Dim[/COLOR] myConnection [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] SqlConnection(myConnString)[/FONT]
[FONT='Courier New']        [COLOR=blue]Dim[/COLOR] myCommand [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] SqlCommand(mySelectQuery, myConnection)[/FONT]
[FONT='Courier New']        myConnection.Open()[/FONT]
[FONT='Courier New']        [COLOR=blue]Dim[/COLOR] myReader [COLOR=blue]As[/COLOR] SqlDataReader[/FONT]
[FONT='Courier New'][/FONT] 
[FONT='Courier New']        myReader = myCommand.ExecuteReader()[/FONT]
[FONT='Courier New'][/FONT] 
[FONT='Courier New']        [COLOR=green]' Always call Read before accessing data.[/COLOR][/FONT]
[FONT='Courier New'][COLOR=green][/COLOR][/FONT] 
[FONT='Courier New']        [COLOR=blue]While[/COLOR] myReader.Read()[/FONT]
[FONT='Courier New'][/FONT] 
[FONT='Courier New'][FONT='Courier New']          Dim NRow as 'YourDatatable'.Newrow[/FONT]
[FONT='Courier New'][/FONT] 
[/FONT] 
[FONT='Courier New']          nrow(columnOrdinal) = myreader.getvalue(columnordinal)[/FONT]
[FONT='Courier New'][/FONT] 
[FONT='Courier New']          'Do this through all the fields in the database.[/FONT]
[FONT='Courier New'][/FONT] 
 
[FONT='Courier New']        [/FONT][FONT='Courier New']        [COLOR=blue]End[/COLOR] [COLOR=blue]While[/COLOR][/FONT]
[FONT='Courier New']        [COLOR=green]' always call Close when done reading.[/COLOR][/FONT]
[FONT='Courier New']        myReader.Close()[/FONT]
[FONT='Courier New']        [COLOR=green]' Close the connection when done with it.[/COLOR][/FONT]
[FONT='Courier New']        myConnection.Close()[/FONT]
[FONT='Courier New'] [/FONT]
[FONT='Courier New']    [COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR] [COLOR=green]'ReadMyData[/COLOR][/FONT]


You may want to adjust it to suit your needs. Hope that helps
 
Thanks for helping.. That would probably work, I didn't try it though. I got the program to work this morning though to fill a dataset and then a datagrid. Here's my code:

VB.NET:
Public Class Form1
Dim myconn As String = ("server=SQL;database=4Ddata;User ID=sa;Password=hoffmann")
Private ds As DataSet
Private dv As DataView
Private cn As SqlConnection
 
Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click
cn = New SqlConnection(myconn)
ds = New DataSet
dv = New DataView
Dim ad As New SqlDataAdapter("SELECT * FROM FreeSchedule WHERE (schedule_event_date <= '" & lblDate.Text & "') AND (schedule_central_time <= '" & lblTime.Text & "') AND (schedule_reminder_ampm = '" & lblampm.Text & "')", cn)
ad.Fill(ds, "FreeSchedule")
dv.Table = ds.Tables("FreeSchedule")
Me.DataGridView2.DataSource = dv
End Sub
End Class
 
Alright, so maybe it's not working like it should. With the SQL statement that I have, it seems to be pulling back more than what it should. When comparing dates and times, is there anything special that I have to do? Cuz I want to pull back all of the times previous to the System.Date.now and put that into a dataset. Is my SQL statement correct? Or any ideas why it could be acting up? My code for the program is here:

VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the '_4DdataDataSet.FreeSchedule' table. You can move, or remove it, as needed.
Me.FreeScheduleTableAdapter.Fill(Me._4DdataDataSet.FreeSchedule)
Me.FreeScheduleBindingSource.MoveNext()
cpdata()
 
ParseCurrentDateTime()
'Run the program
cn = New SqlConnection(myconn)
ds = New DataSet
dv = New DataView

Dim ad As New SqlDataAdapter("SELECT * FROM FreeSchedule WHERE (schedule_event_date <= '" & lblDate.Text & "') AND (schedule_central_time <= '" & lblTime.Text & "') AND (schedule_reminder_ampm = '" & lblampm.Text & "')", cn) 
ad.Fill(ds, "FreeSchedule")

dv.Table = ds.Tables("FreeSchedule")
Me.DataGridView2.DataSource = dv
SendAndDelete()
End Sub
 
Sub ParseCurrentDateTime()
'Dim dateNow As String
Label9.Text = System.DateTime.Now
Dim parsedDateTime() As String = Split(Label9.Text) 'parsing the entire DateTime.Now into Date, Time, and AM/PM
Dim dateflag As String = parsedDateTime(0) 'Date - mm/dd/yyyy
Dim timeflag As String = parsedDateTime(1) 'Time - hh:mm:ss
Dim ampmflag As String = parsedDateTime(2) 'AM or PM


Dim parsedTime() As String = Split(timeflag, ":") 'parsing the Time into Hour, Minute, and Second

Dim timeHour As String = parsedTime(0) 'Hour
Dim timeMinute As String = parsedTime(1) 'Minute
Dim timeSecond As String = parsedTime(2) 'Second
Dim timeHourMinute As String = timeHour & ":" & timeMinute
If ampmflag = "PM" Then
ampmflag = "pm"
timeHour = timeHour + 12
Else
ampmflag = "am"
End If
lblampm.Text = ampmflag
lblDate.Text = dateflag
lblTime.Text = timeHourMinute
End Sub
 
Sub SendAndDelete()
Dim y As Integer
For y = 0 To (ds.Tables(0).Rows.Count - 1)
'First send the message
Dim number As String = ds.Tables(0).Rows(y).Item(4).ToString
Dim message As String = ds.Tables(0).Rows(y).Item(5).ToString
sendMessage("test@4DImpressions.com", number, "testing Free Text", message & " -- 4DImpressions.com")
'Then insert data into Free Audit Trail database table
Dim infoId As String = ds.Tables(0).Rows(y).Item(1).ToString
Dim cell As String = ds.Tables(0).Rows(y).Item(2).ToString
Dim datetimeEntered As String = ds.Tables(0).Rows(y).Item(3).ToString
Dim My2SQL As String = "Insert into FreeAuditTrail (info_id, cust_cell, schedule_date_entered, date_sent, schedule_message) VALUES ('" & infoId & "', '" & cell & "', '" & datetimeEntered & "', '" & Date.Now & "', '" & message & "')"
Dim Command2 As New SqlCommand(My2SQL, conn)
conn.Open()
Command2.ExecuteNonQuery()
conn.Close()
'Last, delete row from freeSchedule database table
Dim scheduleId As String = ds.Tables(0).Rows(y).Item(0).ToString
Dim MySQL As String = "DELETE FROM FreeSchedule WHERE (schedule_id = '" & ds.Tables(0).Rows(y).Item(0).ToString & "')"
Dim Command3 As New SqlCommand(MySQL, conn)
conn.Open()
Command3.ExecuteNonQuery()
conn.Close()
Next
Application.Exit()
End Sub

Does anyone see any problems at all or suggestions on what I should change? Thank you so much!
 
Alrighty, I fixed my own problem. I was comparing a string to an integer with <= and that doesn't work. So I converted all of my times to a 24 hour clock, like military time. Now I can compare these numbers with ease. I would recommend converting it like this. Thanks for all the help!!!!! :D
 
manared said:
Alrighty, I fixed my own problem. I was comparing a string to an integer with <= and that doesn't work. So I converted all of my times to a 24 hour clock, like military time. Now I can compare these numbers with ease. I would recommend converting it like this. Thanks for all the help!!!!! :D

actually, i'd recommend being consistent with the datatypes...

if schedule_event_date is a date, then compare it with another date, not a tring, or an integer...
 
Back
Top