Click here to Skip to main content
15,911,132 members
Home / Discussions / C#
   

C#

 
GeneralRe: DLL or Component for RFID KITS Pin
Tyler4510-Nov-05 13:46
Tyler4510-Nov-05 13:46 
QuestionThrad problem Pin
hg270526-Oct-05 1:04
hg270526-Oct-05 1:04 
AnswerRe: Thrad problem Pin
S. Senthil Kumar26-Oct-05 5:27
S. Senthil Kumar26-Oct-05 5:27 
GeneralRe: Thrad problem Pin
hg270527-Oct-05 20:13
hg270527-Oct-05 20:13 
GeneralRe: Thrad problem Pin
S. Senthil Kumar27-Oct-05 21:11
S. Senthil Kumar27-Oct-05 21:11 
QuestionSlow Query on 2nd query of the same command Pin
AesopTurtle26-Oct-05 0:06
AesopTurtle26-Oct-05 0:06 
AnswerRe: Slow Query on 2nd query of the same command Pin
leppie26-Oct-05 4:12
leppie26-Oct-05 4:12 
AnswerRe: Slow Query on 2nd query of the same command Pin
Rob Graham26-Oct-05 6:20
Rob Graham26-Oct-05 6:20 
You are creating a new connection object each call to the function, and you're not closing it when done (garbage collection will eventually do so, but this is not good practice.
try this:
public class MyClass
{
    private SqlConnection conn = new SqlConnection(this.SQLExpressConnStr);
    public string SQL2KConnStr = "..(my connection string)..";
    public object ExecuteSql(string sql)
    {

        if (conn.State != ConnectionState.Open)
        {
            conn.Open();
        }
        switch(sql.Trim().Substring(0,6).ToLower())
        {
            case "select":
                try
                {
                    SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
                    ds = new DataSet();
                                            adapter.Fill(ds, "QueriedTab");
                }
                finally
                {
                    conn.Close();
                }
                return ds.Tables["QueriedTab"]; //Return a data table.
                break;
            default:
                SqlCommand cmd = new SqlCommand();
                //cmd.CommandType = CommandType.Text;
                cmd.Connection = conn;
                cmd.CommandText = sql;
                long rtn = 0;
                try
                {
                    rtn = cmd.ExecuteNonQuery(); //Return a number of affected rows.
                }
                finally
                {
                    conn.Close();
                }
                return rtn;
                break;
        }
    }
}


Only create the connection once, open and close it with each use.
You might want to add catch(SqlException e)
blocks before each finally block to handle errors.
probably want to move the declaration of the dataset to class or function scope (out of the try block)



Absolute faith corrupts as absolutely as absolute power
Eric Hoffer

All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke

GeneralRe: Slow Query on 2nd query of the same command Pin
AesopTurtle26-Oct-05 15:18
AesopTurtle26-Oct-05 15:18 
QuestionPlease help - How to un-install a Windows Service? Pin
Tigger9925-Oct-05 23:53
Tigger9925-Oct-05 23:53 
AnswerRe: Please help - How to un-install a Windows Service? Pin
seee sharp26-Oct-05 0:15
seee sharp26-Oct-05 0:15 
Questionhow do you create a dbf file? Pin
Anonymous25-Oct-05 23:09
Anonymous25-Oct-05 23:09 
QuestionList box Pin
User 58385225-Oct-05 20:24
User 58385225-Oct-05 20:24 
AnswerRe: List box Pin
User 58385225-Oct-05 20:34
User 58385225-Oct-05 20:34 
GeneralRe: List box Pin
technomanceraus25-Oct-05 21:08
technomanceraus25-Oct-05 21:08 
GeneralRe: List box Pin
User 58385225-Oct-05 21:14
User 58385225-Oct-05 21:14 
QuestionDataReader Pin
Brendan Vogt25-Oct-05 20:16
Brendan Vogt25-Oct-05 20:16 
AnswerRe: DataReader Pin
leppie25-Oct-05 22:03
leppie25-Oct-05 22:03 
QuestionRe: DataReader Pin
Brendan Vogt25-Oct-05 22:33
Brendan Vogt25-Oct-05 22:33 
AnswerRe: DataReader Pin
Colin Angus Mackay25-Oct-05 22:42
Colin Angus Mackay25-Oct-05 22:42 
QuestionEditing Pin
momoo25-Oct-05 19:45
momoo25-Oct-05 19:45 
AnswerRe: Editing Pin
Christian Graus25-Oct-05 20:07
protectorChristian Graus25-Oct-05 20:07 
QuestionCollections Pin
seee sharp25-Oct-05 19:05
seee sharp25-Oct-05 19:05 
AnswerRe: Collections Pin
Christian Graus25-Oct-05 19:23
protectorChristian Graus25-Oct-05 19:23 
GeneralRe: Collections Pin
seee sharp25-Oct-05 21:06
seee sharp25-Oct-05 21:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.