CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2009
    Posts
    192

    Code Enhancement

    Hallo,

    I am working with a Vb.net 2005 application. On the application I have a Create table button which when clicked creates a table in Sql server with the code shown below;

    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()

    sql = "CREATE TABLE myTable" + "(myId INTEGER CONSTRAINT PKeyMyId PRIMARY KEY," + "myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)"
    cmd = New SqlCommand(sql, conn)
    Try
    cmd.ExecuteNonQuery()
    ' Adding records the table
    sql = "INSERT INTO myTable(myId, myName, myAddress, myBalance) " + "VALUES (1001, 'Puneet Nehra', 'A 449 Sect 19, DELHI', 23.98 ) "
    cmd = New SqlCommand(sql, conn)
    cmd.ExecuteNonQuery()

    sql = "INSERT INTO myTable(myId, myName, myAddress, myBalance) " + "VALUES (1002, 'Anoop Singh', 'Lodi Road, DELHI', 353.64) "
    cmd = New SqlCommand(sql, conn)
    cmd.ExecuteNonQuery()

    sql = "INSERT INTO myTable(myId, myName, myAddress, myBalance) " + "VALUES (1003, 'Rakesh M', 'Nag Chowk, Jabalpur M.P.', 43.43) "
    cmd = New SqlCommand(sql, conn)
    cmd.ExecuteNonQuery()

    sql = "INSERT INTO myTable(myId, myName, myAddress, myBalance) " + "VALUES (1004, 'Madan Kesh', '4th Street, Lane 3, DELHI', 23.00) "
    cmd = New SqlCommand(sql, conn)
    cmd.ExecuteNonQuery()

    Catch ae As SqlException
    MsgBox("Table has been created successfully", MsgBoxStyle.Information, "Load Data Application")
    'MessageBox.Show(ae.Message.ToString())
    End Try

    End Sub


    This is really a pre-defined table and the data is already specified to be inserted to the table. However, my primarily task is to read a flat file which has the data and consider the first line as the column to be column names.

    Therefore, initially I created an open dialogue to browse for the text file and store the path such as E:\Datafile\test.txt, in a TextBox1 visible at run time.

    Now the task is to change the above "create table" to read the txt file (column names are on the first line) and then create the table in the database.

    I hope am clear to what I want to achieve, any help will be highly appreciated.

    Thank you in advance

  2. #2
    Join Date
    Aug 2009
    Posts
    12

    Re: Code Enhancement

    Well, I understood. But, there is a problem concerning the data-file, do you use tab to seperate each value? Could you give me an example.

    By they way, don't use + in concatenating string, use & instead. Use + only for the math operation.

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Code Enhancement

    Not a matter in .Net, though + or &
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Code Enhancement

    The best approach would be to make use of Store Procedures, much easier, much quicker, and easier to manipulate

    //PS, dr233 please make use of [CODE] and [/CODE] Tags when posting code

  5. #5
    Join Date
    Aug 2009
    Posts
    12

    Re: Code Enhancement

    I think he used some tools to create a datafile, may be in CSV or XLS format, and then have a program to read the data file, and inserting the data into the database.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured