我正在尝试使用c#从oracle数据库获取数据。我使用System.Data.OracleClient.dll,但由于这已经过时,我决定将其更改为System.Data.Oledb。 我的联系:
connection = new OleDbConnection();
connection.ConnectionString = string.Format("Provider=OraOLEDB.Oracle;Data Source={0};User ID={1};Password=23};", source, user, password);
以下是获取数据的方法:
public ContactInfo GetInfo(string telnr)
{
connection.Open();
Console.WriteLine("OracleState: {0}", connection.State);
OleDbCommand command = connection.CreateCommand();
string sql = @"Select t1.bed_bedrijfsnr
, t1.pers_persoonsnr
, REPLACE(REPLACE(REPLACE(REPLACE(t1.telefoonnr, ' ', ''), '-', ''), '(', ''), ')', '') as telnr
, REPLACE(REPLACE(REPLACE(REPLACE(t1.gsmnr, ' ', ''), '-', ''), '(', ''), ')', '') as gsmnr
, t1.e_mail
, t2.synoniem
, t2.sales_coordinator
From table1 t1
, table2 t2
Where t1.bed_bedrijfsnr = t2.bedrijfsnr
And (REPLACE(REPLACE(REPLACE(REPLACE(t1.telefoonnr, ' ', ''), '-', ''), '(', ''), ')', '') = :tel Or REPLACE(REPLACE(REPLACE(REPLACE(t1.gsmnr, ' ', ''), '-', ''), '(', ''), ')', '') = :tel);";
command.CommandText = sql;
command.Parameters.Add(new OleDbParameter(":tel", telnr));
ContactInfo c = null;
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
//readdata
}
reader.Close();
connection.Close();
Console.WriteLine("Oracle State: {0}", connection.State);
return c;
}
当我将sql语句更改为"从table1"中选择*时它有效,但有了这个声明,我的视觉工作室说' OledbException row-00001无法分配内存' on' OleDbDataReader reader = command.ExecuteReader();'
答案 0 :(得分:0)
我不确定为什么会产生这个特定的错误,但是当你使用Parameters.Add时,它似乎并没有替代多个参数实例。
因为您使用":tel"在SQL语句中需要两次使用Parameters.Add。如果你重复使用相同的参数名称似乎工作正常,但无论如何我都重命名。
string sql = @"Select t1.bed_bedrijfsnr
, t1.pers_persoonsnr
, REPLACE(REPLACE(REPLACE(REPLACE(t1.telefoonnr, ' ', ''), '-', ''), '(', ''), ')', '') as telnr
, REPLACE(REPLACE(REPLACE(REPLACE(t1.gsmnr, ' ', ''), '-', ''), '(', ''), ')', '') as gsmnr
, t1.e_mail
, t2.synoniem
, t2.sales_coordinator
From table1 t1
, table2 t2
Where t1.bed_bedrijfsnr = t2.bedrijfsnr
And (REPLACE(REPLACE(REPLACE(REPLACE(t1.telefoonnr, ' ', ''), '-', ''), '(', ''), ')', '') = :tel1 Or REPLACE(REPLACE(REPLACE(REPLACE(t1.gsmnr, ' ', ''), '-', ''), '(', ''), ')', '') = :tel2);";
command.CommandText = sql;
command.Parameters.Add(new OleDbParameter(":tel1", telnr));
command.Parameters.Add(new OleDbParameter(":tel2", telnr));