2015-09-01 94 views
1

我想從使用c#的oracle數據庫中獲取數據。我正在使用System.Data.OracleClient.dll,但由於這已經過時,我決定將其更改爲System.Data.Oledb。 我的連接:OledbException row-00001無法分配內存

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語句「從表1中選擇*」它的工作原理,但這種說法我Visual Studio的說'OledbException row-00001 can not allocate memory'on'OleDbDataReader reader = command.ExecuteReader();'

回答

0

我不知道爲什麼它會生成特定的錯誤,但是當您使用Parameters.Add時,它不會替代多個參數實例。

由於您在SQL語句中使用「:tel」兩次,所以需要使用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));