I am newbie in mysql and vb.net and have this query:
select `db_employee`.`firstName` as `firstName`, `db_employee`.`lastName` as `lastName`,
`tbl_employment`.`position` as `position`
from (`db_employee` `db_employee`
inner join `tbl_employment` `tbl_employment` on (`tbl_employment`.`ctrlID` = `db_employee`.`ctrlID`))
where (`tbl_employment`.`companyName` like 'MES%')
And I want to convert it to be used here:
Dim MySqlConn = New MySqlConnection
MySqlConn.ConnectionString = connStr
Dim SDA As New MySqlDataAdapter
Dim dbdataset As New DataTable
Dim bsource As New BindingSource
MySqlConn.Open()
Dim query As String
query = ?
you can use below code for displaying data... i hope this what you want.. :)
Dim cn As SqlConn= New SqlConn("Connecting String")
Dim cmd As sqlCommand
Dim dr As sqlDataReader
cn.open()
cmd = New sqlCommand("select * from table or your query, cn)
dr = cmd.ExecuteReader()
dr.Read()
textbox1.Text = dr(1).ToString()
cn.close()
You can do something like this with the MySQL provider you're using. This will create your connection, command and data reader and close and dispose of everything properly. If you want a DataTable instance, you can load it from the DataReader.
Using conn As New MySql.Data.MySqlClient.MySqlConnection("YourConnectionString")
conn.Open()
Using cmd As MySql.Data.MySqlClient.MySqlCommand = conn.CreateCommand
cmd.CommandText = "select db_employee.firstName as firstName, " & _
"db_employee.lastName as lastName, " & _
"tbl_employment.position as position " & _
"from db_employee db_employee " & _
"inner join tbl_employment tbl_employment on tbl_employment.ctrlID = db_employee.ctrlID " & _
"where tbl_employment.companyName like 'MES%' "
' Get a data reader that you can loop over or load a data table with
Dim dr As MySql.Data.MySqlClient.MySqlDataReader = cmd.ExecuteReader
' How to read with a DataReader
While dr.Read
' Do something with this
Dim lastName As String = dr("lastName")
End While
End Using
conn.Close()
End Using
Related
hi guys im new here and i want to show the appointments in currentdate in vb.net using mysql here is my code heres my code
cn.Open()
cm = New MySqlCommand("select count(*) from tblappointment where date=" & Date.Today & "", cn)
frmDashBoard.lblAppointment.Text = Format(CLng(cm.ExecuteScalar), "#,##0")
cn.Close()
You would do it diirectly in mysql
cn.Open()
cm = New MySqlCommand("select count(*) from table1 where `date`= cur_date", cn)
frmDashBoard.lblAppointment.Text = Format(CLng(cm.ExecuteScalar), "#,##0")
cn.Close()
But for the future when you add variables to your code use placeholders for
dim name as string
cnn.ConnectionString = "............"
cmd.Connection = cnn
cnn.Open()
cmd.CommandText = "SELECT Name, adress from tblappointment WHERE ID= #id"
cmd.Parameters.AddWithValue("#id", txtIDInput.Text )
Dim reader = cmd.ExecuteReader()
while reader.Read()
name= reader(1).ToString()
End While
Im a bit confused. I have a mySQL database. It is hashed/encrypted. I used MySqlDataReader and my first query was:
SELECT SUM(column01) FROM dbtest.table01 WHERE column02 = '" & encrypt(selectedValue, salt) & "'
Problem is, the data inside the database is hashed/encrypted that is why it cannot compute the data. Now I switched to MySqlDataAdapter using this query I used in displaying the data in DataGridView(dehashed/decrypted data are displayed here):
SELECT column01 FROM dbtest.table01 WHERE column02 = '" & encrypt(_selectedValue, salt) & "'
Here is my code so far.
Dim dataAdapter As New MySqlDataAdapter
Dim dataTable As New DataTable
Dim bSource As New BindingSource
Try
conn.Open()
Dim query As String
query = "SELECT column01 FROM dbtest.table01 WHERE column02 = '" & encrypt(_selectedValue, salt) & "'"
command = New MySqlCommand(query, conn)
dataAdapter.SelectCommand = command
dataAdapter.Fill(dataTable)
bSource.DataSource = dataTable
DataGridView_Moon.DataSource = bSource
For i As Integer = 0 To dataTable.Rows.Count - 1
dataTable.Rows(i)("column01") = decrypt(dataTable.Rows(i)("column01"), salt)
Next
conn.Close()
Catch ex As Exception
Finally
conn.Dispose()
End Try
If you could, pls place the totalValue into a messagebox. Thanks
I was making a vb program that searches a student info by searching a course.. For example Ajax's course is "BSCS" and DP's course is "BSIT" , then i'm going to select BSCS, so Ajax's must only appear not DP. The result appear's in DataGridView
myConnection.Open()
Dim str As String
str = "SELECT * FROM students WHERE (Course = '" & TextBox1.Text & "')"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
While dr.Read()
FirstNameTextBox = dr("FirstName")
MiddleNameTextBox = dr("MiddleName")
LastNameTextBox = dr("LastName")
AddressTextBox = dr("Address")
CellphoneNumberTextBox = dr("CellphoneNumber")
CourseTextBox = dr("Course")
End While
myConnection.Close()
Based on your Code you should add new DataGridView to your form and also adding columns with header name that you choose and replace the code with mine
myConnection.Open()
Dim str As String
str = "SELECT * FROM students WHERE (Course = '" & TextBox1.Text & "')"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
While dr.Read()
DataGridView1.Rows.Add({dr("FirstName"), dr("MiddleName"), dr("LastName"), dr("Address"), dr("CellphoneNumber"), dr("Course")})
End While
myConnection.Close()
I have this problem:
MySQL server version for the right syntax to use near 'where no ='count'' at line 1
This is my code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "server=localhost;userid=root;password=;database=penjara"
Dim Reader As MySqlDataReader
MysqlConn.Open()
Dim query As String
Dim spath As String
Dim count As Integer = 0
Dim mysound As Media.SoundPlayer
Dim cmd As MySqlCommand
Dim rdr As MySqlDataReader
query = "Select * from penjara.info"
Command = New MySqlCommand(query, MysqlConn)
Reader = Command.ExecuteReader
While Reader.Read
Reader.Close()
count = count + 1
query = "Select penjara.info where no ='count'"
cmd = New MySqlCommand(query, MysqlConn)
rdr = cmd.ExecuteReader
End While
End Sub
How can I solve this error?
Try this:
query = "Select * from penjara.info where no = '" & count & "'"
This will pass in the value of variable count. However, note that this is an unsafe approach since concatenating to form queries makes your app vulnerable to SQL Injection. Instead you should use parameterised queries.
Dim connStr As String = "server=localhost;userid=root;password=;database=penjara"
Using MySqlConn As New MySqlConnection(connStr)
Using cmd As New MySqlCommand()
With cmd
.Connection = MySqlConn
.CommandText = "Select * from penjara.info where no = #count"
.CommandType = CommandType.Text
.Parameters.AddWithValue("#count", count)
End With
Try
MySql.Open()
rdr = cmd.ExecuteReader
Catch ex As Exception
<handle exception>
End Try
End Using
End Using
Try
cn.Open()
Dim query As String
Dim fname As String
Dim reader As MySqlDataReader
query = "Select emp_id,emp_fname,emp_lname,empmname,position,branch from dtrsystem.tblemployee where emp_id = '" & Class1.empid & "' "
sql = New MySqlCommand(query, cn)
reader = sql.ExecuteReader
cn.Close()
Catch ex As Exception
End Try
i want to store my emp_id,emp_fname,emp_lname,empmname,position,branch in a variable and show it in a label. I'm new to vb.net and tried the codes i've searched but it doesnt work. please help
''declare variables v1,v2,v3
Try
cn.Open()
Dim query As String
Dim fname As String
Dim reader As MySqlDataReader
query = "Select emp_id,emp_fname,emp_lname,empmname,position,branch from dtrsystem.tblemployee where emp_id = '" & Class1.empid & "' "
sql = New MySqlCommand(query, cn)
reader = sql.ExecuteReader
while reader.read
v1=reader(0) 'emp id
v2=reader(1) 'emp fname
v3=reader(2) ' emp lname
''etc
End While
cn.Close()
Catch ex As Exception
End Try