ACCESS TO SQL

I tested below code and it worked. If I want to access SQL database, of course I need to replace all Oledab to SQLconnection, SQLcommand, Sqlreader, etc.  SQL does not use  =?, right. What should I replace it with.


Dim StrConnection As String = "provider=microsoft.jet.oledb.4.0;" + _

"data source = C:\Documents and Settings\JUSTATEST_DOM\Desktop\XMAN3\bad.mdb"




Dim Connection As New OleDbConnection(StrConnection)

Dim myCommand As New OleDbCommand("SELECT * FROM badtable where transno = ? ", Connection)

If Me.txtSearch.Text <> Nothing Then



Try



myCommand.Parameters.Add(New OleDb.OleDbParameter("Transno", Me.txtSearch.Text))

Connection.Open()

Dim myreader As OleDbDataReader = myCommand.ExecuteReader(CommandBehavior.SingleRow)

If myreader.HasRows Then

myreader.Read()

Me.txtAddress.Text = myreader.Item("address").ToString

Me.txtCity.Text = myreader.Item("citystate").ToString

Me.txtState.Text = myreader.Item("state").ToString

Me.txtZip.Text = myreader.Item("zip").ToString

Me.RichTextBox1.Text = myreader.Item("accurint").ToString



End If

Connection.Close()



Catch ex As OperationAbortedException

MsgBox(ex.ToString)



End Try

Else

MessageBox.Show("Please Enter Trannum", "Empty field", _

MessageBoxButtons.OK, MessageBoxIcon.Information)



End If


End Sub

VBdotnet2005Asked:
Who is Participating?
 
YZlatConnect With a Mentor Commented:
creates a parameter named @Transno of type string and of lenght about 50 characters
0
 
YZlatCommented:




Dim Conn As New SqlConnection(StrConnection)

Dim myCommand As New SqlCommand("SELECT * FROM badtable where transno = @Transno ", Conn)

If Me.txtSearch.Text <> Nothing Then



Try

myCommand.Parameters.Add("@Transno", SqlDbType.NVarChar, 50)
myCommand.Parameters("@Transno").Value = Me.txtSearch.Text



Conn.Open()

Dim myreader As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.SingleRow)

If myreader.HasRows Then

myreader.Read()

Me.txtAddress.Text = myreader.Item("address").ToString

Me.txtCity.Text = myreader.Item("citystate").ToString

Me.txtState.Text = myreader.Item("state").ToString

Me.txtZip.Text = myreader.Item("zip").ToString

Me.RichTextBox1.Text = myreader.Item("accurint").ToString



End If

Conn.Close()



Catch ex As OperationAbortedException

MsgBox(ex.ToString)



End Try

Else

MessageBox.Show("Please Enter Trannum", "Empty field", _

MessageBoxButtons.OK, MessageBoxIcon.Information)



End If


End Sub
0
 
VBdotnet2005Author Commented:
What does this line do?

myCommand.Parameters.Add("@Transno", SqlDbType.NVarChar, 50
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.