Hallo,
I have the follwoing code which reads a .txt file on my form and shows the path on a textbox called "TextBox1".
The second stage of my development is to have a createtable button. Which when clicked will open the ODBC connection and create a table in a database in SQL.Code:Private Function GetDBName(ByVal filename As String) Dim MyStreamReader As StreamReader Dim pathfile As String pathfile = TextBox1.Text Try 'Open File MyStreamReader = File.OpenText(pathfile) 'Read Text Dim ThisString As String = MyStreamReader.ReadLine 'Close File MyStreamReader.Close() MyStreamReader = Nothing Return ThisString Catch ex As IOException Throw New Exception("Unable To Read Text From File", ex) Finally MyStreamReader = Nothing End Try End Function
Please note: The first line of the .txt file is the column names. Therefore the code in VB.net should recognise the column names and save the respective data in each field in SQL server.
The table name can be called "mytable"... I have something below but is with alot of errors can anyone help me pls..
Any help will indeed be highly appreciated.Code:Private Sub BtnCrtTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCrtTable.Click ' Open the connection Dim sql As String Dim cmd As New SqlCommand Dim conn As SqlConnection = GetDbConnection() Dim DBName As String = GetDBName(TextBox1.Text) sql = "CREATE TABLE " & DBName & " cmd = New SqlCommand(sql, conn) Try cmd.ExecuteNonQuery() ' Adding records to the table sql = "INSERT INTO " & DBName &" Catch ae As SqlException MsgBox("Table has been created successfully", MsgBoxStyle.Information, "Load Data Application") 'MessageBox.Show(ae.Message.ToString()) End Try End Sub
Thanks




Reply With Quote