SqlDataReader read whole query - sql

I have a SQL script with a loop. In each iteration I change the where clause. So I get several selects displayed. But my .net program reads only the first select. The SQL script works in ssms.
This is my SQL script
while (#aktuellParam <= #countParam)
Begin
SELECT name1,
name2
FROM dbo.tableName
WHERE name like #var
SET #aktuellParam = aktuellParam+1
END
This is my vb.net code
Using reader As SqlClient.SqlDataReader = _server.ConnectionContext.ExecuteReader(script)
Dim lfdnr As Integer = 1
Do While reader.Read()
spaltenListe.Add(New ISpalten With
{
.Name1= reader.GetString(reader.GetOrdinal("name1")),
.Name2= reader.GetString(reader.GetOrdinal("name2"))
})
lfdnr = lfdnr + 1
Loop
reader.Close()
End Using

That's because subsequent selects are actually in a different result set. Your reader needs to do a .NextResult after each read.

Related

Naming Column Header Based On Results From Database

net and would to have the Header Text of columns in a datagridview be named after results from the database, e.g the query in my code returns four dates,30/08/2017,04/09/2017,21/09/2017 and 03/02/2018. My aim is to have the column headers in the data grid named after those dates. Your help will highly be appreciated.
sql = "SELECT COUNT (ServiceDate) As NoOfServiceDates FROM (SELECT DISTINCT ServiceDate FROM tblattendance)"
Using command = New OleDbCommand(sql, connection)
Using reader = command.ExecuteReader
reader.Read()
ColumnNo = CInt(reader("NoOfServiceDates")).ToString
End Using
End Using
DataGridView1.ColumnCount = ColumnNo
For i = 0 To DataGridView1.Columns.Count - 1
sql = "SELECT DISTINCT ServiceDate FROM tblattendance"
Using command = New OleDbCommand(sql, connection)
Using reader = command.ExecuteReader
While reader.Read
DataGridView1.Columns(i).HeaderText = reader("ServiceDate").ToString
End While
End Using
End Using
Next
The current code re-runs the query each time through the column count loop, meaning it will set the column header for that column to all of the date values in sequence, so the last value in the query shows in the all the columns. You only need to run the query once:
Dim i As Integer = 0
sql = "SELECT DISTINCT ServiceDate FROM tblattendance"
Using command As New OleDbCommand(sql, connection), _
reader As OleDbDatareader = command.ExecuteReader()
While reader.Read
DataGridView1.Columns(i).HeaderText = reader("ServiceDate").ToString
i+= 1
End While
End Using
Additionally, this still results in two separate trips to the database, where you go once to get the count and again to get the values. Not only is this very bad for performance, it leaves you open to a bug where another user changes your data from one query to the next.
There are several ways you can get this down to one trip to the database: loading the results into memory via a List or DataTable, changing the SQL to include the count and the values together, or adding a new column each time through the list. Here's an example using the last option:
DataGridView1.Columns.Clear()
Dim sql As String = "SELECT DISTINCT ServiceDate FROM tblattendance"
Using connection As New OleDbConnection("string here"), _
command As New OleDbCommand(sql, connection)
connection.Open()
Using reader As OleDbDataReader = command.ExecuteReader()
While reader.Read
Dim column As String = reader("ServiceDate").ToString()
DataGridView1.Columns.Add(column, column)
End While
End Using
End Using
Even better if you can use something like Sql Server's PIVOT keyword in combination with the DataGridView's AutoGenerateColumns feature for DataBinding, where you will write ONE SQL statement that has both column info and data, and simply bind the result set to the grid.
The For Next is incorrect. You execute your command for every column, when you only need to execute it once. The last result from the DataReader will be the header for every column as currently written.
You should iterate through your DataReader and increment the cursor variable there:
Dim i As Integer = 0
Using command = New OleDbCommand(sql, connection)
Using reader = command.ExecuteReader
While reader.Read
DataGridView1.Columns(i).HeaderText = reader("ServiceDate").ToString
i += 1
End While
End Using
End Using

Trying to use Group to get Max on two columns with Linq

I've been translating my programs to use Entity Framework 6 for the last two days. For the most part I have it down with simple CRUD operations. Today I hit a snag. I'm trying to convert;
Using SQLSERVER_Connection As New SqlConnection(GlobalVariables.SQLServer_Login_Details)
Using SQLCommand As New SqlCommand
Dim Command As New Text.StringBuilder
Command.AppendLine("SELECT TOP(1) EarningsYear AS PayYear, Max(EarningsAmt) AS EarnAmt, Max(Hours) AS HRS")
Command.AppendLine("FROM interview_payroll")
Command.AppendLine("GROUP BY CLIENTCODE, EarningsYear, SSN")
Command.AppendLine("HAVING CLIENTCODE = #CLIENTCODE AND SSN = #SSN ORDER BY EarningsYear DESC;")
SQLCommand.CommandText = Command.ToString
SQLCommand.Connection = SQLSERVER_Connection
SQLCommand.Parameters.Add("#CLIENTCODE", SqlDbType.VarChar).Value = sCLIENTCODE
SQLCommand.Parameters.Add("#SSN", SqlDbType.VarChar).Value = txtSSN.Text
SQLSERVER_Connection.Open()
Using Reader As SqlDataReader = SQLCommand.ExecuteReader()
While Reader.Read()
If Not IsDBNull(Reader("PayYear")) Then txtPayrollSourceDate.Text = CStr(Reader("PayYear"))
If Not IsDBNull(Reader("EarnAmt")) Then txtPayrollErnAmt.Text = CStr(Reader("EarnAmt"))
If Not IsDBNull(Reader("HRS")) Then txtPayrollHRS.Text = CStr(Reader("HRS"))
End While
End Using
SQLSERVER_Connection.Close()
End Using
End Using
into Linq.
I've got as far as;
Using DB As New wotcEntities
Dim Reader = From payroll In DB.interview_payroll
Where payroll.CONTROL = CONTROL And payroll.CLIENTCODE = sCLIENTCODE
Group payroll By payroll.CLIENTCODE, payroll.EarningsYear, payroll.SSN Into GPayroll = Group
End Using
But I just can't seem to jump the hurdle to get the MAX in EarningsAmt and Hours. I have this program called 'LINQ - Sample Queries' but it shows a MAX query as;
Public Sub LinqToSqlCount08()
Dim latestHire = Aggregate emp In db.Employees _
Into Max(emp.HireDate)
Console.WriteLine(latestHire)
End Sub
Which isn't going to work if I want to include PayYear.
I could just grab each part as separate queries, but that doesn't feel right.
How do I do this correctly?
I highly recommend the application "LINQPad". It can really help with writing queries and seeing the results immediately.
I created a table similar to what I could gather from your query, and was able to write this query which I believe does what you're asking for.
The "select" is where the Max aggregates are applied.
from payroll In Interview_payrolls
where payroll.CLIENTCODE = sClientCode
group payroll By payroll.CLIENTCODE, payroll.EarningsYear, payroll.SSN Into GPayroll = group
select PayYear = EarningsYear, EarnAmt = GPayroll.Max(Function(p) p.EarningsAmt), HRS = GPayroll.Max(Function(p) p.Hours)
Hope it works for you.

SQL variable returning 0

When I run the following SQL statement in SQL Server Management Studio it returns a count of 2
SELECT COUNT(*)
FROM Daisy_Copy2
WHERE ChargeCode = '1';
But for some reason when I run the following VB.net code the result variable returns a 0 and doesn't identify that duplicate codes exist.
Dim result As Integer
Using cmdb = New SqlCommand("SELECT COUNT(*) FROM Daisy_Copy2 WHERE ChargeCode = '1'", conn)
Int(result = cmdb.ExecuteScalar())
If result > 1 Then
MessageBox.Show("Duplicate Codes Exist!", "Billing", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MsgBox(result)
End If
End Using
Can anyone help me understand why?
Any help greatly appreciated.
Instead of ExecuteNonQuery you should use ExecuteScalar
Dim result As Integer = CInt(cmd.ExecuteScalar())
ExecuteNonQuery is normally used for updates or inserts that don't leave a result, so it returns an integer telling you how many rows were affected, not the result itself.
What you most likely are meaning to use is ExecuteScalar which returns the first column of the first row in the result set returned by the query, in this case the integer containing your count.
this is just a way you can use:
Dim Sqlda = New SqlDataAdapter("SELECT COUNT(*) AS tCount FROM Daisy_Copy2 WHERE ChargeCode=1", conn)
Dim sqlds = New DataSet
Sqlda.Fill(sqlds, "Daisy_Copy2")
Dim tblRow As DataRow
For Each tblRow In sqlds.Tables("Daisy_Copy2").Rows
MsgBox(tblRow("tCount").ToString())
Next
use below link to read more about it
System.Data.SqlClient Namespace
Good luck

Vb.net pull in a SQL table row by row

I am a little new to using vb.net and SQL so I figured I would check with you guys to see if what I am doing makes sense, or if there is a better way. For the first step I need to read in all the rows from a couple of tables and store the data in the way the code needs to see it. First I get a count:
mysqlCommand = New SQLCommand("SELECT COUNT(*) From TableName")
Try
SQLConnection.Open()
count = myCommand.ExecuteScalar()
Catch ex As SqlException
Finally
SQLConnection.Close()
End Try
Next
Now I just want to iterate through the rows, but I am having a hard time with two parts, First, I cannot figure out the SELECT statement that will jet me grab a particular row of the table. I saw the example here, How to select the nth row in a SQL database table?. However, this was how to do it in SQL only, but I was not sure how well that would translate over to a vb.net call.
Second, in the above mycommand.ExecuteScalar() tell VB that we expect a number back from this. I believe the select statement will return a DataRow, but I do not know which Execute() statement tells the script to expect that.
Thank you in advance.
A simple approach is using a DataTable which you iterate row by row. You can use a DataAdapter to fill it. Use the Using-statement to dispose/close objects property that implement IDisposable like the connection:
Dim table = New DataTable
Using sqlConnection = New SqlConnection("ConnectionString")
Using da = New SqlDataAdapter("SELECT Column1, Column2, ColumnX FROM TableName ORDER By Column1", sqlConnection)
' you dont need to open/close the connection with a DataAdapter '
da.Fill(table)
End Using
End Using
Now you can iterate all rows with a loop:
For Each row As DataRow In table.Rows
Dim col1 As Int32 = row.Field(Of Int32)(0)
Dim col2 As String = row.Field(Of String)("Column1")
' ...'
Next
or use the table as DataSource for a databound control.

VB.NET Multiple Selects at once using SQL Server CE

I have an array list which contains ids for some items. I would like to perform a multiple select at once from a SQL Server CE database and using my array list which contains what items id to be selected, something similar when doing for example multiple update in oracle (ODP.NET) as explained here: Oracle bulk updates using ODP.NET
where you can pass an array as a parameter.
I would like to do the same but for a multiple select instead in case of SQL Server CE. Is it possible?
DRAFT about what I would like to do:
SqlCeCommand = SqlCeConnection.CreateCommand()
SqlCeCommand.CommandText = "SELECT * FROM MyTable WHERE Id=:ids"
SqlCeCommand.CommandType = CommandType.Text
SqlCeCommand.Parameters.Add(":ids", DbType.Int32, ArrayListOfIds, ParameterDirection.Input)
Using reader As System.Data.SqlServerCe.SqlCeDataReader = SqlCeCommand.ExecuteReader()
Using targetDb As Oracle.DataAccess.Client.OracleBulkCopy = New Oracle.DataAccess.Client.OracleBulkCopy(con.ConnectionString)
targetDb.DestinationTableName = "MyTable"
targetDb.BatchSize = 100
targetDb.NotifyAfter = 100
targetDb.BulkCopyOptions = Oracle.DataAccess.Client.OracleBulkCopyOptions.UseInternalTransaction
AddHandler targetDb.OracleRowsCopied, AddressOf OnOracleRowsCopied targetDb.WriteToServer(reader)
targetDb.Close()
End Using
reader.Close()
End Using
You should try this approach by constructing your "IN" clause and adding each parameter in a for each loop:
SqlCeCommand = SqlCeConnection.CreateCommand()
SqlCeCommand.CommandType = CommandType.Text
Dim sb As New StringBuilder()
Dim i As Integer = 1
For Each id As Integer In ArrayListOfIds
' IN clause
sb.Append("#Id" & i.ToString() & ",")
' parameter
SqlCeCommand.Parameters.Add("#Id" & i.ToString(), DbType.Int32, id, ParameterDirection.Input)
i += 1
Next
If you're calling a Stored Procedure, you can do this:
Serialize the array to a string of XML, like this: https://stackoverflow.com/a/6937351/734914
Call the stored procedure, passing in the string parameter
Parse the string of XML into a local table variable containing the ID's, like this: https://stackoverflow.com/a/8046830/734914
Execute whatever queries you need to using the ID's
The links that I referenced might not be the best examples on the web, but the concept of "serialize to XML, pass string parameter, deserialize XML" should work here