473,385 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Oledb????

Fia
Hi
I'm trying to use Oledb in Visual Basic .Net, but it doesn't work so well.
Before in Visual Basic 6 I had the code below that worked.

Dim tblSökaOrd As DAO.Recordset
dim DB As Database
Set DB = Worksp.OpenDatabase(App.Path & "\recept.mdb", False, False)
SQL = "select köttid from kött where köttid like '" & PubStr & "*'order by
köttid ;"
Set tblSökaOrd = DB.OpenRecordset(SQL, dbOpenSnapshot)
If tblSökaOrd.RecordCount <> 0 Then
'This code happens
end if

In Visual Basic .Net I have tried this code, but it doesnt work
Dim sökaOrdCom As New OleDbCommand(), SQL As String
Dim sökaOrdRead As OleDbDataReader
Dim field As New OleDbParameter()
SQL = "select köttid from kött where köttid like ? order by köttid ;"
sökaOrdCom.CommandText = SQL
sökaOrdCom.Parameters.Add(field)
sökaOrdCom.Parameters("köttid").Value = "'" & PubStr & "*'"
sökaOrdCom.Connection = DBConn
sökaOrdRead = sökaOrdCom.ExecuteReader
Do While sökaOrdRead.Read
recCount += 1
Loop
sökaOrdRead.Close()
If recCount <> 0 Then
' This code never happens
end if

I don't get any records from the while loop but I know I have records. I
have read in the help files that when you have a query with filter (where)
you should do someting like this, I think.
Can anyone help me please.
I have another question also, I don't understand When and Why I have to use
the keyword New. In the declaration for sökaOrdCom I have to use New but
when I declarate sökaOrdRead I don't have to use New. And also in Visual
Basic 6 we had the property RecordCount, do we have to use a while loop and
increment an integer to get the querie's recordcount?

Greatful for answers
Fia

Nov 21 '05 #1
1 1482
Hi,

Couple of things first remember with ado.net % does what * did with
jet. You dont need to create a new datareader because executereader returns
a datareader. Your sql statement needs to be changed. Here is a working
example.

Dim conn As OleDbConnection

Dim strConn As String

Dim drCustomer As OleDbDataReader

Dim cmd As OleDbCommand

Dim ds As New DataSet

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

strConn &= "Data Source = C:\Northwind.mdb;"

conn = New OleDbConnection(strConn)

cmd = New OleDbCommand("Select * from Customers where CustomerID Like
@MyId", conn)

conn.Open()

cmd.Parameters.Add("@MyId", "A%")

Trace.WriteLine(cmd.CommandText)

drCustomer = cmd.ExecuteReader

Do While drCustomer.Read

Trace.WriteLine(drCustomer.Item("CustomerID").ToSt ring)

Loop

conn.Close()

Ken

--------------------

"Fia" <fi*****@telia.com> wrote in message
news:uG**************@TK2MSFTNGP15.phx.gbl...
Hi
I'm trying to use Oledb in Visual Basic .Net, but it doesn't work so well.
Before in Visual Basic 6 I had the code below that worked.

Dim tblSökaOrd As DAO.Recordset
dim DB As Database
Set DB = Worksp.OpenDatabase(App.Path & "\recept.mdb", False, False)
SQL = "select köttid from kött where köttid like '" & PubStr & "*'order by
köttid ;"
Set tblSökaOrd = DB.OpenRecordset(SQL, dbOpenSnapshot)
If tblSökaOrd.RecordCount <> 0 Then
'This code happens
end if

In Visual Basic .Net I have tried this code, but it doesnt work
Dim sökaOrdCom As New OleDbCommand(), SQL As String
Dim sökaOrdRead As OleDbDataReader
Dim field As New OleDbParameter()
SQL = "select köttid from kött where köttid like ? order by köttid ;"
sökaOrdCom.CommandText = SQL
sökaOrdCom.Parameters.Add(field)
sökaOrdCom.Parameters("köttid").Value = "'" & PubStr & "*'"
sökaOrdCom.Connection = DBConn
sökaOrdRead = sökaOrdCom.ExecuteReader
Do While sökaOrdRead.Read
recCount += 1
Loop
sökaOrdRead.Close()
If recCount <> 0 Then
' This code never happens
end if

I don't get any records from the while loop but I know I have records. I
have read in the help files that when you have a query with filter (where)
you should do someting like this, I think.
Can anyone help me please.
I have another question also, I don't understand When and Why I have to use
the keyword New. In the declaration for sökaOrdCom I have to use New but
when I declarate sökaOrdRead I don't have to use New. And also in Visual
Basic 6 we had the property RecordCount, do we have to use a while loop and
increment an integer to get the querie's recordcount?

Greatful for answers
Fia


Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Error while executing SP | last post by:
Hi, I am getting an error while executing a sp from Oracle database. Can you let me know what could be the problem? here is the code using(System.Data.OleDb.OleDbConnection cn = new...
3
by: Chris Thunell | last post by:
I have a program where i connect to an access database and get some information out of it. I'm using the oledb.3.51 version, my computer doesn't seem to have the 4.0 version, but the computer that...
12
by: Parking Meters | last post by:
Today I managed to get the OleDB provider to let me put datasets from an as400 using sql. Great, the provider string I used is: Provider=IBMDA400.DataSource.1;User ID=XXXXX;Password=XXXX;Data...
4
by: NS | last post by:
Hi, I am trying to execute a prepare statement using oledb provider for DB2. The command.Prepare() statement is giving me an exception " No error information available:...
2
by: Joe | last post by:
Hi All, I am new to using the Access DB and I need some help if someone is able to give it to me. What I want to do is get the names of the columns of certain tables. Not the data in the table...
9
by: Pam Ammond | last post by:
I need the code to update the database when Save is clicked and a text field has changed. This should be very easy since I used Microsoft's wizards for the OleDBAdapter and OleDBConnection, and...
1
by: T8 | last post by:
I have a asp.net (framework 1.1) site interfacing against SQL 2000. It runs like a charm 99% of the time but once in a while I get the following "unspecified error". Sometimes it would resolve by...
3
by: Brian Foree | last post by:
I am developing an ASP.NET application that uses Access 2000 as its backend, and have just started getting the following error on 2 ASP.NET pages that had been working until late last week (and I...
2
by: Martin | last post by:
Hi, I currently have an application that connects to an MS ACCESS database. This application uses an OLEDB connection string for MS ACCESS. Now, I'd like to upsize the application so I converted...
0
by: NicK chlam via DotNetMonster.com | last post by:
this is the error i get System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. at System.Data.Common.DbDataAdapter.Update(DataRow dataRows, DataTableMapping tableMapping) at...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.