I'm slowly transitioning to VB.Net and it seems like the simplest of things are completely different. I'm trying to do a connection to SQL Server and just grab some data. I've tested the connection and it's fine. Now I'm trying to simply select some data from the server and display the first name in the Msgbox (or anything for that matter). Here is the code I've gotten so far....
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Public conn As New SqlConnection("data source=Tuys1;initial catalog=DDDBuyer; user ID=userID; password=password")
Private dA As New SqlDataAdapter("Select FirstName, LastName from tblBuyers where Buyerid=1473", conn)
Private dS As New DataSet
End Class
I'm not really sure how to go from here... I understand there is something like
.hasRows
if ds.hasrows then
msgbox = "HELLO"
End if
or something like displaying the FirstName in txtFirstName
I am trying to get an idea of how ths works and after some research it's hard to find a specific example that would help me do what I'm trying to accomplish.
Dim custAdapter As SqlDataAdapter = New SqlDataAdapter( _
"SELECT * FROM dbo.Customers", customerConnection)
Dim ordAdapter As OleDbDataAdapter = New OleDbDataAdapter( _
"SELECT * FROM Orders", orderConnection)
Dim customerOrders As DataSet = New DataSet()
custAdapter.Fill(customerOrders, "Customers")
ordAdapter.Fill(customerOrders, "Orders")
Dim relation As DataRelation = _
customerOrders.Relations.Add("CustOrders", _
customerOrders.Tables("Customers").Columns("CustomerID"), _
customerOrders.Tables("Orders").Columns("CustomerID"))
Dim pRow, cRow As DataRow
For Each pRow In customerOrders.Tables("Customers").Rows
Console.WriteLine(pRow("CustomerID").ToString())
For Each cRow In pRow.GetChildRows(relation)
Console.WriteLine(vbTab & cRow("OrderID").ToString())
Next
Next
As explained here
Related
My app is using datagridview to display data from sqlserver.
I am working on a button code that will enable the user to find and replace the entire column. Eg find Class1 and replace with Class2.
something like UPDATE students SET class = combo1.text with combo2,text WHERE%
I am feeling too far from reality. How can this code work?
You want to set your class to the value of Combo2, where the value already matches the value of Combo1.
"UPDATE students
SET class = "+combo2.text+"
where class = "+combo1.text+" "
Copy paste this code (taken from msdn)
Public Sub ExecuteQuery(ByVal queryString As String, _
ByVal connectionString As String)
Using connection As New SqlConnection(connectionString)
Dim command As New SqlCommand(queryString, connection)
command.Connection.Open()
command.ExecuteNonQuery()
End Using
End Sub
.......
and now when you need to execute you query write this
Dim cn as String
Dim UpdateQry as String
cn = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\eDatabase.mdf;Integrated Security=True;User Instance=True"
UpdateQry = "Update students SET class = '" + combo2.text + "' WHERE class = '" + combo1.text + "'"
ExecuteQuery(UpdateQry, cn)
hope this helps you.
regards
Hye... I currently doing my final year project using vb.net and i got this error. Im tryin' to fix the error but still not succeed. I use ms access for database in my project. I try to put con.Open() before the 'dt' statement and con.Close() after the 'cboProduct.Select' but the result is the same. Really appreciate your helps. Thanks :)
'GLOBAL DECLARATIONS
Dim conString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Acer User\Documents\MAKLUMAT IVENTORI.accdb"
Dim con As OleDbConnection = New OleDbConnection(conString)
Dim adapter As New OleDbDataAdapter
Dim cmd As New OleDbCommand
Dim dt As New DataTable
Dim ds As New DataSet
Private Sub RefreshData()
dt = New DataTable
ds = New DataSet
ds.Tables.Add(dt)
adapter = New OleDbDataAdapter("Select * FROM product WHERE lab_kod='66'", con)
adapter.Fill(dt)
DataGridView1.DataSource = dt.DefaultView
labelID.Text = getAutoID()
lblLabCode.Text = "66"
cboProduct.Select()
Dim v_SQL As String = "SELECT * FROM kategori_product"
cmd = New OleDbCommand(v_SQL, con)
con.Open()
Dim v_dataReader As OleDbDataReader = cmd.ExecuteReader()
Dim v_dataTable As New DataTable
v_dataTable.Columns.Add("product_kod", Type.GetType("System.String"))
If v_dataReader.HasRows Then
While v_dataReader.Read
v_dataTable.Rows.Add(v_dataReader.GetString(0))
End While
cboProduct.DataSource = v_dataTable
End If
cboProduct.DisplayMember = "product_kod"
cboProduct.ValueMember = "product_kod"
v_dataReader.Close()
con.Close()
End Sub
Don't store short-lived objects, such as database connections, as global variables.
In fact, don't use global variables, ever, at all.
(though I note that assuming this is a Class and not a Module those are actually class fields, not globals, but you're still misusing them)
Your OleDbDataAdapter.Fill call requires the connection to be open, but you call .Fill(dt) before calling con.Open()
Use Using (using() in C#) blocks to ensure your database connection is always closed, even in the event of failure.
I am very new to VB 2015. I want to learn about database update command. I try to understand SqlDataAdapter. Could anyone please advise me? As my code below, it run completely with no error, but my database table (WORKSHEET) was not updated.
Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim command2 As String
command2 = "Update WORKSHEET set cancel_flag = 'Y' WHERE CNumber LIKE #reversalNumber"
Using con2 As New SqlConnection(WindowsApplication1.My.Settings.SaleCommDatabaseConnectionString)
Using cmd2 As New SqlCommand(command2)
Using oda2 As New SqlDataAdapter
cmd2.Connection = con2
con2.Open()
cmd2.Parameters.Add("#reversalNumber", SqlDbType.VarChar, 10, "15332")
oda2.UpdateCommand = New SqlCommand(command2, con2)
End Using
End Using
End Using
MsgBox("ggggg")
End Sub
End Class
The SqlDataAdapter class is useful for applying changes from a DataSet to the database. Try filling a DataSet with the modified data, and then apply changes using the adapter's update method (oda2.Update(WORKSHEET)).
EDIT: Make sure that you fill the DataSet with the data by using the SqlDataAdapter's Fill method.
oda2.Fill(yourDataSet)
Before this, you need to select the proper command with oda2.SelectCommand = YourCommand.
I have been trying to figure this out for a few days but every time I try and fix it I encounter the same error. I am trying to implement the data from my database into a candlestick style chart.Every time I run it i'm faced with this same error:
"An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: IErrorInfo.GetDescription failed with E_FAIL(0x80004005)."
I have used a combination of the limited resources given to me and code that I found here on a previously answered question to create the following code.
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim myConnection As OleDbConnection = New OleDbConnection
Dim provider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
Dim dataFile As String = "C:\Users\Ian\Documents\A2 Woking\Computing\Database1\Database_data_source .accdb"
Dim DatabasePass As String = "DatabasePW"
Dim connString As String = provider & dataFile
Dim execute As OleDbDataReader
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
myConnection.ConnectionString = connString
Dim objDataAdapter As OleDbCommand = New OleDbCommand("Select Time, Open, High, Low, Last FROM EUR_USD_TB", myConnection)
myConnection.Open()
execute = objDataAdapter.ExecuteReader
While execute.Read
chartTemplate.Series("dataSeries").Points.AddXY(execute("Time"), execute("Low"), execute("High"), execute("Open"), execute("Last"))
End While
' Close the reader and the connection
execute.Close()
myConnection.Close()
End Sub
End Class
Any help would be appreciated i'm sure its something annoyingly simple I don't get.
The path is correct---
The database is not protected by a password, that variable is for later---
The query works in access---
This same code works perfectly for accessing a separate table in my login
I'm trying to open an image from my database to a picture box but I just don't how to do it.
I've searched for some answers and I am not familiar with the codes for I am beginner only.
The only codes that I researched about are for connecting the database to the system:
Imports System.Data.OleDb
Module Module1
Public acsconn As New OleDbConnection
Public acsdr As OleDbDataReader
Public acsda As New OleDbDataAdapter
Public acscmd As New OleDbCommand
Public strsql As String
Public acsds As New DataSet
Public Sub connect()
Try
acsconn.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=|datadirectory|\database1.mdb;"
acsconn.Open()
If acsconn.State = ConnectionState.Open Then
MsgBox("Connected")
Else
MsgBox("Error")
End If
Catch ex As Exception
End Try
End Sub
End Module
I do not know what is next. BTW, those codes - I used it for saving the image to the database.
I think this is the sort of thing you are looking for:
Private Sub HandleRequest(context as HttpContext)
Dim SqlCnn As SqlConnection = Nothing, sql As String
Dim emp_id As Integer
emp_id = Int32.Parse(context.Request.QueryString("id"))
ConnectDB(SqlCnn)
Try
sql = "SELECT image FROM employees (NOLOCK) WHERE ID =" & emp_id
sqlcmd = New SqlCommand(sqlstr, SqlCnn)
Dim imageData As Byte() = DirectCast(sqlcmd.ExecuteScalar(), Byte())
context.Response.ContentType = "image/jpeg"
context.Response.BinaryWrite(imageData)
Catch ex As Exception
ReportError(ex)
Finally
CloseDB(SqlCnn)
End Try
End Sub