Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
C#
public DBCon()
{
   ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DataCon"].ConnectionString;
}

private void connect()
{
   try
   {
      con = new SqlConnection(ConnectionString);
      if (con.State == ConnectionState.Open)
         con.Close();
      con.Open();
   }
   catch { }
}

public bool ExecuteNonQuery(String sql)
{
   bool Check = false;
   try
   {
      cmd = new SqlCommand();
      cmd.CommandText = sql;
      cmd.CommandType = CommandType.Text;
      connect();
      cmd.Connection = con;
      if (cmd.ExecuteNonQuery() > 0)
         Check = true;
   }
   catch
   {
      Check = false;
   }
   finally
   {
      con.Close();
      cmd.Dispose();
      cmd = null;
   }
   return Check;
}

connectionString="Data Source=amit-pc\\sqlexpress;Initial Catalog=KasturiNaturals;Persist Security Info=True;User ID=sa;Password=123456;"

when i run this code i am getting this error.

ServerVersion = 'con.ServerVersion' threw an exception of type 'System.InvalidOperationException'


What I have tried:

i am trying to create methods to simplify my code to access database.
Posted
Updated 4-Apr-19 15:07pm
v3
Comments
RedDk 4-Apr-19 14:33pm    
Why do you con.Close() then con.Open() after you check if con.State is (==) ConnectionState.open? Wouldn't it be safe if ConnectionState.Closed was the condition then just con.Open() the result of the "try"?
Richard Deeming 4-Apr-19 15:07pm    
That class is going to force you to write code which is vulnerable to SQL Injection. You need to provide a way to pass parameters to your queries, without stuffing the values into the query text.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]

1 solution

Your code doesn't make sense. There are a few try-catch block in your code which are unnecessary and you don't have to manually close and dispose the connection. Just use the Using statement to automatically closes and disposes object that eat resources such as SqlConnection and SqlCommand. If you are trying to create a method that executes a query command then you can simply do something like this:

C#
private static void ExecuteCommand(string sqlString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        using(SqlCommand command = new SqlCommand(sqlString, connection))
        {
              command.Connection.Open();
              command.ExecuteNonQuery();
        }
    }
}


You can just then simply call the ExecuteCommand method by passing your sql statement and connectionstring:

C#
ExecuteCommand("YourSqlStatement","YourConnectionString");



Of course, you can still refactor the code to call your ConnectionString internally so you don't have to pass the connection string value everytime you call the ExcuteCommand method.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900