SQL statements in Visual Studio .NET applications

0

With the following image I intend to explain myself in a better way.

My goal is to create an App similar to Query browser. (I do not know if it will be correct to say interpreter SQL simulator).

  • Being able to execute SQL statements
  • View records
  • Show errors in the statements.
  • But when making the connection you must define to which database you want to connect, which prevents me from sending a "Create Database", I can only work within the database, so add the combobox to change the database .

    Summing up: How can I execute a database creation statement from a textbox.

    They asked me to show the code that I have been carrying so far, so here I leave it. I have it in a button that I added later although I would like it to be executed when pressing F5

        Dim sentencia As String
        sentencia = TextBox1.Text
        TextBox3.Text = sentencia
    
        Dim con As New SqlConnection(My.Settings.Conexion)
        Dim sql As String = sentencia
        Dim cmd As New SqlCommand(sql, con)
    
        Try
            Dim da As New SqlDataAdapter(cmd)
            Dim ds As New DataSet
            da.Fill(ds, "Tabla1")
    
            Me.DataGridView1.DataSource = ds.Tables("Tabla1")
    
        Catch ex As Exception
            MsgBox(ex.Message)
            TextBox3.Text = ex.Message
        End Try
    
    
    End Sub
    
        
    asked by Vicete Geovanny Franco Siles 26.05.2017 в 00:58
    source

    1 answer

    0

    With this code you can create the BD, only concatenates the name of the database of the text box, so that it is parametric.

        Dim str As String
    
        Dim myConn As SqlConnection = New SqlConnection("Server=(local)\netsdk;" & _
                                                        "uid=sa;pwd=;database=master")
    
        str = "CREATE DATABASE MyDatabase ON PRIMARY " & _
              "(NAME = MyDatabase_Data, " & _
              " FILENAME = 'D:\MyFolder\MyDatabaseData.mdf', " & _
              " SIZE = 2MB, " & _
              " MAXSIZE = 10MB, " & _
              " FILEGROWTH = 10%) " & _
              " LOG ON " & _
              "(NAME = MyDatabase_Log, " & _
              " FILENAME = 'D:\MyFolder\MyDatabaseLog.ldf', " & _
              " SIZE = 1MB, " & _
              " MAXSIZE = 5MB, " & _
              " FILEGROWTH = 10%) "
    
        Dim myCommand As SqlCommand = New SqlCommand(str, myConn)
    
        Try
            myConn.Open()
            myCommand.ExecuteNonQuery()
            MessageBox.Show("Database is created successfully", _
                            "MyProgram", MessageBoxButtons.OK, _
                             MessageBoxIcon.Information)
           Catch ex As Exception
               MessageBox.Show(ex.ToString())
           Finally
               If (myConn.State = ConnectionState.Open) Then
                   myConn.Close()
               End If
           End Try
    
    End Sub
    

    Of course you should re-name the files of the BD and MDF of the BD

    With this example you sure do.

    Greetings

        
    answered by 26.05.2017 в 05:02