• 0

[C#] ADODB Connection State


Question

Hey guys..

I'm a C# newbie, I was able to get my program to connect to an Access DB using ADO, but now I wanna wask about the Connection.State property, and I don't know how....I mean is it Connection.State=1 or Connection.State=adStateOpen ?? so I was wondering if anyone can give me some pointers....also if you have some info on Error Handling I would appreciate it :laugh:

Thanks ;)

Link to comment
https://www.neowin.net/forum/topic/249301-c-adodb-connection-state/
Share on other sites

9 answers to this question

Recommended Posts

  • 0

Hiya ArBoK,

There are no "magic numbers" in .NET (appart from one function that uses a magic number). Absolutely everything is object oriented and usually eccepts and returns objects.

The first step in handling exceptions is to use the Try/Catch block:

try
{
 ? ?// some code here to attempt to execute
}
catch (Exception Ex)
{
 ? System.Windows.Forms.MessageBox(Ex.Message);
}

Hope this hel:D :D

- Tranq

  • 0

I don't really program in C# but here is some vb.net code which does the trick

it should not be that hard to convert it to C#, just a few semicolons here and there!

' You need the following data library
Imports System.Data.OleDb

' Declare variables
        Dim strConnectionParameters As String

        ' Connection string
        strConnectionParameters = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDatabasePath & ";Jet OLEDB:System database=" & strDatabaseWorkgroup

        ' Create a new connection to the database & open it
        Try
            myConnection = New OleDbConnection(strConnectionParameters)
            myConnection.Open()
        Catch ex As Exception
            MessageBox.Show("Aection to the database could not be established. The returned error was: " & ex.Message, "Database Connection Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try


' AND TO GET STATE

        ' 0 (closed), 1 (open), 2 (connecting), 4 (executing), 8 (fetching), 16 (broken)
        Dim result As Short = myConnection.State
        MsgBox(result)

  • 0

Don't assume that a given connection state will always equate to a given number (e.g. 0 = closed), as the numbers could be changed in the future by MS. Plus, using numbers can be difficult to read if the person reading doesn't know that 0 is closed, 1 is open and so on.

There is an enumeration named "ConnectionState" in the System.Data namespace. Use that instead:

ConnectionState.Closed

ConnectionState.Open

And so on. Intellisense would tell you this information as well.

  • 0
  DrZoidberg said:
Don't assume that a given connection state will always equate to a given number (e.g. 0 = closed), as the numbers could be changed in the future by MS.  Plus, using numbers can be difficult to read if the person reading doesn't know that 0 is closed, 1 is open and so on.

There is an enumeration named "ConnectionState" in the System.Data namespace.  Use that instead:

ConnectionState.Closed

ConnectionState.Open

And so on. Intellisense would tell you this information as well.

584992896[/snapback]

That's an enum, which has an integer backing it ;). You should always use Enum's, because they're a lot easier to read, and you dont have to remember integer values.

  • 0
  Sn1p3t said:
That's an enum, which has an integer backing it ;). You should always use Enums, because they're a lot easier to read, and you dont have to remember integer values.

584993667[/snapback]

Err, yes. I already said that in my post:

  DrZoidberg said:
Plus, using numbers can be difficult to read if the person reading doesn't know that 0 is closed, 1 is open and so on.
  • 0
  DrZoidberg said:
Err, yes.  I already said that in my post:
  DrZoidberg said:
Plus, using numbers can be difficult to read if the person reading doesn't know that 0 is closed, 1 is open and so on.

584996531[/snapback]

Oh, I know. I was simply explaining that to anyone who didn't know what it was :p

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • all these "selling for great prices" articles feel like filler anymore....
    • It has the same switch to allow whitelisting or not as the regular AdBlock extension.
    • These 20 crypto phishing applications are scamming Play Store users by Hamid Ganji Google Play Store is the main venue for Android users to download applications. While Google has strict rules and policies for verifying apps, some malicious apps somehow slip through anyway. Meanwhile, when it comes to crypto wallet apps, both Google app auditors and Play Store users need to be even more cautious. Cyble Research and Intelligence Labs has identified at least 20 crypto phishing applications on the Google Play Store that impersonate legitimate and popular crypto wallet apps and try to steal users' crypto credentials. By impersonation, these malicious apps trick users into downloading them and then start to capture the user's actual login data. "What makes this campaign particularly dangerous is the use of seemingly legitimate applications, hosted under previously benign or compromised developer accounts, combined with a large-scale phishing infrastructure linked to over 50 domains. This extends the campaign's reach and lowers the likelihood of immediate detection by traditional defenses." Cyble writes. Some of these malicious apps have the same name but come with a different package name. After removing duplicate names, here's the list of 9 newly discovered crypto phishing applications on the Play Store: Pancake Swap Suite Wallet Hyperliquid Raydium BullX Crypto OpenOcean Exchange Meteora Exchange SushiSwap Harvest Finance Blog According to Cyble, these apps prompt users to enter their 12-word mnemonic phrase to access the fake crypto wallet. Also, scammers use accounts that were previously used to distribute legitimate apps to minimize the risk of getting caught by Google. These accounts are more likely to be compromised and then taken over by scammers. If you've downloaded any of these fake crypto wallet apps from the Play Store, make sure to delete them as soon as possible. In 2024, revenue from crypto scams was estimated to be around $9.9 billion. This billion-dollar crypto scam business is expected to grow massively in 2025 thanks to AI.
  • Recent Achievements

    • Reacting Well
      viraltui earned a badge
      Reacting Well
    • Week One Done
      LunaFerret earned a badge
      Week One Done
    • Week One Done
      Ricky Chan earned a badge
      Week One Done
    • Week One Done
      maimutza earned a badge
      Week One Done
    • Week One Done
      abortretryfail earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      482
    2. 2
      +FloatingFatMan
      264
    3. 3
      snowy owl
      239
    4. 4
      ATLien_0
      229
    5. 5
      Edouard
      179
  • Tell a friend

    Love Neowin? Tell a friend!