:: problem in use UPDATE statement ::
-
I want to do something like below(in ASP.NET & C#): string strupdate = "UPDATE table SET field = field + intvariable WHERE otherfield = yes"; but i don't know how i can do (add a variable value to a numerical field in a ACCESS database). Please tell me the right string that i can use.
-
I want to do something like below(in ASP.NET & C#): string strupdate = "UPDATE table SET field = field + intvariable WHERE otherfield = yes"; but i don't know how i can do (add a variable value to a numerical field in a ACCESS database). Please tell me the right string that i can use.
I don't understand your question. Are you asking how to add (or concatenate) two values, or how to execute the UPDATE statement? As far as adding or concatenating values, what you typed works fine so long as the
field
andintvariable
are of the same type or are convertable to the same type. To execute the statement for an Access database, look at theSystem.Data.OleDb
namespace, specificallyOleDbConnection
,OleDbCommand
, andOleDbParameter
. Using parameters in your statement makes it easy to execute a statement multiple times by only changing the values of parameters and executing the statement, and saves you from having to do string concatenation (very inefficient if you just use+
) and proper quoting and escaping.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
I want to do something like below(in ASP.NET & C#): string strupdate = "UPDATE table SET field = field + intvariable WHERE otherfield = yes"; but i don't know how i can do (add a variable value to a numerical field in a ACCESS database). Please tell me the right string that i can use.
Nazila I thought I might give you a different twist on your question. I write classes that overloaded the toString()'s (if I am being lazy I make them methods) so that I can always get my variables back with the correctly. Example public class AccessDb { //in my data object public int pInt(int intIn) // This is the lazy way {// this is only an example I would use inherited base classes and // overloading the toString it is so much cleaner. return "'" + intIn.toString() + "'" } } // then later in your Business object code AccessDB db = new AccessDb(); db.Update("update table set field=" + db.pInt(intvariable)+ "where otherfield=yes"); I started doing this so I don't get caught recoding everytime the database gets switched from Access to MySql or something else happens. I really like insulating the data objects from the business objects and this is a big help in making generic interfaces. My applications can switch database backends by setting preferences which makes my life much better. It is very important to read up on your database access in .Net and get your design down solid first you will need this when you are setting up the data classes. There are a couple of really cool articles on how to implement this. Let me know what you think of the idea. Kevin