ranhell
asked on
How do I get the value from Sql command
I have the following code Sub on an Visual Studio 2008 project.
How do I assigned the result value of the SQL Query to a variable
I'm new into to datasets so I'm trying to understand them a little
I think I need to create a dataset and fill it with the resulst of the query and search trhough there.
I was just wondering if there is a straight way of doing this without creating a data set
sample code expected!!
Private Sub CHECKMAXID()
Dim cmd As OleDb.OleDbCommand
sSql = ""
sSql = "SELECT MAX(ID) FROM MASTER"
If Con.State = ConnectionState.Closed Then Con.Open()
cmd = New OleDb.OleDbCommand(sSql, Con)
cmd.ExecuteNonQuery()
End Sub
How do I assigned the result value of the SQL Query to a variable
I'm new into to datasets so I'm trying to understand them a little
I think I need to create a dataset and fill it with the resulst of the query and search trhough there.
I was just wondering if there is a straight way of doing this without creating a data set
sample code expected!!
Private Sub CHECKMAXID()
Dim cmd As OleDb.OleDbCommand
sSql = ""
sSql = "SELECT MAX(ID) FROM MASTER"
If Con.State = ConnectionState.Closed Then Con.Open()
cmd = New OleDb.OleDbCommand(sSql, Con)
cmd.ExecuteNonQuery()
End Sub
yes instead of cmd.ExecuteNonQuery()
you can have somthn like
Dim retValue as Integer = cmd.ExecuteScalar()
you can have somthn like
Dim retValue as Integer = cmd.ExecuteScalar()
ASKER
thanks both for UR help it worked just fine.
I will assigned the points anyways
Now, I don't mean to be excesive here by asking a related question
but If you don't mind, can I ask a couple o question since I kind new to VB.net,
I come from the VB 6 sintax, So
1.-What is the concept behind
cmd.ExecuteNonQuery() and cmd.ExecuteScalar()
Where and Why should I use these commands.
2.-Is there any other method other than SQL commands for inserting , updateing records etc.. or is VB2008 based on SQL Comands, the thing is that I come from VB6 using Index, Seek, Addnew instrunctions using Recordsets.
Also could you take a look at another question I have open and see I you have any suggestions
https://www.experts-exchange.com/questions/23833316/Visual-Studio-DataGridView-change-values.html
I will assigned the points anyways
Now, I don't mean to be excesive here by asking a related question
but If you don't mind, can I ask a couple o question since I kind new to VB.net,
I come from the VB 6 sintax, So
1.-What is the concept behind
cmd.ExecuteNonQuery() and cmd.ExecuteScalar()
Where and Why should I use these commands.
2.-Is there any other method other than SQL commands for inserting , updateing records etc.. or is VB2008 based on SQL Comands, the thing is that I come from VB6 using Index, Seek, Addnew instrunctions using Recordsets.
Also could you take a look at another question I have open and see I you have any suggestions
https://www.experts-exchange.com/questions/23833316/Visual-Studio-DataGridView-change-values.html
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Last question
Could you give me a brief explanation of Why and Where should I use Fill.Datasets
compare to using Recordsets back in VB 6, or is there good comparison chart or tutorial that approaches this question.
I already assigned the points, is mostly a follow up question.
thank you very much for UR help
Could you give me a brief explanation of Why and Where should I use Fill.Datasets
compare to using Recordsets back in VB 6, or is there good comparison chart or tutorial that approaches this question.
I already assigned the points, is mostly a follow up question.
thank you very much for UR help
Datasets are good when you need to read and update data like if you fill a datagridview and you want to allow users to change and save the data.
Using ExecuteNonQuery is for update/delete.
SqlDataReader is good for read only data pulls like populating a dropdown list.
Use ExecuteScalar when you only want a single value read.
Use datasets when you need to handle more advanced database-like tasks.
Using ExecuteNonQuery is for update/delete.
SqlDataReader is good for read only data pulls like populating a dropdown list.
Use ExecuteScalar when you only want a single value read.
Use datasets when you need to handle more advanced database-like tasks.
Basically, you can use DataSets and/or DataTables whenever you are working with data. It actually has more benefit with Windows forms than ASP.NET.
You can think of your DataTable as an in-memory representation of the data that is in the database. You can insert new records into it, modify existing records, and delete existing records. But, because the data is only in memory, it doesn't actually make any changes to the database.
You can use a DataAdapter to work with the data in a database and the data in a dataset. The DataAdapter is kind of like a "go-between" between the database and dataset. You can use the DataAdapter to get the data from the database and fill the dataset with it (SELECT command), and you can use it to UPDATE your database with the changes made to the dataset, DELETE records and INSERT new records as well.
Here's a little example:
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
SelectRecords()
End Sub
Private Sub SelectRecords()
'Select the records from the database
Dim SQL As String = "SELECT * FROM Employees"
Dim cnString As String = "Data Source=localhost;Initial Catalog=Northwind;Integrat ed Security=SSPI"
Dim table As New DataTable()
Using adapter As New SqlClient.SqlDataAdapter(S QL, cnString)
adapter.Fill(table)
End Using
Me.DataGridView1.DataSourc e = table
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
ModifyRecords()
SelectRecords()
End Sub
Private Sub ModifyRecords()
'Modify the records in the database
Dim cnString As String = "Data Source=localhost;Initial Catalog=Northwind;Integrat ed Security=SSPI"
Dim cn As New SqlClient.SqlConnection(cn String)
Dim table As New DataTable()
Dim adapter As New SqlClient.SqlDataAdapter()
adapter.SelectCommand = New SqlClient.SqlCommand()
With adapter.SelectCommand
.CommandText = "SELECT * FROM Employees"
.CommandType = CommandType.Text
.Connection = cn
End With
adapter.UpdateCommand = New SqlClient.SqlCommand()
With adapter.UpdateCommand
.CommandText = "UPDATE Employees SET FirstName=@FirstName, LastName=@LastName " & _
"WHERE EmployeeId=@EmployeeId"
.CommandType = CommandType.Text
.Connection = cn
With .Parameters
.Add("@EmployeeId", SqlDbType.Int, 0, "EmployeeId")
.Add("@FirstName", SqlDbType.VarChar, 0, "FirstName")
.Add("@LastName", SqlDbType.VarChar, 0, "LastName")
End With
End With
adapter.InsertCommand = New SqlClient.SqlCommand()
With adapter.InsertCommand
.CommandText = "INSERT INTO Employees (FirstName, LastName) " & _
"VALUES (@FirstName, @LastName)"
.CommandType = CommandType.Text
.Connection = cn
With .Parameters
.Add("@FirstName", SqlDbType.VarChar, 0, "FirstName")
.Add("@LastName", SqlDbType.VarChar, 0, "LastName")
End With
End With
adapter.DeleteCommand = New SqlClient.SqlCommand()
With adapter.DeleteCommand
.CommandText = "DELETE FROM Employees WHERE EmployeeId = @EmployeeId"
.CommandType = CommandType.Text
.Connection = cn
With .Parameters
.Add("@EmployeeId", SqlDbType.Int, 0, "EmployeeId")
End With
End With
adapter.Fill(table)
'Modify a row
table.Rows(0).Item("FirstN ame") = "VBRocks"
table.Rows(0).Item("LastNa me") = ""
'Add a row
Dim row As DataRow = table.NewRow()
row.Item("FirstName") = "ranhell"
row.Item("LastName") = ""
table.Rows.Add(row)
'Delete a row
table.Rows(5).Delete()
'Update the database with the changes:
adapter.Update(table)
'Release resources used by adapter
adapter.Dispose()
End Sub
You can think of your DataTable as an in-memory representation of the data that is in the database. You can insert new records into it, modify existing records, and delete existing records. But, because the data is only in memory, it doesn't actually make any changes to the database.
You can use a DataAdapter to work with the data in a database and the data in a dataset. The DataAdapter is kind of like a "go-between" between the database and dataset. You can use the DataAdapter to get the data from the database and fill the dataset with it (SELECT command), and you can use it to UPDATE your database with the changes made to the dataset, DELETE records and INSERT new records as well.
Here's a little example:
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
SelectRecords()
End Sub
Private Sub SelectRecords()
'Select the records from the database
Dim SQL As String = "SELECT * FROM Employees"
Dim cnString As String = "Data Source=localhost;Initial Catalog=Northwind;Integrat
Dim table As New DataTable()
Using adapter As New SqlClient.SqlDataAdapter(S
adapter.Fill(table)
End Using
Me.DataGridView1.DataSourc
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
ModifyRecords()
SelectRecords()
End Sub
Private Sub ModifyRecords()
'Modify the records in the database
Dim cnString As String = "Data Source=localhost;Initial Catalog=Northwind;Integrat
Dim cn As New SqlClient.SqlConnection(cn
Dim table As New DataTable()
Dim adapter As New SqlClient.SqlDataAdapter()
adapter.SelectCommand = New SqlClient.SqlCommand()
With adapter.SelectCommand
.CommandText = "SELECT * FROM Employees"
.CommandType = CommandType.Text
.Connection = cn
End With
adapter.UpdateCommand = New SqlClient.SqlCommand()
With adapter.UpdateCommand
.CommandText = "UPDATE Employees SET FirstName=@FirstName, LastName=@LastName " & _
"WHERE EmployeeId=@EmployeeId"
.CommandType = CommandType.Text
.Connection = cn
With .Parameters
.Add("@EmployeeId", SqlDbType.Int, 0, "EmployeeId")
.Add("@FirstName", SqlDbType.VarChar, 0, "FirstName")
.Add("@LastName", SqlDbType.VarChar, 0, "LastName")
End With
End With
adapter.InsertCommand = New SqlClient.SqlCommand()
With adapter.InsertCommand
.CommandText = "INSERT INTO Employees (FirstName, LastName) " & _
"VALUES (@FirstName, @LastName)"
.CommandType = CommandType.Text
.Connection = cn
With .Parameters
.Add("@FirstName", SqlDbType.VarChar, 0, "FirstName")
.Add("@LastName", SqlDbType.VarChar, 0, "LastName")
End With
End With
adapter.DeleteCommand = New SqlClient.SqlCommand()
With adapter.DeleteCommand
.CommandText = "DELETE FROM Employees WHERE EmployeeId = @EmployeeId"
.CommandType = CommandType.Text
.Connection = cn
With .Parameters
.Add("@EmployeeId", SqlDbType.Int, 0, "EmployeeId")
End With
End With
adapter.Fill(table)
'Modify a row
table.Rows(0).Item("FirstN
table.Rows(0).Item("LastNa
'Add a row
Dim row As DataRow = table.NewRow()
row.Item("FirstName") = "ranhell"
row.Item("LastName") = ""
table.Rows.Add(row)
'Delete a row
table.Rows(5).Delete()
'Update the database with the changes:
adapter.Update(table)
'Release resources used by adapter
adapter.Dispose()
End Sub
Dim value as string = cmd.ExecuteScalar()