-1

I decided to try out C# for the first time after using VB.net.

Out of curiousity, when I used VB.net I had :

Dim conn As OleDbConnection = New OleDbConnection("Provider=""****"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)

When trying to convert this format to C# I did the following:

string strAccessConn = "Provider=****;user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd

However, my strUserId, strDatabase and strPssWd where saved in my Module.vb for VB.net like so:

Module Module1
  Friend strDatabase As String = "****"
  Friend strUserID As String = "****"
  Friend strPssWd As String = "****"

End Module

How do I make the Module in C# (an example would be helpful) Thanks!

FYI: I was told C# Equivalent for VB 'module' was a duplicate.

However the formatting and process of their post isn't equivalent to mine. I am asking a module based on database connection.

4
  • 1
    Duplicated here: stackoverflow.com/questions/30870487/… Commented Jul 8, 2015 at 17:42
  • @Fruitbat I looked at your duplicate link and it doesn't look like what I have. May be asking the same question but results are not what I need. Sstan's answer was what I need based on my format. So if you downgraded my post then I don't know why. Commented Jul 8, 2015 at 18:11
  • Not me. I just put the link there because I thought it was what you needed. Commented Jul 8, 2015 at 18:28
  • @Fruitbat I see. I just got a "duplicate" alert at top with same link so that is why I asked. Thanks. Commented Jul 8, 2015 at 18:45

1 Answer 1

4

You can put constants in a public static class like this:

public static class MyConnectionStringConstants
{
    public const string strDatabase = "****";
    public const string strUserID = "****";
    public const string strPssWd = "****";
}

To use it, you will need to refer to the constants like this:

string strAccessConn = "Provider=****;user id=" + MyConnectionStringConstants.strUserID + ";data source=" + MyConnectionStringConstants.strDatabase + ";password=" + MyConnectionStringConstants.strPssWd

BTW, in C#, you concatenate strings using the + operator, not the & operator.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for clarification and example. The format of what you sent is exactly similar to my old version. I really appreciate it! I also wasn't fully aware yet that c# uses + so appreciate that information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.