在datagridview对象上显示来自mysql服务器的数据.没有显示任何东西 [英] Displaying data from a mysql server on a datagridview object. is not displaying anything

查看:121
本文介绍了在datagridview对象上显示来自mysql服务器的数据.没有显示任何东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过使用数据集工具在设计端链接数据集,但是由于它不断消失而无法正常工作,这是另一种方法,它无法在数据网格视图上显示任何值,我不确定为什么我对vb的这一方面还很陌生,所以如果您也能解释一下,那就太好了.预先感谢.

I have tried making a dataset using the dataset tool to link it on the design side however that hasn't worked as it keeps disappearing and this is the other way it does not display any values on the datagrid view i am unsure why and i am fairly new to this side of vb so if you could explain it as well that would be great. Thanks in advance.

Imports MySql.Data.MySqlClient

Public Class Search
    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            If TextBox1.Text = "" Then 'this acts as a simple presence check on the textbox
            Else
                Dim val = "name"
                If RadioButton1.Checked = True Then 'This changes the type of search i do as it filters which column the query looks in
                    val = "type"
                End If
                Await getDataSet(TextBox1.Text) ' waits for infomation to be retrieved
            End If
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message) 'Catches any errors 
        End Try
    End Sub

    Async Function getDataSet(partname As String) As Task(Of DataSet) 'This retrieves the values that matches the users input 
        Return Await Task.Factory.StartNew(
    Function()
        Dim connectionString = "server=localhost; userid=root; password=; database=partstest1; CharSet=utf8;" 'These are the login details for the database in the form of a connection string 
        Dim commandText = "SELECT ID, Benchpoint, Name, Type, BrandID FROM `parts` WHERE `name` Like  '%" & TextBox1.Text & "%';"
        Using connDB = New MySqlConnection(connectionString), objCmd = New MySqlCommand(), objAdpt = New MySqlDataAdapter()
            connDB.Open()
            objCmd.Connection = connDB
            objCmd.CommandText = commandText
            objCmd.CommandType = CommandType.Text 'These lines specify the command i am using and execute it
            objAdpt.SelectCommand = objCmd
            Dim objDs = New DataSet()
            objAdpt.Fill(objDs) 'Puts all of the values into a dataset
            PartsDataGridView.DataSource = objDs.Tables(0) 'This shows the datasource and displays it
            Console.WriteLine(objDs)
            Return objDs
        End Using
    End Function)
    End Function
End Class

推荐答案

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