By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,835 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,835 IT Pros & Developers. It's quick & easy.

VB recordset to ADO.Net question

P: n/a
Hi all-

I am a former VB6 programmer and new at C# and I have a question dealing
with converting some code from VB6 to C#. The code is below and essentially,
what it does is gets data from a SQL Server database and parses some of the
data and puts the parsed data into a text field. I omitted a some of the code
and left the data parsing part of it, which is what my question is about.
Exactly what the code below does is gets data from SQL Server and parses one
of the date fields. So if the date it retrieves is 1/9/2006, the parsing
outcome becomes:
01092006. I am not sure how to accomplish this in C#, being that I am no
longer dealing with a recorset and I'm not sure how to accomplish this using
a dataset or datareader.

VB Code:

Set rsRecordset = New Recordset
rsRecordset.Open (sSQL), adoConn, adOpenStatic, adLockOptimistic
Dim varNumber As Variant
Dim intDP As Integer
Dim sYY As String 'Pulls the year value from the recordset
Dim sMM As String 'Pulls the month value from the recordset
Dim sDD As String 'Pulls the day value from the recordset
Dim sAV As String 'Pulls the actual value from the recordset
Dim sIV As String 'Pulls the indicated value from the recordset
Dim iAV As String
Dim iIV As String
Dim iYY As String
Dim iMM As String
Dim iDD As String
sPrefix = ""

Do While Not rsRecordset.EOF
sYY = CStr(rsRecordset("YY")) 'the year
sMM = CStr(rsRecordset("MM")) 'the month
sDD = CStr(rsRecordset("DD")) 'the day
iYY = CInt(sYY)
iDD = CInt(sDD)
iMM = CInt(sMM)

If iYY < 10 Then sYY = "0" & iYY 'if the year digit is less then 10
add a zero
If iDD < 10 Then sDD = "0" & iDD 'same
If iMM < 10 Then sMM = "0" & iMM 'same

sAV = CStr(rsRecordset("AV")) 'Actual value
sIV = CStr(rsRecordset("IV")) 'Indicated value
iAV = CInt(sAV)
iIV = CInt(sIV)

If iAV < 10 Then sAV = "000" & CStr(rsRecordset("AV"))
If iIV < 10 Then sIV = "000" & CStr(rsRecordset("IV"))

If iAV >= 10 And iAV < 100 Then sAV = "00" & CStr(rsRecordset("AV"))
If iIV >= 10 And iAV < 100 Then sIV = "00" & CStr(rsRecordset("IV"))

If iAV >= 100 And iAV < 1000 Then sAV = "0" & CStr(rsRecordset("AV"))
If iIV >= 100 And iIV < 1000 Then sIV = "0" & CStr(rsRecordset("IV"))
Me.Text1.Text = Me.Text1.Text & sPrefix & "8" &
CStr(rsRecordset("ST")) & CStr(rsRecordset("CNTY")) _
& CStr(rsRecordset("SITE")) &
CStr(rsRecordset("PARMETER")) & CStr(rsRecordset("POC")) _
& CStr(rsRecordset("TMINT")) & "001" & "0" &
CStr(rsRecordset("MET")) _
& sYY & sMM & sDD & "0" & sAV & sIV & Space(41) & "I"
rsRecordset.MoveNext
sPrefix = vbCrLf
Loop
Thanks!

Nov 13 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Cast your date into a DateTime object. Then use the string output
method that meets your formatting needs.

//Pass in year, month and day here from your database records
DateTime myDate = new DateTime(year,month, day);
myDate.ToShortDateString():

Joseph wrote:
Hi all-

I am a former VB6 programmer and new at C# and I have a question dealing
with converting some code from VB6 to C#. The code is below and essentially,
what it does is gets data from a SQL Server database and parses some of the
data and puts the parsed data into a text field. I omitted a some of the code
and left the data parsing part of it, which is what my question is about.
Exactly what the code below does is gets data from SQL Server and parses one
of the date fields. So if the date it retrieves is 1/9/2006, the parsing
outcome becomes:
01092006. I am not sure how to accomplish this in C#, being that I am no
longer dealing with a recorset and I'm not sure how to accomplish this using
a dataset or datareader.

VB Code:

Set rsRecordset = New Recordset
rsRecordset.Open (sSQL), adoConn, adOpenStatic, adLockOptimistic
Dim varNumber As Variant
Dim intDP As Integer
Dim sYY As String 'Pulls the year value from the recordset
Dim sMM As String 'Pulls the month value from the recordset
Dim sDD As String 'Pulls the day value from the recordset
Dim sAV As String 'Pulls the actual value from the recordset
Dim sIV As String 'Pulls the indicated value from the recordset
Dim iAV As String
Dim iIV As String
Dim iYY As String
Dim iMM As String
Dim iDD As String
sPrefix = ""

Do While Not rsRecordset.EOF
sYY = CStr(rsRecordset("YY")) 'the year
sMM = CStr(rsRecordset("MM")) 'the month
sDD = CStr(rsRecordset("DD")) 'the day
iYY = CInt(sYY)
iDD = CInt(sDD)
iMM = CInt(sMM)

If iYY < 10 Then sYY = "0" & iYY 'if the year digit is less then 10
add a zero
If iDD < 10 Then sDD = "0" & iDD 'same
If iMM < 10 Then sMM = "0" & iMM 'same

sAV = CStr(rsRecordset("AV")) 'Actual value
sIV = CStr(rsRecordset("IV")) 'Indicated value
iAV = CInt(sAV)
iIV = CInt(sIV)

If iAV < 10 Then sAV = "000" & CStr(rsRecordset("AV"))
If iIV < 10 Then sIV = "000" & CStr(rsRecordset("IV"))

If iAV >= 10 And iAV < 100 Then sAV = "00" & CStr(rsRecordset("AV"))
If iIV >= 10 And iAV < 100 Then sIV = "00" & CStr(rsRecordset("IV"))

If iAV >= 100 And iAV < 1000 Then sAV = "0" & CStr(rsRecordset("AV"))
If iIV >= 100 And iIV < 1000 Then sIV = "0" & CStr(rsRecordset("IV"))
Me.Text1.Text = Me.Text1.Text & sPrefix & "8" &
CStr(rsRecordset("ST")) & CStr(rsRecordset("CNTY")) _
& CStr(rsRecordset("SITE")) &
CStr(rsRecordset("PARMETER")) & CStr(rsRecordset("POC")) _
& CStr(rsRecordset("TMINT")) & "001" & "0" &
CStr(rsRecordset("MET")) _
& sYY & sMM & sDD & "0" & sAV & sIV & Space(41) & "I"
rsRecordset.MoveNext
sPrefix = vbCrLf
Loop
Thanks!
Nov 13 '06 #2

P: n/a

Joseph wrote:
Hi all-

I am a former VB6 programmer and new at C# and I have a question dealing
with converting some code from VB6 to C#. The code is below and essentially,
what it does is gets data from a SQL Server database and parses some of the
data and puts the parsed data into a text field. I omitted a some of the code
and left the data parsing part of it, which is what my question is about.
Exactly what the code below does is gets data from SQL Server and parses one
of the date fields. So if the date it retrieves is 1/9/2006, the parsing
outcome becomes:
01092006. I am not sure how to accomplish this in C#, being that I am no
longer dealing with a recorset and I'm not sure how to accomplish this using
a dataset or datareader.

VB Code:

Set rsRecordset = New Recordset
rsRecordset.Open (sSQL), adoConn, adOpenStatic, adLockOptimistic
Dim varNumber As Variant
Dim intDP As Integer
Dim sYY As String 'Pulls the year value from the recordset
Dim sMM As String 'Pulls the month value from the recordset
Dim sDD As String 'Pulls the day value from the recordset
Dim sAV As String 'Pulls the actual value from the recordset
Dim sIV As String 'Pulls the indicated value from the recordset
Dim iAV As String
Dim iIV As String
Dim iYY As String
Dim iMM As String
Dim iDD As String
sPrefix = ""

Do While Not rsRecordset.EOF
sYY = CStr(rsRecordset("YY")) 'the year
sMM = CStr(rsRecordset("MM")) 'the month
sDD = CStr(rsRecordset("DD")) 'the day
iYY = CInt(sYY)
iDD = CInt(sDD)
iMM = CInt(sMM)

If iYY < 10 Then sYY = "0" & iYY 'if the year digit is less then 10
add a zero
If iDD < 10 Then sDD = "0" & iDD 'same
If iMM < 10 Then sMM = "0" & iMM 'same

sAV = CStr(rsRecordset("AV")) 'Actual value
sIV = CStr(rsRecordset("IV")) 'Indicated value
iAV = CInt(sAV)
iIV = CInt(sIV)

If iAV < 10 Then sAV = "000" & CStr(rsRecordset("AV"))
If iIV < 10 Then sIV = "000" & CStr(rsRecordset("IV"))

If iAV >= 10 And iAV < 100 Then sAV = "00" & CStr(rsRecordset("AV"))
If iIV >= 10 And iAV < 100 Then sIV = "00" & CStr(rsRecordset("IV"))

