Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi!

I'm looking for a help on how i can write code in VB.NET so that i can browse a shapefile from my computer and save it to SQL database.
I know there are ready-made tools like this https://www.sharpgis.net/file.axd?file=SqlSpatialTools_build3759.zip[^] that are capable to do this task BUT
I want to do manually by writing some code on Visual studio 2017 and Microsoft SQL server 2017

The application will have two buttons, one button to browse file from computer and another save button to save shapefile into database and an OpenfileDialog box.

Can someone show the way how to go ...??

Thanks.

What I have tried:

VB
Private Sub BtnBrowse_Click(sender As System.Object, e As 
    System.EventArgs) Handles BtnBrowse.Click
    OpenFileDialog1.InitialDirectory = "E:\"
    OpenFileDialog1.Filter = "shapefile files (*.shp)|*.|All files 
    (*.*)|*.*"
    OpenFileDialog1.ShowDialog()
    txtFileName.Text = OpenFileDialog1.FileName

  End Sub

  Private Sub BtnSave_Click(sender As System.Object, e As 
   System.EventArgs) Handles BtnSave.Click
    Try
        'code to check if it is a shapefile



        'code to save shapefile in database 

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

    End Sub
Posted
Updated 25-Jul-19 22:40pm
v3
Comments
Richard MacCutchan 25-Jul-19 5:42am    
You just need to use an SQL INSERT clause, and save the file as a BLOB.
mk2tk2 25-Jul-19 5:46am    
Hi!

@Richard MacCutchan

May you guide with a sample code? I still cant grasp what you said.
Thanks
Richard MacCutchan 25-Jul-19 6:19am    
Not really, since I have no idea about your database or what your issues may be. You need to think in more detail about what problem you are trying to solve: How to read theses files, where you want to store them, is a database the right solution, etc.

1 solution

Quote:
'code to check if it is a shapefile

Two options:
1) Basic - just check the extension. Path.GetExtension Method (System.IO) | Microsoft Docs[^] will return ".SHP" or ".TXT" as appropriate. But ... why do you allow non ".SHP" files if you only want those?
2) Advanced: read the file and check it as a valid shapefile. Complicated, but this may help: GIS – How to Check a Shapefile’s Data in C# – Insights into Software Development[^], yes, it's in C#. Provided it's in a separate assembly, thatisn;t a problem, your VB code can use it.
Quote:
'code to save shapefile in database

Read the file as binary data into a Byte array.
Create an INSERT command connected to your DB, and pass the Byte array directly as a parameter.
Execute the command to SQL.
 
Share this answer
 

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