Author Topic: How to open an access database via .net?  (Read 2168 times)

0 Members and 1 Guest are viewing this topic.

Bob Q

  • Guest
How to open an access database via .net?
« on: December 18, 2013, 02:24:58 PM »


I have updated a phone directory and put the data into an access database.
I was using the below Process.Start command to open the text file.
I thought I could use a similar statement to open an access database for viewing, but it didn't work.
So I googled it found a piece of sample code, tweaked it to the correct path, but it didn't work either.
Could anyone point me in the right direction possibly?
Thanks!  :lol:

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        'Process.Start("nProcess.Start("notepad.exe", "P:\lsp2\misc\Phone.txt")

        'first try, just use the same "process.start" type statement.... didn't work!!!!!!
        'Process.Start("nProcess.Start("msaccess.exe", "P:\lsp2\misc\Phone_Directory_2013.accdb")



        '*********************************
       'Second try, using sample cod off the net..... didn't work!!!!

        'Dim oAccess As Application


        ' Start a new instance of Access for Automation:
        'oAccess = New Access.ApplicationClass()

        ' Open a database in exclusive mode:
        'oAccess.OpenCurrentDatabase(filepath:="P:\lsp2\misc\Phone_Directory_2013.accdb", Exclusive:=True)
        '*********************************



    End Sub

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: How to open an access database via .net?
« Reply #1 on: December 18, 2013, 02:45:15 PM »
Check out System.Data and System.Data.Odbc namespaces.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: How to open an access database via .net?
« Reply #2 on: December 18, 2013, 02:54:16 PM »
Do you simply want to open Access or do you want to access the data store in the Access database and display it in your application? That would determine how you would proceed.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Bob Q

  • Guest
Re: How to open an access database via .net?
« Reply #3 on: December 18, 2013, 03:24:58 PM »

I want to open access with the targeted "phone_directory_2013" database.
i.e. just as if you were to open it through explorer by double clicking on it.
Thanks!

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: How to open an access database via .net?
« Reply #4 on: December 18, 2013, 03:32:07 PM »
Ok, then try this:

Code: [Select]
Process.Start("P:\lsp2\misc\Phone_Directory_2013.accdb")
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

MickD

  • King Gator
  • Posts: 3646
  • (x-in)->[process]->(y-out) ... simples!
Re: How to open an access database via .net?
« Reply #5 on: December 18, 2013, 03:34:23 PM »
you may want to use a ProcessStartInfo class to set up you Process.Start arguments, that way you can use command line switches to select the database, use passwords etc.

Here are some switches -> http://support.microsoft.com/kb/209207

here's some rough code

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "Access.exe";
startInfo.Arguments = "database databasefile.mdb"; // something like that anyway...

Process.Start(startInfo);

very rough but you get the idea :)
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

fixo

  • Guest
Re: How to open an access database via .net?
« Reply #6 on: December 19, 2013, 06:39:29 AM »

I want to open access with the targeted "phone_directory_2013" database.
i.e. just as if you were to open it through explorer by double clicking on it.
Thanks!

You may want to try this example, tested on Access 15,
change path of database in the connection string and table name in the query:
Code: [Select]
        Private Sub TestReadMyDataTable()

            Try
                ' Create a connection string
                Dim connStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Test\MyDataBase.mdb;Jet OLEDB:Database Password=;"
                ' Select fields from table Blocks
                Dim sqlQuery As String = "SELECT BlockName, TAG1, TAG2, TAG3, TAG4 From [Blocks]" ''<---   Blocks is table name
                ' Create a Connection object and open it
                Dim conn As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection()

                conn.ConnectionString = connStr
                conn.Open()
                ' Dim connMode As Integer = ADODB.ConnectModeEnum.adModeRead
                Dim cmd As System.Data.OleDb.OleDbCommand = conn.CreateCommand()
                cmd.CommandType = CommandType.TableDirect
                cmd.CommandText = sqlQuery
                ' Create dataset and data adpater objects
                Dim ds As System.Data.DataSet = New System.Data.DataSet("MyTable")
                Dim da As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
                da.SelectCommand = cmd
                ' Call data adapter's Fill method to fill data from ADODB
                ' Recordsets to the dataset
                da.Fill(ds, "MyTable")
                ' Now use dataset
                Dim dt As System.Data.DataTable = ds.Tables(0)

                Dim sb As New StringBuilder
                Dim strRow As String
                For Each row As System.Data.DataRow In dt.Rows
                    strRow = " | "
                    For Each item As Object In row.ItemArray
                        strRow = strRow + " " + item.ToString
                    Next
                    sb.AppendLine(strRow)
                Next
                MsgBox(sb.ToString())
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End Sub

fixo

  • Guest
Re: How to open an access database via .net?
« Reply #7 on: December 19, 2013, 04:59:27 PM »
Here is winforms project just for your test