Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have this select statement to select the file to insert into database.
SQL
Dim query As String = "INSERT INTO  test (School, Campus, AdminNo, ModuleCode, ModuleGrp) " & _
       "SELECT F1 as School, F2 as Campus, F3 as AdminNo, F4 as ModuleCode, F5 as ModuleGrp FROM [Text;HDR=NO;DATABASE=J:\FYP\;].[SEG1.txt]"


i can i change the select from file to the openfiledialog path/file?

i tried:

SQL
Dim query As String = "INSERT INTO  test (School, Campus, AdminNo, ModuleCode, ModuleGrp) " & _
        "SELECT F1 as School, F2 as Campus, F3 as AdminNo, F4 as ModuleCode, F5 as ModuleGrp FROM [Text;DATABASE=" & System.IO.Path.GetDirectoryName(Me.OpenFileDialog1.FileName) & ";HDR=NO].[" & Me.OpenFileDialog1.SafeFileName & "]"





Thanks in advance!
Posted
Comments
Basmeh Awad 9-Jul-13 4:45am    
so what is the error??
where did you stuck??
12345_abcde 9-Jul-13 4:48am    
the code i tried, it cant work. as for the first one, it works fine.
ArunRajendra 9-Jul-13 4:54am    
what do mean by not woking? is it throwing the exception? Did try to put the break poing and compare the generated string with working string?
12345_abcde 9-Jul-13 4:58am    
the columns and data are not showing.
i dont quite get what you meant. im still not familiar with VB.

Yes, you can ust it OpenFileDialog[^], but in different way, using string variable instead OpenFileDialog properties.

Please, create new WindowsApplication project and put one button on the form, then double-click on it. Copy and paste below code:
VB
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ofd As OpenFileDialog = Nothing
        Dim sFullFileName As String = String.Empty
        Try
            ofd = New OpenFileDialog()
            With ofd
                .InitialDirectory = Environment.SpecialFolder.MyDocuments
                .Multiselect = False
                .DefaultExt = "txt"
                .Filter = "txt files (*.txt)|*.txt"
                If .ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Try
                sFullFileName = .FileName
            End With

            MsgBox("Directory name: '" & System.IO.Path.GetDirectoryName(sFullFileName) & "'" & vbCr & _
                "Short file name: '" & System.IO.Path.GetFileName(sFullFileName) & "'", MsgBoxStyle.Information, "Information...")

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...")
        Finally
            ofd = Nothing
        End Try
    End Sub
End Class
 
Share this answer
 
try to this...
C#
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
    strfilename = openFileDialog1.InitialDirectory + openFileDialog1.FileName;
}
 
Share this answer
 
Comments
12345_abcde 9-Jul-13 23:10pm    
erm this is for C# ? im using vb.net
[no name] 10-Jul-13 6:35am    
If ofd.ShowDialog = DialogResult.OK Then
Dim strfilename = ofd.InitialDirectory & "" & ofd.FileName
End If

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900