If iAV >= 100 And iAV < 1000 Then sAV = "0" & CStr(rsRecordset("AV"))
If iIV >= 100 And iIV < 1000 Then sIV = "0" & CStr(rsRecordset("IV"))
Me.Text1.Text = Me.Text1.Text & sPrefix & "8" &
CStr(rsRecordset("ST")) & CStr(rsRecordset("CNTY")) _
& CStr(rsRecordset("SITE")) &
CStr(rsRecordset("PARMETER")) & CStr(rsRecordset("POC")) _
& CStr(rsRecordset("TMINT")) & "001" & "0" &
CStr(rsRecordset("MET")) _
& sYY & sMM & sDD & "0" & sAV & sIV & Space(41) & "I"
rsRecordset.MoveNext
sPrefix = vbCrLf
Loop
Thanks!
Joseph,

Like Matt says, except to get the date format you need (which I assume
is MMDDYYY and not DDMMYYYY) you can use this format string:
dt.ToString("MMddyyyy")

Where dt is the DateTime object Matt refers to.

-Jay

Nov 13 '06 #3

P: n/a
Thanks for replying guys.

Additionally: Would you use a dataset or datareader to perfom this? Can you
provide a snippet of code that would demonstrate how to actually capture the
data from the database and put into a variable. Thanks

"Jay Riggs" wrote:
>
Joseph wrote:
Hi all-

I am a former VB6 programmer and new at C# and I have a question dealing
with converting some code from VB6 to C#. The code is below and essentially,
what it does is gets data from a SQL Server database and parses some of the
data and puts the parsed data into a text field. I omitted a some of the code
and left the data parsing part of it, which is what my question is about.
Exactly what the code below does is gets data from SQL Server and parses one
of the date fields. So if the date it retrieves is 1/9/2006, the parsing
outcome becomes:
01092006. I am not sure how to accomplish this in C#, being that I am no
longer dealing with a recorset and I'm not sure how to accomplish this using
a dataset or datareader.

VB Code:

Set rsRecordset = New Recordset
rsRecordset.Open (sSQL), adoConn, adOpenStatic, adLockOptimistic
Dim varNumber As Variant
Dim intDP As Integer
Dim sYY As String 'Pulls the year value from the recordset
Dim sMM As String 'Pulls the month value from the recordset
Dim sDD As String 'Pulls the day value from the recordset
Dim sAV As String 'Pulls the actual value from the recordset
Dim sIV As String 'Pulls the indicated value from the recordset
Dim iAV As String
Dim iIV As String
Dim iYY As String
Dim iMM As String
Dim iDD As String
sPrefix = ""

Do While Not rsRecordset.EOF
sYY = CStr(rsRecordset("YY")) 'the year
sMM = CStr(rsRecordset("MM")) 'the month
sDD = CStr(rsRecordset("DD")) 'the day
iYY = CInt(sYY)
iDD = CInt(sDD)
iMM = CInt(sMM)

If iYY < 10 Then sYY = "0" & iYY 'if the year digit is less then 10
add a zero
If iDD < 10 Then sDD = "0" & iDD 'same
If iMM < 10 Then sMM = "0" & iMM 'same

sAV = CStr(rsRecordset("AV")) 'Actual value
sIV = CStr(rsRecordset("IV")) 'Indicated value
iAV = CInt(sAV)
iIV = CInt(sIV)

If iAV < 10 Then sAV = "000" & CStr(rsRecordset("AV"))
If iIV < 10 Then sIV = "000" & CStr(rsRecordset("IV"))

If iAV >= 10 And iAV < 100 Then sAV = "00" & CStr(rsRecordset("AV"))
If iIV >= 10 And iAV < 100 Then sIV = "00" & CStr(rsRecordset("IV"))

If iAV >= 100 And iAV < 1000 Then sAV = "0" & CStr(rsRecordset("AV"))
If iIV >= 100 And iIV < 1000 Then sIV = "0" & CStr(rsRecordset("IV"))
Me.Text1.Text = Me.Text1.Text & sPrefix & "8" &
CStr(rsRecordset("ST")) & CStr(rsRecordset("CNTY")) _
& CStr(rsRecordset("SITE")) &
CStr(rsRecordset("PARMETER")) & CStr(rsRecordset("POC")) _
& CStr(rsRecordset("TMINT")) & "001" & "0" &
CStr(rsRecordset("MET")) _
& sYY & sMM & sDD & "0" & sAV & sIV & Space(41) & "I"
rsRecordset.MoveNext
sPrefix = vbCrLf
Loop
Thanks!

Joseph,

