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

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

4个回答

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

窗体传值参考我的例子

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

whzmtc
whzmtc 要得
5 年多之前 回复
oi211
oi211 问题是不知道怎么获得DataGridView显示的表名
5 年多之前 回复

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

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

**这是我的代码 窗体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**

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