Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 832401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:21:26+00:00 2026-05-15T04:21:26+00:00

I am working on a c# application which connects to MySql server. There are

  • 0

I am working on a c# application which connects to MySql server. There are about 20 functions which will connect to database. This application will be deployed in 200 over machines. I am using the below code to connect to my database which is identical for all the functions. The problem is, i can some connections were not closed and still alive when deployed in 200 over machines.

Connection String :

<add key="Con_Admin" value="server=test-dbserver; database=test_admindb; uid=admin; password=1Password; Use Procedure Bodies=false;" />

Declaration of the connection string Globally in application [Global.cs] :

public static MySqlConnection myConn_Instructor = new MySqlConnection(ConfigurationSettings.AppSettings["Con_Admin"]);

Function to query database :

  public static DataSet CheckLogin_Instructor(string UserName, string Password)
        {
            DataSet dsValue = new DataSet();
            //MySqlConnection myConn = new MySqlConnection(ConfigurationSettings.AppSettings["Con_Admin"]);
            try
            {
                string Query = "SELECT accounts.str_nric AS Nric, accounts.str_password AS `Password`," +
                        " FROM accounts " +
                        " WHERE accounts.str_nric = '" + UserName + "' AND accounts.str_password = '" + Password + "\'";

                MySqlCommand cmd = new MySqlCommand(Query, Global.myConn_Instructor);
                MySqlDataAdapter da = new MySqlDataAdapter();
                if (Global.myConn_Instructor.State == ConnectionState.Closed)
                {
                    Global.myConn_Instructor.Open();
                }

                cmd.ExecuteScalar();
                da.SelectCommand = cmd;
                da.Fill(dsValue);
                Global.myConn_Instructor.Close();
            }
            catch (Exception ex)
            {
                Global.myConn_Instructor.Close();
                ExceptionHandler.writeToLogFile(System.Environment.NewLine + "Target  :  " + ex.TargetSite.ToString() + System.Environment.NewLine + "Message :  " + ex.Message.ToString() + System.Environment.NewLine + "Stack   :  " + ex.StackTrace.ToString());
            }

            return dsValue;
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:21:27+00:00Added an answer on May 15, 2026 at 4:21 am

    You should be using a unit-of-work approach using disposables. The MySqlConnection type is disposable, so should be cleanly wrapped in using blocks:

    using (MySqlConnection connection = CreateConnection("Con_Admin")) {
      // Do work here.
    }
    

    What this does, is it ensures that Dispose() is called on the connection object, which closes the connection. This is incredibly important.

    I’ve also noticed you’re using appSettings for your connection strings, there is actually a dedicated connectionStrings element in a config file:

    <connectionStrings>
      <add name="Con_Admin" connectionString="..." providerName="MySql.Data" />
    </connectionStrings>
    

    Which you can use to create instances of connections:

    public MySqlConnection CreateConnection(string name)
    {
      if (string.IsNullOrEmpty(name))
        throw new ArgumentException("Connection name must be provided", "name");
    
      string connection = ConfigurationManager.ConnectionStrings[name].ConnectionString;
      return new MySqlConnection(connection);
    }
    

    The last point of interest, you should try using parameterised queries, which protect against sql injection attacks:

    string query = "SELECT * FROM SomeTable Where SomeField = @field";
    
    using (MySqlCommand command = new MySqlCommand(query))
    {
      command.Parameters.AddWithValue("@field", "someFieldValue");
    }
    

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a web application(using PHP) which will use MySQL database in
I've got an application which does connect to a MySQL 5-Server via the ODBC-Driver.
I am working on an application which connects to the mail server using python
I'm working on a vb.net application connecting to mysql db which will have alot
I'm working with an application which connects to a database. I definitely want to
I am working on an android application for my company which connects to a
I'm currently developing an application which connects to a database using sqlalchemy. The idea
I'm working on application which has workflow like this: 1.parsing home page (using HttpURLConnection,
I'm working on an application where a client connects with a TCP connection which
I have written an application using Seam 2.2.1 & MySQL which is working. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.