G
Guest
Hi, I'm doing an application using C# and I have this question:
I have a method called sqlQueryBD which receives a string sql query and
executes it against a database.
I also have a class called sqlCompat which has one property "sqlText" with
its getter and setter methods. The purpose of this class to translate the
receiving sql query into an "escaped" sql query, for instance:
insert into people values ('cod001','Carl
Johnson','\\computer\directory\file01.txt')
is translated into:
insert into people values ('cod001','Carl
Johnson','\\\\computer\\directory\\file01.txt')
The constructor receiving a string parameter is as follows:
public sqlCompat(string text)
{
this.text = sqlCompatSTR(text.Trim());
}
These are getter and setter methods for sqlText:
private string text;
public string sqlText
{
get
{
return text;
}
set
{
text = sqlCompatSTR(value.Trim());
}
}
where sqlCompatSTR is in charge of doing the translation.
In a form called frmQuerySFG in a button event I have this and it works
perfectly:
sqlQueryBD(new sqlCompat("select * from table").sqlText);
But if I do this it works exactly the same way:
//Declare a frmQuerySFG attribute (this is outside of button's event)
sqlCompat s_sql = new sqlCompat();
//This goes inside the button event
s_sql.sqlText = "select * from table";
sqlQueryBD(s_sql.sqlText);
Ok, which one is better between these two approaches?
The first approach is faster to code because it is only one line of code,
but I don't know if there are disadvantages in creating a new instance each
time I call that method.
The second approach doesn't create a new instance each time sqlQueryBD is
called but it involves more lines of code.
I've been using the first approach and I would like to know if it's better
to use the second approach. I always like to improve my code.
Thanks for your help.
Best Regards
I have a method called sqlQueryBD which receives a string sql query and
executes it against a database.
I also have a class called sqlCompat which has one property "sqlText" with
its getter and setter methods. The purpose of this class to translate the
receiving sql query into an "escaped" sql query, for instance:
insert into people values ('cod001','Carl
Johnson','\\computer\directory\file01.txt')
is translated into:
insert into people values ('cod001','Carl
Johnson','\\\\computer\\directory\\file01.txt')
The constructor receiving a string parameter is as follows:
public sqlCompat(string text)
{
this.text = sqlCompatSTR(text.Trim());
}
These are getter and setter methods for sqlText:
private string text;
public string sqlText
{
get
{
return text;
}
set
{
text = sqlCompatSTR(value.Trim());
}
}
where sqlCompatSTR is in charge of doing the translation.
In a form called frmQuerySFG in a button event I have this and it works
perfectly:
sqlQueryBD(new sqlCompat("select * from table").sqlText);
But if I do this it works exactly the same way:
//Declare a frmQuerySFG attribute (this is outside of button's event)
sqlCompat s_sql = new sqlCompat();
//This goes inside the button event
s_sql.sqlText = "select * from table";
sqlQueryBD(s_sql.sqlText);
Ok, which one is better between these two approaches?
The first approach is faster to code because it is only one line of code,
but I don't know if there are disadvantages in creating a new instance each
time I call that method.
The second approach doesn't create a new instance each time sqlQueryBD is
called but it involves more lines of code.
I've been using the first approach and I would like to know if it's better
to use the second approach. I always like to improve my code.
Thanks for your help.
Best Regards