querying a MySQL table from VB.NET - mysql

I made my software in vb.net and connected it with MySQL databse and using phpmyadmin I have create a table update and a column version.
In the version column I have inserted the link of version.txt
I want that my update library which is updateVB will get the link of version.txt from the database from that table....
Updatevb1.checkforupdate("Text file where your version is stated (URL)",
"Current Version",
"URL of executable updater (SFX Archive),
"Username for FTP", "Password for FTP",
showUI As Boolean)
I want to get every of these information like: Text file version URL, current version, URL of executable update etc.
How can this be done?
conn = New MySqlConnection(ServerString)
Try
conn.Open()
Dim sqlquery As String = "SELECT FROM updater"
Dim data As MySqlDataReader
Dim adapter As New MySqlDataAdapter
Dim command As New MySqlCommand
command.CommandText = sqlquery
command.Connection = conn
adapter.SelectCommand = command
data = command.ExecuteReader
While data.Read()
If data.HasRows() Then
Dim vlink As String = data(1).ToString
Dim dlink As String = data(2).ToString
Dim ftpu As String = data(3).ToString
Dim ftpp As String = data(4).ToString
End If
End While
UpdateVB1.checkforupdate("vlink", "0.0.9", "dlink", "ftpu", "ftpp", showUI:=True)
data.Close()
conn.Close()

The first problem is that your SQL statement isn't specifying any columns.
Supply the list of columns that you want to consume.
Dim sqlquery As String = "SELECT vlink, dlink, ftpu, dtpp FROM updater"

Related

How properly insert data from dynamic DataTable to MySQL?

Actually i have a method where the user is able to choose data from excel file and choose between some headers name which are set statically and are equals to MySQL columns name in a table, then i just collect that data in a DataTable.
Now i would be able to store that data in MySQL database but which would be the best method to insert that data from a DataTable into MySQL? The rows of DataTable could contain more than 50.000/100.000 items.
That's how the DataTable could look's like (The header is choosen dynamically by the user from a combobox)
So in this Insert method which would be the best way to add that data? Should i just loop throw dataTable items and make multiple inserts?
Private Sub InsertDB()
Dim dataTable = ExcelToDT(TxtUpload.Text, ColumnHeader(PanelColumns, Column))
Dim dbCon As New Odbc.OdbcConnection
Dim conStr As String = "DSN=WRRPT"
dbCon.ConnectionString = conStr
dbCon.Open()
Dim cmd As New Odbc.OdbcCommand
With cmd
.CommandText = "INSERT INTO ? VALUES ?"
.Connection = dbCon
End With
Dim reader As Odbc.OdbcDataReader
reader = cmd.ExecuteReader
dbCon.Close()
reader = Nothing
cmd = Nothing
End Sub

How to insert data from XML in MySql?

Actually i'm trying to create a program in VB.NET that get xml files from a folder and set it's data to MySQL.
Each XML file has unique ID for which is created a database and all tables.
In the folder i can have files like "DC_001" "DF_001" "DC_002" and that's mean that the data from the XML file DC_001 have to be placed in database 001 in table DC.
Actually i've made the method which create the database for each unique ID and it's tables but as i'm not very in to VB.NET i need some suggestion on how to put the data inside the XML to each database.
Here is my method where i create the database and tables.
Dim PIVA As New ArrayList
Dim conn As MySqlConnection = New MySqlConnection
conn.ConnectionString = "server=127.0.0.1;user id=root; password=block; database=mysql"
For Each fileName As String In Directory.EnumerateFileSystemEntries("C:\Users\imytyuk\Desktop\test")
Dim file As String = Trim(EstrCampo("_", fileName, 1, 2))
If Not PIVA.Contains(file) Then
PIVA.Add(file)
End If
Next
For Each iva In PIVA
Dim cmd As MySqlCommand = New MySqlCommand("CREATE DATABASE `" & iva & "`", conn)
Try
conn.Open()
Try
cmd.ExecuteNonQuery()
Catch ex As Exception
End Try
conn.Close()
CreaTB(iva)
Catch
End Try
Next
While here is like a XML file can be structured
UPDATE:
I'm trying to use the following method to insert the data from XML but i get ""Column count doesn't match value count at row 1"}" as Exception.
Here is the code:
Sub AddXML()
For Each fileName As String In Directory.EnumerateFileSystemEntries("C:\Users\imytyuk\Desktop\test")
databaseFilePut(fileName, "datacollect", Trim(EstrCampo("_", fileName, 1, 2)))
File.Delete(fileName)
Next
End Sub
Public Shared Sub databaseFilePut(ByVal varFilePath As String, ByVal table As String, ByVal db As String)
Dim file() As Byte
Dim stream = New FileStream(varFilePath, FileMode.Open, FileAccess.Read)
Dim reader = New BinaryReader(stream)
file = reader.ReadBytes(CType(stream.Length, Integer))
Dim conn As MySqlConnection = New MySqlConnection
conn.ConnectionString = "server=127.0.0.1;user id=root; password=block; database=" & db
Dim cmd As MySqlCommand = New MySqlCommand("INSERT INTO " & table & " Values(#File)", conn)
Try
conn.Open()
cmd.Parameters.Add("#File", MySqlDbType.VarBinary, file.Length).Value = file
cmd.ExecuteNonQuery()
Catch ex As Exception
End Try
End Sub