Like Matt says, except to get the date format you need (which I assume
is MMDDYYY and not DDMMYYYY) you can use this format string:
dt.ToString("MMddyyyy")

Where dt is the DateTime object Matt refers to.

-Jay

Nov 13 '06 #4

P: n/a
Joseph wrote:
Thanks for replying guys.

Additionally: Would you use a dataset or datareader to perfom this? Can you
provide a snippet of code that would demonstrate how to actually capture the
data from the database and put into a variable. Thanks

"Jay Riggs" wrote:

Joseph wrote:
Hi all-
>
I am a former VB6 programmer and new at C# and I have a question dealing
with converting some code from VB6 to C#. The code is below and essentially,
what it does is gets data from a SQL Server database and parses some of the
data and puts the parsed data into a text field. I omitted a some of the code
and left the data parsing part of it, which is what my question is about.
Exactly what the code below does is gets data from SQL Server and parses one
of the date fields. So if the date it retrieves is 1/9/2006, the parsing
outcome becomes:
01092006. I am not sure how to accomplish this in C#, being that I am no
longer dealing with a recorset and I'm not sure how to accomplish this using
a dataset or datareader.
>
VB Code:
>
Set rsRecordset = New Recordset
rsRecordset.Open (sSQL), adoConn, adOpenStatic, adLockOptimistic
>
>
Dim varNumber As Variant
Dim intDP As Integer
Dim sYY As String 'Pulls the year value from the recordset
Dim sMM As String 'Pulls the month value from the recordset
Dim sDD As String 'Pulls the day value from the recordset
Dim sAV As String 'Pulls the actual value from the recordset
Dim sIV As String 'Pulls the indicated value from the recordset
>
>
Dim iAV As String
Dim iIV As String
Dim iYY As String
Dim iMM As String
Dim iDD As String
>
>
sPrefix = ""
>
Do While Not rsRecordset.EOF
sYY = CStr(rsRecordset("YY")) 'the year
sMM = CStr(rsRecordset("MM")) 'the month
sDD = CStr(rsRecordset("DD")) 'the day
iYY = CInt(sYY)
iDD = CInt(sDD)
iMM = CInt(sMM)
>
If iYY < 10 Then sYY = "0" & iYY 'if the year digit is less then 10
add a zero
If iDD < 10 Then sDD = "0" & iDD 'same
If iMM < 10 Then sMM = "0" & iMM 'same
>
sAV = CStr(rsRecordset("AV")) 'Actual value
sIV = CStr(rsRecordset("IV")) 'Indicated value
>
>
iAV = CInt(sAV)
iIV = CInt(sIV)
>
>
>
If iAV < 10 Then sAV = "000" & CStr(rsRecordset("AV"))
If iIV < 10 Then sIV = "000" & CStr(rsRecordset("IV"))
>
If iAV >= 10 And iAV < 100 Then sAV = "00" & CStr(rsRecordset("AV"))
If iIV >= 10 And iAV < 100 Then sIV = "00" & CStr(rsRecordset("IV"))
>
If iAV >= 100 And iAV < 1000 Then sAV = "0" & CStr(rsRecordset("AV"))
If iIV >= 100 And iIV < 1000 Then sIV = "0" & CStr(rsRecordset("IV"))
>
>
Me.Text1.Text = Me.Text1.Text & sPrefix & "8" &
CStr(rsRecordset("ST")) & CStr(rsRecordset("CNTY")) _
& CStr(rsRecordset("SITE")) &
CStr(rsRecordset("PARMETER")) & CStr(rsRecordset("POC")) _
& CStr(rsRecordset("TMINT")) & "001" & "0" &
CStr(rsRecordset("MET")) _
& sYY & sMM & sDD & "0" & sAV & sIV & Space(41) & "I"
rsRecordset.MoveNext
sPrefix = vbCrLf
Loop
>
>
Thanks!
Joseph,

Like Matt says, except to get the date format you need (which I assume
is MMDDYYY and not DDMMYYYY) you can use this format string:
dt.ToString("MMddyyyy")

Where dt is the DateTime object Matt refers to.

-Jay
Hi Joseph,

I'd use a data reader.

Here's a sample project using a DataReader:
http://www.codeproject.com/dotnet/adonet_datareader.asp

See this link for a discussion of your options (see specifically the
section entitled "Working with DataReaders, DataSets, DataAdapters, and
DataViews"):
http://msdn2.microsoft.com/en-us/lib...netbest_topic3

See this link for a DataReader/ DataSet comparison:
http://msdn.microsoft.com/msdnmag/is...06/DataPoints/

HTH
-Jay

Nov 13 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.