VB.NET 2010 to connect to ms access database - vb.net

Can someone show me how to connect vb.net 2010 to ms access database to get the data and display it in vb.net application that I am doing right now. My project is that I am doing dictionary application with vb.net so every time i put new word in search box, I want the vb.net to get the definition from ms access and display it in the application.
And code snippet would be great or tutorial

'Grabs data from a table and posts it into a ListView
Dim Table_ As String = "Table1"
Dim query As String = "SELECT * FROM " & Table_
Dim MDBConnString_ As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=TestDatabase.mdb;"
Dim ds As New DataSet
Dim cnn As OleDbConnection = New OleDbConnection(MDBConnString_)
cnn.Open()
Dim cmd As New OleDbCommand(query, cnn)
Dim da As New OleDbDataAdapter(cmd)
da.Fill(ds, Table_)
cnn.Close()
Dim t1 As DataTable = ds.Tables(Table_)
Dim row As DataRow
Dim Item(2) As String
For Each row In t1.Rows
Item(0) = row(0)
Item(1) = row(1)
Dim NextListItem As New ListViewItem(Item)
ListView1.Items.Add(NextListItem)
Next

Related

Add only new items from listbox to SQL

i have a listbox that users can add Countries Names into it anytime.
now i need to only add new data to check if the data is new then insert it to sql.
my code not work:
For Each i As String In listbox1.Items
Dim sql = "select * From Countries where CountryName=N'" & i & "'"
Dim adp As New SqlClient.SqlDataAdapter(sql, SQlconn)
Dim ds As New DataSet
adp.Fill(ds)
Dim dt = ds.Tables(0)
If dt.Rows.Count = 0 Then
Dim dr = dt.NewRow
dr!CountryName = i
dt.Rows.Add(dr)
Dim cmd As New SqlClient.SqlCommandBuilder(adp)
adp.Update(dt)
End If
Next
If you're loading the existing names from the database in the first place then there's no need for you to check anything. Simply query the database to populate a DataTable and bind that to the ListBox. When the user adds a new country, add it to the DataTable and it will automatically show up in the ListBox. When it's time to save, just use the same data adapter to save the DataTable and only the new records will be saved.

Search by datagridview1 column1 values

i using this code to search by textbox to get the related data for the ID
Dim connetionString As String
Dim cnn As SqlConnection
connetionString="DataSource=IP;InitialCatalog=DB;UserID=sa;Password=password"
cnn = New SqlConnection(connetionString)
Dim com As String = "SELECT * FROM Table WHERE ID= '" & TextBox1.Text & "'"
Dim Adpt As New SqlDataAdapter(com, cnn)
Dim ds As New DataSet()
Adpt.Fill(ds, "Table")
DataGridView1.DataSource = ds.Tables(0)
What iam trying to do, i need to do the same code but with datagridview column1
Explaining:
i've already add button to upload data to datagridview the data include ID's
i need to search by those ID's in bulk, that included to the datagridview then post the income data to another datagridview,
Thanks in advance.
First, I think you should correct your existing code to use SQL Parameters.
Dim com As New SqlCommand("SELECT * FROM Table WHERE ID= #Id;", cnn)
com.Parameters.Add("#Id", SqlDbType.VarChar).Value = TextBox1.Text
Dim Adpt As New SqlDataAdapter(com)
Now to use the value in the first row, first column
Dim strId As String = DataGridView1.Rows(0).Cells(0).Value.ToString
Dim com2 As New SqlCommand("SELECT * FROM Table WHERE ID= #Id;", cnn)
com2.Parameters.Add("#Id", SqlDbType.VarChar).Value = strId
Dim Adpt2 As New SqlDataAdapter(com2)
I had to guess at the datatype. Please check your database to get the correct type. I guessed it was some type of string since you had it enclosed in single quotes.

vb.net Loading Images from Access Database to DataTable?

