When oracle inserts string data, the string contains 'single quotes, oracle strings
When you use insert into (field1, field2. ..) values ('val1', 'val2'...), an error is returned if the value contains single quotes.
Solution: Check whether val1 and val2 contain single quotation marks. If single quotation marks are included, replace single quotation marks with two single quotation marks ''.
Organize fields and field values into a HashTable, and then abstract the function getSqlByHashTable () for organizing SQL statements ():
HashTable ht = new HashTable ();
Ht. add (field1, val1 );
Ht. add (field2, val2 );
Ht. add (field3, val3 );
...
Public string getSqlByHashTable (string tablename, HashTable ht ){
StringBuilder sb = new StringBuilder ();
Sb. append ("insert into" + tablename + "(");
StringBuilder fsb = new StringBuilder ();
StringBuilder vsb = new StringBuilder ();
Foreach (var key in ht. Keys)
{
Fsb. append (key + ",");
String value = ht [key];
If (value. trim () = "")
{
Value = "";
}
Else
{
If (value. Contains ("'"))
{
Value = value. replace ("'","''");
}
Value = "'" + value + "'";
}
Vsb. append (value + ",");
}
Sb. append (fsb. toString (). Substring (0, fsb. toString (). length-1 ));
Sb. append (") values (");
Sb. append (vsb. toString (). Substring (0, vsb. toString (). length-1 ));
Sb. append (")");
Return sb. toString ();
}