I want to create a general function in c# for sql requests at my oracle db.
I want to save all columns to strings. I tried to use the reader.GetString()
method but an exception is thrown, saying it is unconvertable...
How can I make a function which does that ?
string [] request (OracleConnection con, String Table, String condition, String columns, String sort){ //The magic code return "A string-array with all information"; }
Can you use the Item property on DBReader? Also, this method is a serious security risk -- using a condition string instead of params invites sql injection.
public string[] Request(IDBConnection conn, string table, string condition, string columns, string sort)
{
List<string> output = new List<string>();
string[] cols = columns.Split(',');
string sql = string.Format("select * from {0} etc", table);
using (IDBCommand cmd = new OracleCommand(conn, sql))
{
conn.Open();
IDBReader reader = cmd.ExecuteReader();
while (reader.Read())
{
foreach (string col in cols)
{
object field = reader.Item[col];
output.Add(field.ToString());
}
}
}
return output.ToArray();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.