I've got a big query which gives me a timeout. I don't want to configure the Database configurations so my Idea is to split the query into smaller.
Is it possible to iterate (for example: over 500 Data sets), put it in a loop and then Update always 500 Data Sets till the Table count?
If so, suggestions how to code it?
My SQL Executioner:
If Connection.State = Data.ConnectionState.Open Then
myCMD = New OracleCommand(sSQL, Connection)
myCMD.CommandTimeout = 30
myReader = myCMD.ExecuteReader()
While myReader.Read
Dim row = New ArrayList
For i = 0 To myReader.FieldCount - 1
row.Add(myReader(i))
Next
resultSet.Add(row)
End While
myReader.Close()
Related
I am working on a visual basic project. I have a mdb database connected to my project. I want to add a SELECT query that finds the results which are in array that i give it on my program
I have tried to write a statement like that:
SELECT kodu, adi_soyadi, sectigi_ders_say
FROM ogrenciler
WHERE kodu IN ?
But it does not work. In my page codes I have an array and I want to find results from "ogrenciler" table where the "kodu" is in my array.
Well, you could send that array to a temp table in Access, but that would prevent more then one user using the software at the same time. (or you could add some user name to the temp table. However, if the array of choices is small, say about max 50, then you can create the sql string.
eg:
Dim MySQL As String = "SELECT * from tblHotels WHERE ID IN("
Dim IdList(5) As Integer
Dim i As Integer
For i = 1 To 5
IdList(i) = i
Next
Dim MyList As String = ""
For i = 1 To 5
If MyList <> "" Then MyList = MyList & ","
MyList = MyList & IdList(i)
Next
MySQL = MySQL & MyList & ")"
Using MyCon2 As New OleDbConnection(My.Settings.OLESQL)
Dim da As New OleDbDataAdapter(MySQL, MyCon2)
Dim rstDat As New DataTable()
da.Fill(rstDat)
For i = 0 To rstDat.Rows.Count - 1
Debug.Print(rstDat.Rows(i).Item("HotelName"))
Next ' etc etc. etc.
End Using
So you can use the SQL format of:
SELECT * FROM tblHotels where ID IN (1,2,3)
And thus build up the "list". The only downside to this approach is that the sql string is limited to 2000 characters. So, if your list is larger then say 50 or so items, then you have to adopt a different approach.
I have a part of code in VB6 which i'm trying to convert to VB.net. Specifically recordsets.
This is a part of VB6 code:
Data19.RecordSource = "select id from headers where type=12"
Data19.Refresh
If Data19.Recordset.RecordCount > 0 Then
Data6.RecordSource = "select sum(left * right) from footers where type=12"
Data6.Refresh
ss = Format(Data6.Recordset.Fields(0), "0.00")
Data19.Recordset.MoveLast
a = Data19.Recordset.RecordCount - 1
Data19.Recordset.MoveFirst
For i = 0 To a
If i > 0 Then Data19.Recordset.MoveNext
Data22.RecordSource = "select * from documents where type=12"
Data19.Recordset.Fields(0)
Data22.Refresh
With Data22.Recordset
If .RecordCount > 0 Then
.MoveLast
b = .RecordCount - 1
.MoveFirst
For j = 0 To b
If j > 0 Then .MoveNext
If .Fields("link1") < ra And .Fields("code") <> "00" Then
.Edit
.Fields("link1") = ra
.Fields("pc") = Format(.Fields("dpc") * (100 - ra) / 100, "0.00")
.Fields("total") = Format(.Fields("amount") * .Fields("dpc") * (100 - ra) / 100, "0.00")
.Update
End If
Next
End If
End With
Next
End If
I am a bit confused about .MoveLast, .MoveFirst,.Recordset.
In my VB.net code i have been using this function to get the table that i want from the database:
Public Function returnTable(ByVal queryString As String)
Dim query1 As String = queryString
'Console.WriteLine(query1)
Dim table As New DataTable
Using connection As New MySqlConnection(connection)
Using adapter As New MySqlDataAdapter(query1, connection)
Dim cmb As New MySqlCommandBuilder(adapter)
table.Clear()
adapter.Fill(table)
Return table
End Using
End Using
End Function
So the part with recordsources should go something like this if i'm not wrong:
Dim data19 as new datatable
Data19 = returnTable("select id from headers where type=12")
But later on, in the code, i cannot figure out how to write the equivalents to .MoveLast, .MoveFirst,.Recordset,.MoveNext, etc...
It looks like you're trying to write the VB.Net equivalents to VB6 .MoveLast, .MoveFirst, .Recordset, .MoveNext, etc.
.Recordset
You're very close to your solution with the returnTable function that you created. Datatables are just collections of DataRow objects, which is a bit different than recordset objects in VB6. You created a DataTable with your .Net function:
Dim data19 as new datatable
Data19 = returnTable("select id from headers where type=12")
In this case, Data19 is a DataTable that takes the place of a recordset in the VB6 example.
.MoveLast
In your VB6 example, the reason for using .MoveLast is to expose the count of records in the recordset. The count of records is not known until you move to the last record.
After you move to the last record, then you can load the count into a variable.
With ADO.Net, you don't need to use .MoveLast to get the count. You can simply get the row count like this:
Dim row_count As Integer = Data19.Rows.Count
You'll see below that this variable is not needed when you convert to .Net. You use it in VB6 to know how many records to loop through (and when to stop). In .Net you will use For Each.. Next to achieve the same purpose.
.MoveFirst
For your example, .MoveFirst is used only because you used .MoveLast to get the record count. In order to walk through the recordset, you have to go back to the first record.
Since you no longer need to use .MoveLast in .Net, you also don't need to use .MoveFirst.
.MoveNext
In your VB6 example, .MoveNext is used to walk through the recordset and do some actions on each row. To walk through the DataTable that you created, you can do something like this:
Dim my_row as DataRow
For Each my_row in Data19.Rows
If my_row("link1") < ra And my_row("code") <> "00" Then
.. do some actions
End If
Next
This will walk through the recordset in a similar fashion. One thing to consider is that you are working with a disconnected recordset when you use your DataTable. When you get to the .Edit and .Update parts of your VB6 procedure, you might need to use a parameterized query to perform the actual update to any records. This will use a command object and .ExecuteNonQuery() method to perform an SQL update statement.
Can you tell me how to get ages from 0-5 and so on from sql server and input the total in a textbox or label? please click the link for sample
Generate Ages
thank you!
Do you mean something like
Dim sql As String = "SELECT age_column FROM your_table WHERE age_column BETWEEN '0' AND '5'"
'The above line selects the all records where age is between 0-5 (Change the variables)
Dim da As New OleDbDataAdapter(sql, connectionstring)
Dim ds As New DataSet
Dim dt as new DataTable
da.Fill(ds)
dt = ds.Tables(0).Copy()
' Above code stores the results in an iterable table
Dim i As Integer = 0
For Each dr as DataRow in dt.Rows
i = i + 1
Next
TextBox1.Text = i
'i will be the total number of records where age is between 0-5, and display it in a `TextBox`
Your question is, however, very poor, and normally people would not put too much effort into answering it, since you've put no effort in yourself.
I am writing a program for my lab. Based on a salesordernumer (SO-nr) I need to find the corresponding part number out of a table in access and put it into a variable( Prob a string?). later I need to split the different parts from the partnumber but before that I need to get it out of the MS table. This is de code I use now but I get an error.
Private Sub BtnOphaal_Click(sender As Object, e As EventArgs) Handles BtnOphaal.Click
If conn.State = ConnectionState.Open Then
Dim Sonr As String
Sonr = "SELECT *FROM prodvolg "
Dim SQL As New OleDb.OleDbCommand(Sonr, conn)
Dim DataAdapter As New OleDb.OleDbDataAdapter(SQL)
Dim datatabel As New DataTable("prodvolg")
DataAdapter.Fill(datatabel)
Dim queryString As String = "SELECT [pPart] AS Partnummer FROM [prodvolg] WHERE ([pSonr]='" & txtSOnummer.Text & "')"
Dim command As New OleDbCommand(queryString, conn)
Dim reader As OleDbDataReader = command.ExecuteReader()
While reader.Read()
txtppart.Text = (reader.GetString(1))
End While
reader.Close()
End If
End Sub
As you can see I'm just a beginning programmer.
The error is occuring at txtppart.Text = (reader.GetString(1)) the error message:
A first chance exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll
Additional information: Index was outside the bounds of the array.
at school I learned programming at ADO system but the future seems to be oledb but I don't understand the OLEdb system good so far. If anyone could help me I would be so happy.
The partnumber is could look like this: "CA-017630-6.35M-1/0-2"
Arrays start at index zero, not one. You have only one field returned by the query
txtppart.Text = (reader.GetString(0))
Also, keep in mind that GetString could fail if your row at index zero contains a null value.
If this is the case I suggest to check for null values with
txtppart.Text = IF(reader.IsDBNull(0), string.Empty, reader.GetString(0))
You are retrieving just one field pPart with your SQL statement, so you'll have just one field on the DataReader. Try with reader.GetString(0), because the arrays starts at index zero
Lets say I have a dataset with I dunno 20 records in it.
How do I get to the 16th record in the dataset or the Nth? So any number from 1 to 20
I have spent most of this week trying to think of a method and so far I have gotten no where.
I want the 16th record down in this dataset and there doesn't seem to be a specific command for it.
I'm working in VB.NET with OLE commands.
I'm not sure any code would be of any use to help solve this but I'm populating the dataset something like this:
SQL_Str = "SELECT FROM A TABLE WHERE CRITERIA IS MET"
dbDataAdapter = New OleDbDataAdapter(SQL_Str, dbConnector)
dbDataAdapter.Fill(DataSet, "SelectedRecords")
Now what do I do to get to the 16th row in this dataset knowing that there is 20 records in the dataset?
Since you say you are using VB.NET, just read the row from the dataSet.
Private Function GetRow(ByVal ds As Data.DataSet, ByVal rowNum As Integer) As Data.DataRow
Dim result As Data.DataRow = Nothing
Dim table As Data.DataTable = ds.Tables(0)
result = table.Rows(rowNum)
Return result
End Function
There are overloads to DataSet.Tables: Consider: