oi211
oi211
采纳率100%
2015-01-04 06:06 阅读 3.0k

用vb.net连接sql做的表名查询

用vb.net连接sql做的表查询将查找到的表用DataGridView显示出来,现在的问题是如何选中一个表然后跳转到另一个窗口进行查询表内容

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    caozhy 从今以后生命中的每一秒都属于我爱的人 2015-01-04 06:32

    编写CellClick事件的处理函数,获取所在单元格的所在行,根据id去查询,然后显示出来。

    窗体传值参考我的例子

    http://bbs.csdn.net/topics/360140208

    点赞 评论 复制链接分享
  • oi211 oi211 2015-01-04 06:47

    问题是不知道怎么获得DataGridView显示的表名

    点赞 评论 复制链接分享
  • caozhy 从今以后生命中的每一秒都属于我爱的人 2015-01-04 06:52

    DataGridView不是你加载的么?你可以将表名存在tag属性中,或者用(datasource as datatable).tablename

    点赞 评论 复制链接分享
  • oi211 oi211 2015-01-04 07:09

    **这是我的代码 窗体13用来获取表名
    Imports System.Data.SqlClient

    Public Class Form13

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim strConnection As String = "Server=DELL-PC\SQLEXPRESS;database=renshi;uid=ouqing;pwd=123;"
        Dim sqlConnection1 As New SqlConnection(strConnection)
        Dim dataAdapter As New SqlDataAdapter
        Dim dst As New DataSet
        Dim dt As New DataTable
        sqlConnection1.Open()   '打开数据库  
        If TextBox1.Text = "" Then
            Dim sql As String = "select name From sys.tables"
            Dim cmd As SqlCommand = New SqlCommand(sql, sqlConnection1)
            dataAdapter.SelectCommand = cmd
            dataAdapter.Fill(dst, "name")
            dt = dst.Tables("name")
            sqlConnection1.Close()   '关闭数据库  
            DataGridView1.AutoGenerateColumns = True '自动创建列  
            DataGridView1.DataSource = dt
        Else
            Dim sql As String = "select name From sys.tables where name like '%" & TextBox1.Text & "%'"
            Dim cmd As SqlCommand = New SqlCommand(sql, sqlConnection1)
            dataAdapter.SelectCommand = cmd
            dataAdapter.Fill(dst, "name")
            dt = dst.Tables("name")
            sqlConnection1.Close()   '关闭数据库  
    
    
            DataGridView1.AutoGenerateColumns = True '自动创建列  
            DataGridView1.DataSource = dt
        End If
    
    End Sub
    

    **
    ** Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim SelRowColl As DataGridViewSelectedRowCollection
    Dim dgvRow As String
    Dim t As New Class1
    Dim i As Integer
    SelRowColl = DataGridView1.SelectedRows
    For i = 0 To SelRowColl.Count - 1
    dgvRow = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells("name").ToString
    '可得到选中的每一行

            t.setuse(dgvRow) '用存储获得的值
        Next
    
        Form20.Show() ‘窗体20用来显示选中的表的内容
    
    
    End Sub
    

    ** 这是窗体20的代码**
    Public Class Form20

    Private Sub Form20_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    
        Dim strConnection As String = "Server=DELL-PC\SQLEXPRESS;database=renshi;uid=ouqing;pwd=123;"
        Dim sqlConnection1 As New SqlConnection(strConnection)
        Dim dataAdapter As New SqlDataAdapter
        Dim dst As New DataSet
        Dim dt As New DataTable
        sqlConnection1.Open()   '打开数据库  
        Dim dgv As Object
        dgv = Class1.useid1
    
        Dim sql As String = "select * From '" & Class1.useid1 & "'"
        Dim cmd As SqlCommand = New SqlCommand(sql, sqlConnection1)
        dataAdapter.SelectCommand = cmd
        dataAdapter.Fill(dst, " & Class1.useid1 &")
        dt = dst.Tables(" & Class1.useid1 &")
        sqlConnection1.Close()   '关闭数据库  
    
    
        DataGridView1.AutoGenerateColumns = True '自动创建列  
        DataGridView1.DataSource = dt
    End Sub
    

    End Class**

    点赞 评论 复制链接分享

相关推荐