如何使用vb.net在sql server上创建一个表,其中用户输入了名称

时间:2016-03-22 10:08:48

标签: mysql sql-server database vb.net datagridview

我想用vb.net在sql server上创建一个表,但是用户应该输入服务器,数据库和表名,为此我尝试使用新表单但是在输入信息之前执行了代码

这是我的代码

Form2.Show()
    Dim grid = DirectCast(Me.TabControl1.SelectedTab.Controls(0), DataGridView)
    Dim header As String = ""
    Dim sql As String
    connection = New SqlConnection("Data Source=" & Form2.TextBox2.Text & ";Initial Catalog=" & Form2.TextBox3.Text & ";integrated security=true")
    For Each column As DataGridViewColumn In grid.Columns
        header = header & column.HeaderText & " varchar(50) NULL,"
    Next
    sql = "CREATE TABLE " & Form2.TextBox1.Text & "(" & header & ")"
    Dim Mycommand As SqlCommand = New SqlCommand(sql, connection)
    Try
        Mycommand.Connection.Open()
        Mycommand.ExecuteNonQuery()
        Mycommand.Connection.Close()
    Catch
        MsgBox(" Already installed table")
    End Try

非常感谢任何有关此事的帮助

1 个答案:

答案 0 :(得分:0)

您应该使用ShowDialog而不是ShowShow将显示该表单,然后继续执行其余代码,ShowDialog将阻止,直到表单关闭。