?
Solved

ACCESS TO SQL

Posted on 2006-05-16
3
Medium Priority
?
242 Views
Last Modified: 2008-03-10
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

0
Comment
Question by:VBdotnet2005
  • 2
3 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 16691530




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
 

Author Comment

by:VBdotnet2005
ID: 16691602
What does this line do?

myCommand.Parameters.Add("@Transno", SqlDbType.NVarChar, 50
0
 
LVL 35

Accepted Solution

by:
YZlat earned 1000 total points
ID: 16692860
creates a parameter named @Transno of type string and of lenght about 50 characters
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question