So I have a MS Access Database with 1 table (Records) and 2 fields in it ("RecordID" (Number), which is the primary key, and "LowRes" (OLE Object) which is a low Resolution image). There are about 100 records.
I/m trying to load the Access Table into a DataTable (ID_Table) in VB.net.
Code so far:
Dim cnString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=SBS2257_ID.accdb;"
Dim theQuery As String = "SELECT [RecordID], [LowRes] FROM [Records];"
Using CN As New OleDbConnection(cnString)
Dim command As New OleDbCommand(theQuery, CN)
Using objDataAdapter = New OleDbDataAdapter(command)
Dim ID_Table As New DataTable
CN.Open()
Dim pictureData As Byte() = DirectCast(command.ExecuteScalar(), Byte())
Dim picture As Image = Nothing
Using stream As New IO.MemoryStream(pictureData)
picture = Image.FromStream(stream)
objDataAdapter.Fill(ID_Table)
End Using
End Using
End Using
However the "DirectCast" command fails when I tell it to look at more then 1 field in my SQL statement with a datatype mismatch (if I just do [LowRes] it does not throw a error). However, I get stuck again when trying to apply the result to the table via the objDataAdapter, it doesnt fill the table with anything? I also notice that "picture" only contains the first image in the database.
I could put this database query in a function using "WHERE RECORDID=..." and loop it manually building the table returning "picture" each time, but Id like to avoid running a function 100 times, esp one that access a database.
Is it possible to read the whole database that contains images and just load it directly into a Datatable in one big swoop?
EDIT: So I got this to work:
Dim strConnection As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=SBS2257_ID.accdb;"
Dim strSQL As String = "SELECT [RecordID], [LowRes] FROM [Records];"
Using objConnection = New OleDbConnection(strConnection)
Using objCommand = New OleDbCommand(strSQL, objConnection)
Using objDataAdapter = New OleDbDataAdapter(objCommand)
Dim objDataTable As New DataTable("IDs")
objDataAdapter.Fill(objDataTable)
Return objDataTable
End Using
End Using
End Using
how ever when I go to view row 0, col 1 which should be the first LowRes image via a .ToString Useing this code:
Private Sub PrintValues(ByVal table As DataTable)
For Each row As DataRow In table.Rows
For Each col As DataColumn In table.Columns
MsgBox(row(col).ToString())
Next col
Next row
End Sub
It just displays "System.Byte[]". It knows its a Byte datatype, but how do I display that in a picturebox?
The ExecuteScalar() executes the query, and returns the first column of the first row in the result set returned by the query.
as your query is
Dim theQuery As String = "SELECT [RecordID], [LowRes] FROM [Records];"
the first column is RecordID which is not a Byte().
you can change your query as following:
Dim theQuery As String = "SELECT [LowRes] FROM [Records];"
or you have to use other methods to get data from the database
Dim strSql As String = "SELECT [RecordID], [LowRes] FROM [Records]"
Dim dtb As New DataTable
Using cnn As New OleDbConnection(connectionString)
cnn.Open()
Using dad As New OleDbDataAdapter(strSql, cnn)
dad.Fill(dtb)
End Using
cnn.Close()
End Using

Several rows in SQL Developer but VB says no rows

Dim Test123 As String
Dim Conn As OleDb.OleDbConnection = New OleDbConnection("Private")
Conn.Open()
Dim mySelectQuery As String = "SELECT mti_part_no,sum(mpcs.shop_inv_history.quantity) As FebQTY from mpcs.shop_inventory, mpcs.shop_inv_history where mpcs.shop_inv_history.date_time Like '%Feb% %2015%' AND comments = 'CHECK ITEM OUT' AND mpcs.shop_inventory.si_key=mpcs.shop_inv_history.si_key AND SHOP_INVENTORY.CATEGORY BETWEEN 900 and 999 AND SHOP_INVENTORY.SCRAP_FLAG <> 1 group by mti_part_no order by FebQTY DESC"
Dim CMD As OleDbCommand = New OleDbCommand(mySelectQuery, Conn)
Dim Myreader As OleDbDataReader
Myreader = CMD.ExecuteReader()
Myreader.Read()
Test123 = Myreader("FebQTY")
There are 180 rows when I copy/paste my query into SQL Developer, however whenever I try to use this in VB.NET to populate my listview, I was getting nothing. So I tossed it into the above code just to see what I was quickly getting back from FebQTY and the error is that there are no rows, same with mti_part_no. Can't figure out what the problem is.

How to redirect one selected item on data gridview to another data gridview in vb.net?

I'm using vb.net. I have successfully query out this in data gridview by using sql
After I click the "Performance", I want to query out the details of "LM3409QHVMYUY8" and the details is inside the oracle and display into another data grid view
How I'm going to that ?
Thanks
I'm used this code to get the above datagridview:
Dim connectionString As String = System.Configuration.ConfigurationManager.AppSettings("connCommon")
Dim sqlConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT * FROM ODS_Nspn_Pkg_Group_v WHERE ([SAG-Grp] = '" & ddlPackage & "')"
Dim sqlCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(queryString, sqlConnection)
sqlCommand.CommandTimeout = 0
Dim dataAdapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(sqlCommand)
Dim dataSet As DataSet = New DataSet
dataAdapter.Fill(dataSet)
Return dataSet