[英]Get all data from an Oracle DB request
I want to create a general function in c# for sql requests at my oracle db. 我想在C#中为我的Oracle数据库中的sql请求创建一个常规函数。
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... 我试图使用
reader.GetString()
方法,但是抛出了一个异常,说它是不可转换的。
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? 您可以在DBReader上使用Item属性吗? Also, this method is a serious security risk -- using a condition string instead of params invites sql injection.
而且,此方法存在严重的安全风险-使用条件字符串而不是params会邀请sql注入。
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.