A C# question

  • Thread starter Thread starter Jim
  • Start date Start date
J

Jim

Hi,

Is there no way I can make this work?

if(!Globals.workonline)
{
mysql dbconn = new mysql();
}
else
{
mysqlonline dbconn = new mysqlonline();
}

// Check if the user exists
OdbcCommand rsc = new OdbcCommand("select password from login where
username='" + txtUsername.Text + "'", dbconn.conn);

It keeps complaining on the OdbcCommand line that dbconn does not exist.
Basically, I need to initialize the right DB connection based on user
input. I'm using the 1.0 .NET framework.

Any help would be much appreciated, I have a deadline and am a newbie to C#.

Thanks,
Jim
 
Hi Jim,

What about this?

OdbcCommand rsc;
if(!Globals.workonline)
{
mysql dbconn = new mysql();
rsc = new OdbcCommand("select password from login where username='"
+ txtUsername.Text + "'", dbconn.conn);}
else
{
mysqlonline dbconn = new mysqlonline();
rsc = new OdbcCommand("select password from login where username='"
+ txtUsername.Text + "'", dbconn.conn);
}

Happy coding!
Morten
 
Hi Morten,

Thank you very much. It worked.

However, I can't close the database connection once I'm done with it. Is
this okay? Will the garbage collection feature dispose of it once the
form closes?

Thanks,
Jim
 
Jim said:
Thank you very much. It worked.

However, I can't close the database connection once I'm done with it. Is
this okay? Will the garbage collection feature dispose of it once the
form closes?

Not immediately. It will eventually, but that's not really a good way
of working.

Do mysql and mysqlonline implement a common interface? If so, declare
dbconn as being the appropriate common type, but declare it before the
"if".
 
Back
Top