Error when getting MySQL data in .NET

I'm creating a .NET web application that retrieves data from a database. I keep getting the following error when running the below code though.
Fatal error encountered during command execution.
The InnerException is {"Parameter '?Username' must be defined."}, but I can see that ?Username is defined and I know that Session("Username") is correct and exists.
Here is the code. It fails on the SupervisorNameAdapter.Fill(SupervisorNameData, "Data") line.
Dim SupervisorID As String = Session("Username")
Dim qryGetSupervisorName As String = "select * from USERS where UserID = ?Username"
Using cn As New MySqlConnection(ConfigurationManager.ConnectionStrings("ConnectionInfo").ConnectionString), cmd As New MySqlCommand(qryGetSupervisorName, cn)
cmd.Parameters.AddWithValue("?Username", SupervisorID)
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
Dim SupervisorNameAdapter As New MySqlDataAdapter(qryGetSupervisorName, cn)
Dim SupervisorNameData As New DataSet
SupervisorNameAdapter.Fill(SupervisorNameData, "Data")
If SupervisorNameData.Tables(0).Rows.Count = 0 Then
MsgBox("An error has occured. Please refresh the page.", vbOKOnly, "Error!")
Else
SupervisorName.Text = SupervisorNameData.Tables(0).Rows(0).Item(1) & " " & SupervisorNameData.Tables(0).Rows(0).Item(2)
End If
End Using
Does anyone know why this is happening?
You are not setting correctly the command to be used by the MySqlAdapter. You just pass the command text string and with that string the adapter build another command that is missing the required parameter
Just change your code to
Dim SupervisorID As String = Session("Username")
Dim qryGetSupervisorName As String = "select * from USERS where UserID = ?Username"
Using cn As New MySqlConnection(ConfigurationManager.ConnectionStrings("ConnectionInfo").ConnectionString), cmd As New MySqlCommand(qryGetSupervisorName, cn)
cmd.Parameters.AddWithValue("?Username", SupervisorID)
Dim SupervisorNameAdapter As New MySqlDataAdapter(cmd)
Dim SupervisorNameData As New DataSet
....
End Using

How to print mysql column names to a file

How to print out MySQL table field names and types or
how to save the MySQL table field names to a text file?
is it possible. (front end VB.NET 2010)
Eg:- SHOW COLUMNS FROM tablename;
how can I get the above command result to a printer or a file?
Thanks in advance
Try below code
Dim cn As New OleDbConnection(connectionString)
Dim cmd As New OleDbCommand
Dim rdr As OleDbDataReader
Dim tbl As DataTable
cmd.CommandText = "Select * From " & TableName
cn.Open()
cmd.Connection = cn
rdr = cmd.ExecuteReader(CommandBehavior.SchemaOnly Or
CommandBehavior.KeyInfo)
tbl = rdr.GetSchemaTable // Read tbl and Write it to text file
rdr.Close()
Hope this will help.

Need help filling datagrid from MySQL

I have made a table in mysql with attributes Product code,Quantity,company,price. And I have created a datagridview in vb 2012 and I want to take input from the form and then display the results in a datagridview. I also want to display price from the table I have created in mysql. But, i'm not able to do so.
Here is the code of my program. plz help me
Dim row As Integer = DataGridView1.Rows.Add()
Dim connection As String
Dim command As String
Dim command2 As String
command2 = "select Company from Stock WHERE Product_Code =('" + TextBox1.Text + "');"
connection = "Data Source=localhost; Database=Entry; User Id=root; Password=;"
command = "select Price from Stock WHERE Product_Code =('" + TextBox1.Text + "');"
Dim con As New MySqlConnection(connection)
Dim cmd As New MySqlCommand(command)
Dim data As DataTable
Dim adp As New MySqlDataAdapter
Dim data2 As DataTable
Dim adp2 As New MySqlDataAdapter
DataGridView1.Rows.Item(row).Cells(0).Value = TextBox1.Text
DataGridView1.Rows.Item(row).Cells(2).Value = TextBox2.Text
Try
adp = New MySqlDataAdapter(command, connection)
adp2 = New MySqlDataAdapter(command2, connection)
data = New DataTable
data2 = New DataTable
adp.Fill(data)
adp2.Fill(data2)
DataGridView1.Rows.Item(row).Cells(1).Value = data
DataGridView1.Rows.Item(row).Cells(3).Value = data
Catch ex As Exception
MessageBox.Show("Error")
End Try
You should be able to find examples of how to do this all over SO (stack overflow). But to give you a helping hand, here are the things you need to research:
First, you should parameterize your SQL to prevent injection and readability: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx
Second, you dont add rows to a datagrid, you set the datasource to something that implements IList: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource.aspx Then you add items to your list if you need to. If you just want to display the rows from your table, you can set the datasource to your datatable (DATA).