Changing ConnectionString programmatically

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Have the following situation:

I have an old C++ application, which I have managed to compile with /clr to
make it access .net functionality.
For some of the new functionality I have decided to create a C# DLL, being a
very neat and handy language. I have used wizards to create the dataset,
extracting data to a grid from an SQLServer with stored procedures, all
generated from wizards and life is fine. Except:

The connection string that I have is of course "hard coded default", so that
I could get the data in in the first place. The real life connection string
that I want is something that I have read from another database in the main
application (C++), which I now would like the C# DLL to use as well. This
connection string also contains username and password set from a user login
(using SQLServer authorisation).

The C++ application, originally being unmanaged does not read an app.config
file. The connection string is therefore not accessible through
System.Configuration.Configuration.

And the MyDLL.Properties.Settings.Default.myConnectionString is readonly,
and might not change things anyway.

Anyone, please? Or do I have to rewrite the whole db code?

Ole
 
Hi Miha,

Using .net 2.0.

Ole



Miha Markic said:
Hi Ole,

Which .net version?

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Ole said:
Have the following situation:

I have an old C++ application, which I have managed to compile with /clr
to
make it access .net functionality.
For some of the new functionality I have decided to create a C# DLL, being
a
very neat and handy language. I have used wizards to create the dataset,
extracting data to a grid from an SQLServer with stored procedures, all
generated from wizards and life is fine. Except:

The connection string that I have is of course "hard coded default", so
that
I could get the data in in the first place. The real life connection
string
that I want is something that I have read from another database in the
main
application (C++), which I now would like the C# DLL to use as well. This
connection string also contains username and password set from a user
login
(using SQLServer authorisation).

The C++ application, originally being unmanaged does not read an
app.config
file. The connection string is therefore not accessible through
System.Configuration.Configuration.

And the MyDLL.Properties.Settings.Default.myConnectionString is readonly,
and might not change things anyway.

Anyone, please? Or do I have to rewrite the whole db code?

Ole
 
Then you might use application settings (check .net docs) to configure
connection string. It stores default values in configuration file (.config)
and the values are read on assembly initialization sort of.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Ole said:
Hi Miha,

Using .net 2.0.

Ole



Miha Markic said:
Hi Ole,

Which .net version?

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Ole said:
Have the following situation:

I have an old C++ application, which I have managed to compile with
/clr
to
make it access .net functionality.
For some of the new functionality I have decided to create a C# DLL,
being
a
very neat and handy language. I have used wizards to create the
dataset,
extracting data to a grid from an SQLServer with stored procedures, all
generated from wizards and life is fine. Except:

The connection string that I have is of course "hard coded default", so
that
I could get the data in in the first place. The real life connection
string
that I want is something that I have read from another database in the
main
application (C++), which I now would like the C# DLL to use as well.
This
connection string also contains username and password set from a user
login
(using SQLServer authorisation).

The C++ application, originally being unmanaged does not read an
app.config
file. The connection string is therefore not accessible through
System.Configuration.Configuration.

And the MyDLL.Properties.Settings.Default.myConnectionString is
readonly,
and might not change things anyway.

Anyone, please? Or do I have to rewrite the whole db code?

Ole
 
Except...
(citing myself..)So how do I get that to work?

In general the config business is a funny thing though... Why am I supposed
to store this in a config file that I do not want to read? I want the
connection string to come from somewhere else...

Ole





Miha Markic said:
Then you might use application settings (check .net docs) to configure
connection string. It stores default values in configuration file (.config)
and the values are read on assembly initialization sort of.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Ole said:
Hi Miha,

Using .net 2.0.

Ole



Miha Markic said:
Hi Ole,

Which .net version?

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Have the following situation:

I have an old C++ application, which I have managed to compile with
/clr
to
make it access .net functionality.
For some of the new functionality I have decided to create a C# DLL,
being
a
very neat and handy language. I have used wizards to create the
dataset,
extracting data to a grid from an SQLServer with stored procedures, all
generated from wizards and life is fine. Except:

The connection string that I have is of course "hard coded default", so
that
I could get the data in in the first place. The real life connection
string
that I want is something that I have read from another database in the
main
application (C++), which I now would like the C# DLL to use as well.
This
connection string also contains username and password set from a user
login
(using SQLServer authorisation).

The C++ application, originally being unmanaged does not read an
app.config
file. The connection string is therefore not accessible through
System.Configuration.Configuration.

And the MyDLL.Properties.Settings.Default.myConnectionString is
readonly,
and might not change things anyway.

Anyone, please? Or do I have to rewrite the whole db code?

Ole
 
Then you'll have to pass to the assembly (through a static property?)

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Ole said:
Except...
(citing myself..)So how do I get that to work?

In general the config business is a funny thing though... Why am I
supposed
to store this in a config file that I do not want to read? I want the
connection string to come from somewhere else...

Ole





Miha Markic said:
Then you might use application settings (check .net docs) to configure
connection string. It stores default values in configuration file
(.config)
and the values are read on assembly initialization sort of.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Ole said:
Hi Miha,

Using .net 2.0.

Ole



:

Hi Ole,

Which .net version?

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Have the following situation:

I have an old C++ application, which I have managed to compile with
/clr
to
make it access .net functionality.
For some of the new functionality I have decided to create a C# DLL,
being
a
very neat and handy language. I have used wizards to create the
dataset,
extracting data to a grid from an SQLServer with stored procedures,
all
generated from wizards and life is fine. Except:

The connection string that I have is of course "hard coded default",
so
that
I could get the data in in the first place. The real life connection
string
that I want is something that I have read from another database in
the
main
application (C++), which I now would like the C# DLL to use as well.
This
connection string also contains username and password set from a
user
login
(using SQLServer authorisation).

The C++ application, originally being unmanaged does not read an
app.config
file. The connection string is therefore not accessible through
System.Configuration.Configuration.

And the MyDLL.Properties.Settings.Default.myConnectionString is
readonly,
and might not change things anyway.

Anyone, please? Or do I have to rewrite the whole db code?

Ole
 
.... which is fine. I have already passed the connectionstring to the assembly.

But how can I change the hard-coded connectionstring in there, that gives
the schemas, the adapters etc. etc.?

In other words, must I write my own functions, throw away the schemas and
all that, just because I am not able to access the connectionstring
programmatically?

There must be a way!

Ole

P.S. Appreciating your suggestions!!



Miha Markic said:
Then you'll have to pass to the assembly (through a static property?)

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Ole said:
Except...
(citing myself..)
The C++ application, originally being unmanaged (Win32!) does not read
an
app.config
file. The connection string is therefore not accessible through
System.Configuration.Configuration.
So how do I get that to work?

In general the config business is a funny thing though... Why am I
supposed
to store this in a config file that I do not want to read? I want the
connection string to come from somewhere else...

Ole





Miha Markic said:
Then you might use application settings (check .net docs) to configure
connection string. It stores default values in configuration file
(.config)
and the values are read on assembly initialization sort of.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Hi Miha,

Using .net 2.0.

Ole



:

Hi Ole,

Which .net version?

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Have the following situation:

I have an old C++ application, which I have managed to compile with
/clr
to
make it access .net functionality.
For some of the new functionality I have decided to create a C# DLL,
being
a
very neat and handy language. I have used wizards to create the
dataset,
extracting data to a grid from an SQLServer with stored procedures,
all
generated from wizards and life is fine. Except:

The connection string that I have is of course "hard coded default",
so
that
I could get the data in in the first place. The real life connection
string
that I want is something that I have read from another database in
the
main
application (C++), which I now would like the C# DLL to use as well.
This
connection string also contains username and password set from a
user
login
(using SQLServer authorisation).

The C++ application, originally being unmanaged does not read an
app.config
file. The connection string is therefore not accessible through
System.Configuration.Configuration.

And the MyDLL.Properties.Settings.Default.myConnectionString is
readonly,
and might not change things anyway.

Anyone, please? Or do I have to rewrite the whole db code?

Ole
 
Back
Top