If I use the following code to write and read rtf data into an access .mdf file its fast to save the data but very slow to read it back.
I tested with a rtf file which is 1mg in size contains around 10 lines in bold and 4 small images.
Is there anyway to read it back faster,
Note if load the rtf from file it takes around 1 sec,
SAVE DATA
Dim constring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\paul\Documents\Code.mdb"
Using myconnection As New OleDbConnection(constring)
myconnection.Open()
Dim sqlQry As String = "INSERT INTO [table1] ([Code]) VALUES (#Code)"
Using cmd As New OleDbCommand(sqlQry, myconnection)
cmd.Parameters.AddWithValue("#code", RTB1.Rtf)
cmd.ExecuteNonQuery()
End Using
myconnection.Close()
End Using
READ DATA
Dim constring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\paul\Documents\Code.mdb"
Dim ds As New DataSet
Using myconnection As New OleDbConnection(constring)
myconnection.Open()
Dim strSql As String = "SELECT * FROM table1"
Using cmd As New OleDbCommand(strSql, myconnection)
Dim da As New OleDbDataAdapter(cmd)
da.Fill(ds, "Table")
End Using
myconnection.Close()
End Using
Me.RTB1.rtf = ds.Tables(0).Rows(0).Item(1)
Related
I am using this function to upload a excel file in a datatable or datagridview, my problem is that at the beginning the program was executed instantaneously but now wastes a couple of seconds running the command xlsConn.Close (), any idea why now is slower if at the beginning it ran instantly? Also if I eliminate that command (xlsConn.Close ()), I will have any problems? I've seen that I get some problems trying to open the uploaded excel file, it only gives me reading access.
Function ReadExcelFile(ByVal InputFile As String, ByVal DataSheet As String)
InputFile = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & InputFile & ";Extended Properties=Excel 12.0"
DataSheet = "select * from [" & DataSheet & "$]"
Dim da As New OleDbDataAdapter
Dim dt As New DataTable
Dim cmd As New OleDbCommand
Dim xlsConn As OleDbConnection
xlsConn = New OleDbConnection(InputFile)
Try
xlsConn.Open()
cmd.Connection = xlsConn
cmd.CommandType = CommandType.Text
cmd.CommandText = (DataSheet)
da.SelectCommand = cmd
da.Fill(dt)
Catch
MsgBox(ErrorToString)
Finally
xlsConn.Close()
xlsConn = Nothing
End Try
Return dt
End Function
I have a question, I want to add a TabPage to my TabControl from an access record? I think it's something along these lines but it didn't work:
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & My.Application.Info.DirectoryPath.ToString() & "\data\testing.Accdb;Persist Security Info=False;")
con.Open()
Dim constr As String = "SELECT ProductType, Discription FROM TblProductType"
Dim cmd As OleDbCommand = New OleDbCommand(constr, con)
Dim da As OleDbDataAdapter = New OleDbDataAdapter(cmd)
Dim ds As DataSet = New DataSet()
da.Fill(ds, "TblProductType")
Me.TabControl1.TabPages.Add("Discription")
con.Close()
You have to iterate over the row collection of the table:
Using cmd As New OleDbCommand("SELECT ProductType, Discription FROM TblProductType", con)
Using rdr As OleDbDataReader = cmd.ExecuteReader
While rdr.Read
TabControl1.TabPages.Add(rdr("Discription").ToString)
End While
End Using
End Using
Of course, this only gives you an empty TabPage for every record and in it's current implementation, you don't have any reference to the record anymore. That ProductType, if it's unique, should be used somewhere, too.
i have a program needs to update data in dbf file. but it keeps appear error 'operator/operand type mismatch'. here is sample code :
Dim con As OleDbConnection = New OleDbConnection("Provider=vfpoledb;Data Source=C:\folder\paytran.dbf;Collating Sequence=machine;")
Try
Dim strSQL As String = "UPDATE paytran.dbf SET workhr = 20 WHERE empno = 102"
Dim cmd As OleDbCommand = New OleDbCommand(strSQL, con)
con.Open()
Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet()
' Using DataAdapter object fill data from database into DataSet object
myDA.Fill(myDataSet, "MyTable")
' Binding DataSet to DataGridView
DGV.DataSource = myDataSet.Tables("MyTable").DefaultView
con.Close()
con = Nothing
Catch ex As Exception
MessageBox.Show(ex.Message, "Error Select Data")
Finally
If con IsNot Nothing Then
con.Close()
End If
End Try
please help me..
Its your connection string. The connection string should only have to point to the PATH where the data files are located, then all the SQL based commands will by default be able to see any .DBF tables IN that path (or forward if subpaths exists).
Data Source=C:\folder
instead of
Data Source=C:\folder\paytran.dbf
So, now if you have 30 tables in the "C:\folder", you can now query from ALL of them as needed.
You need to explicitly open and close the DBF. Try:
Dim strSQL As String = "Use paytran in 0 shared;UPDATE paytran SET workhr = 20 WHERE empno = 102;use in select('paytran')"
I have following code that reads data using OleDbDataReader.
The funny thing is that I can only return numbers although I converted to string.
Dim cn As New OleDbConnection
Dim fileloc = Server.MapPath("~/test/")
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileloc + ";Extended Properties='text;HDR=Yes;FMT=Delimited'"
cn.Open()
Dim cmd As New OleDbCommand
cmd.Connection = cn
cmd.CommandText = "SELECT * FROM feed.csv"
Dim reader As OleDbDataReader = cmd.ExecuteReader()
While reader.Read()
Response.Write(reader("Stock Number").ToString)
End While
reader.Close()
cn.Close()
So I opened the csv file with test and looked the data.
When ever "Stock Number" has string, it doesn't not return data.
IM-95-189-012 ----> returns blank
2241 -----> returns 2241
2241B -----> returns blank
This is the first time I work with OleDbDataReader.
Any idea what's going on ?
If you are always trying to get a string value, then use GetString. If you need more info, check the knowledge base
Response.Write(reader.GetString("Stock Number"))
This has a direct impact on this. Answer is for C#, but should be convertible to VB.
I'm currently working on an ms access database in vb.net.
And I need a code that can determine which data I am updating in the database. Because when I try to update the data, the previous data is being cloned and it will generate two data(the updated and the previous data), and the program will also generate a random id number for the updated data, which is not good.
Here is my code for the update button:
'update
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("GH").Rows(INC).Item(1) = TextBox13.Text
ds.Tables("GH").Rows(INC).Item(2) = TextBox14.Text
ds.Tables("GH").Rows(INC).Item(3) = TextBox15.Text
da.Update(ds, "GH")
MsgBox("Data updated")
My code for form load:
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\ACCESS DATABASE\search.mdb"
con.Open()
sql = "SELECT * FROM GH"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "GH")
con.Close()
I'm using system.data.oledb namespace
Here are my declarations:
Dim cmd As OleDbCommand
Dim cn As OleDbConnection
Dim dr As OleDbDataReader
Dim ds As New DataSet
Dim con As New OleDb.OleDbConnection
Dim da As OleDb.OleDbDataAdapter
Your question is not clear to me at all, but if you want to find the last ID of an insert, you would usually check the results of "SELECT ##IDENTITY" immediately after the record is inserted.