Get first entry from MySQL database in VB - mysql

Could you please help me? I just want to take the first row of a table in a mysql database via Visual Basic. I prefer simple code. The only thing i found is using a while, thing that i dont want. Thanks in advance!

Try something like this
Try
Dim sql As String = "SELECT first_row FROM test_table WHERE id = 1"
Dim cmd as OleDbCommand
cmd = New OleDbCommand(sql, Reconnect)
Dim itmReader as SqlDataReader
itmReader = cmd.ExecuteReader
If itmReader.Read Then
Dim firstRowData as String
firstRowData = itmReader.Item("first_row")
MessageBox.Show(firstRowData)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try

Related

TableDataAdapter.update() and TableDataAdapter.Fill() it take long time

have table with 200 columns, and when try to fill or update apporx. (15000) rows using the following:-
TableDataAdapter.update(DataTable)
TableDataAdapter.Fill(Datatable)
it take around 8 mins and some cases stacked and not complete the process
then tried to use the below code for select command to fill
Dim My_Trans as SQLTransaction
Dim My_Table as new DataTable
Dim My_SQLDataAdapter as new SqlDataAdapter
Dim My_BindingSource as new BindingSource
conn.Open()
My_Trans = conn.BeginTransaction()
myqry = "Select * From My_Table"
My_SQLDataAdapter = New SqlDataAdapter(myqry, conn)
Try
My_SQLDataAdapter.SelectCommand.Transaction = My_Trans
My_BindingSource.DataSource = My_Table
DataGridView1.DataSource = My_BindingSource '(<----- her is take long time)
My_Trans.Commit()
Catch ex As Exception
My_Trans.Rollback()
End Try
conn.Close()
but still take the same time , even for update command too, when tracking found the time taking in (DataGridView1.DataSource = My_BindingSource)
any advise please how speed up fill and update process?
many thanks in advance
I have tried as follow:-
1- Adding Class
Namespace DataSet1TableAdapters
Partial Public Class Table1TableAdapter
Public Function CreartTransaction() As Data.IDbTransaction
Try
Dim cmd As SqlCommand
Dim oConnection = me.CommandCollection(0).Connection
if oConnection.State = ConnectionState.Open
Else
oConnection.Open()
End If
Dim oTrans As SqlTransaction = oConnection.BeginTransaction()
For Each cmd In me.CommandCollection
cmd.Connection = oConnection
cmd.Transaction = oTrans
Next
Return oTrans
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Function
End Class
End Namespace
2- then on from added this code to save changes (Delete/Add/Modified):-
Dim oTrans As SqlTransaction = Table1TableAdapter.CreartTransaction
me.Table1TableAdapter.Transaction = oTrans
try
me.Table1TableAdapter.Update(me.DataSet1.Table1) (<----- Here when Have new rows not work and stop process without any error message)
oTrans.Commit
Catch ex As Exception
oTrans.Rollback
End Try
if Modified or deleted rows everything works fine, but if new rows added stacked and stop process without error message,
any explanation why ?
many thanks

How to add a column to a mysql database using visual basic

My Goal: Add a column to mysql database using visual basic!
I have tried multiple ways, but here is the code I'm currently using:
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "Server=xxx.xx.xxx.xxx;Port=3306;Database=test;Uid=tester;Pwd=***"
Try
MysqlConn.Open()
Dim Query As String
Query = "ALTER TABLE user ADD test INT NOT NULL"
COMMAND = New MySqlCommand(Query, MysqlConn)
MysqlConn.Close()
Catch ex As Exception
End Try
End Sub
I think it's a problem with the query! Thanks for your help in advance!
This is different from ms sql, it's visual basic.
My visual studio was actually not using my most up to date code and now everything is working LOL. Here is my revised code:
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "Server=**.**.***.***;Port=3306;Database=test;Uid=tester;Pwd=***"
Dim READER As MySqlDataReader
Try
MysqlConn.Open()
Dim Query As String
Query = "alter table test.user add test int not null"
COMMAND = New MySqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
MysqlConn.Close()
Catch ex As Exception
End Try

Why does MysqlCommand.ExecuteScalar return System.NullReferenceException

I am using MySQL in a VB 2013 project. I am trying to get the number of records in a table running
Dim SQLstr As String = "SELECT COUNT(*) FROM lieferantenartikel WHERE LiefID=1 AND LfAIDLief=1"
Dim CheckExist As New MySqlCommand(SQLstr, New MySqlConnection(strConn))
Try
Dim recEx As Integer = CheckExist.ExecuteScalar()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
By definition COUNT(*) is supposed to return a scalar number, i.e. 0 if no records exist or otherwise any other positive number.
But I am getting a System.NullReferenceException. What am I doing wrong?
I had similar issues. This happened when a table had no entries. You can Do a workaround with "ISNULL". But maybe there is a better way.
In fact the problem has gon when I used an open connection instead of the connection object ! Thank you, plutonix. I have obviously overseen in the documentation that the connection has to be open.
So the corrent code is now:
Dim SQLstr As String = "SELECT COUNT(*) FROM lieferantenartikel WHERE LiefID=1 AND LfAIDLief=1"
Dim cConn as New MySqlConnection(strConn)
cConn.Open()
Dim CheckExist As New MySqlCommand(SQLstr, cConn)
Try
Dim recEx As Integer = CheckExist.ExecuteScalar()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Thank you very much for your quick help!e

vb.net mysql combobox show tables

I am new at programming, and I'm working on a basic VB.NET application that allows the user to select, insert, update, and delete various data tables from MySQL.
The trouble I'm having is, I need to populate a combobox with all the table names from one specific database, so that the user can select which database table to work with. I thought my code would work, but all I'm getting when I run the app is a blank combobox.
Could someone please tell me what's wrong with my code?
Thanks so much in advance!
Code:
Private Sub TableList_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles TableList.SelectedIndexChanged
Try
command = New MySqlCommand
dt = New DataTable
adapter = New MySqlDataAdapter
If (conn.State = ConnectionState.Closed) Then
setConnection()
End If
command.Connection = conn
command.CommandText = "SHOW TABLES"
adapter.SelectCommand = command
reader = command.ExecuteReader
'adapter.Fill(dt)
dt.Load(reader)
TableList.DataSource = dt
TableList.DisplayMember = "Tables_in_sampledata" 'What is displayed
TableList.ValueMember = "Tables_in_sampledata" 'The ID of the row
Catch ex As MySqlException
MessageBox.Show("Error1: " & ex.Message)
Finally
reader.Close()
conn.Close()
End Try
End Sub
Instead of SHOW TABLES, use the following query
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA='YourDatabase';

Grab a Column in MySQL in VB.NET

I have just learned MySQL in VB.NET, but I am having a complication..
When I grab a SELECT Query, I want to get - lets say the 'username' column in each row it receives. How would I do that?
MySQL.CommandText = "SELECT * FROM online"
MySQL.ExecuteNonQuery()
Label1.Text = 'usernamehere'
Thanks :)
Public Sub CreateMySqlDataReader(mySelectQuery As String, myConnection As MySqlConnection)
Dim myCommand As New MySqlCommand(mySelectQuery, myConnection)
myConnection.Open()
Dim myReader As MySqlDataReader
myReader = myCommand.ExecuteReader()
Try
While myReader.Read()
Console.WriteLine(myReader.GetString(0))
End While
Finally
myReader.Close
myConnection.Close
End Try
End Sub
Found on: http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlcommand.html#connector-net-examples-mysqlcommand-executereader
Also as a bonus. If you are going to throw in some parameters in that query I would suggest you look into prepared statements.