2014-04-16 72 views
0

下面的代碼可以在Oracle的SqlPlus(使用Oracle 11.2.02.0g)中正常工作,但是當我通過C#代碼連接並通過ODBC連接時,我得到的消息告訴我有一個無效字符。Oracle vs Oracle ODBC

由於單引號在SQLplus中不起作用,因此我假定被ODBC認爲無效的字符是雙引號。我已經嘗試了大括號{'和括號'['但仍然得到相同的錯誤 - >錯誤[HY000] [Oracle] [ODBC] [Ora] ORA-00911:無效字符< -

任何幫助非常感激。我仍然不明白爲什麼SQL語句會因連接類型而被解釋爲不同。

CREATE USER "AD1\EGRYXU" IDENTIFIED EXTERNALLY; 

如果單獨運行,表明用戶名與另一個用戶或角色名衝突,則會出錯。它確實在數據庫中創建用戶。

C#代碼如下。

private void button1_Click(object sender, EventArgs e) 
     { 
      string happy = ""; 
      string sql1 = ""; 
      string sql2 = ""; 
      string sql3 = ""; 
      string sql4 = ""; 
      string column; 
      int rownum = -1; 
      bool frst = false; 
      string dirIni = "\\\\ramxtxss021-f01\\hou_common_013\\globaluser\\"; 
      string fileIni = "add_users.sql"; 
      string transIniFullFileName = Path.Combine(dirIni, fileIni); 
      System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection(); 

      num_users = (usrdetails.Count > 0); 
      if (regions && num_users) 
      { 
       using (StreamWriter sw = new StreamWriter(transIniFullFileName)) 
       { 
        for (int y = 0; y < usrdetails.Count; y++) 
        { 
         switch(usrdetails[y].add_del.ToUpper()) 
         { 
          case "A": 
            sql1 = "CREATE USER \"" + usrdetails[y].userID.ToUpper() + "\" IDENTIFIED EXTERNALLY;"; 
            sql2 = "GRANT EDMROLE TO \"" + usrdetails[y].userID.ToUpper() + "\";"; 
            sql3 = "INSERT INTO MD_SITE_USER VALUES(generate_key(5), (select user_id from MD_SITE_USER where user_name = '" + 
             usrdetails[y].group + "') , {" + usrdetails[y].userID.ToUpper() + "}, " + usrdetails[y].seclev + 
             ", '" + usrdetails[y].username.ToUpper() + "', 'U', '" + usrdetails[y].isext.ToUpper() + "', 'N');"; 
            sw.WriteLine(sql1); 
            sw.WriteLine(sql2); 
            sw.WriteLine(sql3); 
            break; 
          case "D": 
            sql2 = "DELETE MD_SITE_APP_ACTION_OWNER WHERE user_id in (SELECT user_id FROM MD_SITE_USER where user_name = ‘"+ usrdetails[y].userID + "’+ and user_or_group = ‘U’);"; 
            sql3 = "DELETE FROM MD_SITE_USER where user_name = ‘"+ usrdetails[y].userID + "’ and user_or_group = ‘U’;"; 
            sql4 = "DROP USER "+ usrdetails[y].userID + " FROM USERS;"; 
            sw.WriteLine(sql2); 
            sw.WriteLine(sql3); 
            sw.WriteLine(sql4); 
            break; 
          default: 
            MessageBox.Show("Add/Delete command argument not recognized for user\r\n" + usrdetails[y].userID + " \r\n Argument -> " + usrdetails[y].add_del); 
            break; 

         } 
        } 
       sw.Close(); 
       } 
       for (int x = 0; x < region.Count; x++) 
       { 
        OdbcCommand command = new OdbcCommand(); 
        conn.ConnectionString = "Driver={Oracle in OraClient11g_home1};" + 
              "Dbq=" + region[x].dbname + 
              ";Uid=" + region[x].username + ";Pwd=" + region[x].password + ";"; 
        try 
        { 
         string cmdTexts = File.ReadAllText(transIniFullFileName); 
         conn.Open(); 
         using (conn) 
         { 
          command.Connection = conn; 
          command.CommandText = cmdTexts; 
          command.ExecuteNonQuery(); 
          OdbcDataReader dr = command.ExecuteReader(); 
          Form6.dataGridView2.AutoGenerateColumns = false; 
          if (!frst) 
          { 
           for (int i = 0; i < dr.FieldCount; i++) 
           { 
            column = dr.GetName(i); 
            Form6.dataGridView2.Columns.Add("col" + i, column); 
            Form6.dataGridView2.Columns[i].FillWeight = 1; 
           } 
           frst = true; 
          } 
          rownum++; 
          dataGridView1.Rows.Add(); 
          dataGridView1.Rows[rownum].Cells[0].Value = "Results for Region -> " + Form5.region[x].dbname; 
          dataGridView1.Refresh(); 
          while (dr.Read()) 
          { 
           rownum++; 
           Form6.dataGridView2.Rows.Add(); 
           for (int i = 0; i < dr.FieldCount; i++) 
           { 
            column = dr.GetValue(i).ToString(); 
            Form6.dataGridView2.Rows[rownum].Cells[i].Value = column; 
           } 
          } 
          Form6.dataGridView2.Refresh(); 
          Form6.dataGridView2.Show(); 
          Form6.Show(); 
         } 
         conn.Close(); 
         Form6.dataGridView2.Refresh(); 
        } 
        catch (Exception ex) 
        { 
         MessageBox.Show("Error Message: " + ex.Message); 
        } 
       } 
      } 
      else 
      { 
       if (!regions) 
        happy = "Error - You have not selected any regions.\r\n"; 
       else 
        happy = "Regions are now selected.\r\n"; 
       if (!num_users) 
        happy = happy + "Error - You have not entered any users.\r\n"; 
       MessageBox.Show(happy); 
      } 
      File.Delete(transIniFullFileName); 
     } 
+0

您可能想向我們展示您的C#代碼... – mustaccio

+0

這裏有三個單獨的語句。從C#執行時,三者中的哪一個會導致錯誤? –

+0

據我所知第一和第二個陳述(第三個依賴於前兩個)。 C#代碼現在也包含在內。 – user3308131

回答

2

請勿使用「;」 (分號)。

ODBC或ODP中的命令文本應該是一個命令,例如,不是一組命令,因此 - 「;」不相關,並且是無效字符。


似乎您試圖運行一個腳本.. 如果這是你的意圖,它應該有一個「開始」和「結束」的代碼能夠運行來填充:

BEGIN 
INSERT...; 
DELETE ...; 
END; 

(參見http://www.intertech.com/Blog/executing-sql-scripts-with-oracle-odp/獲取更多信息)

最後一件事 - 如果你想運行一個「創建用戶」從一個匿名塊或程序中(或任何其他DDL),你需要與運行「立即執行「語法:

BEGIN 
execute immediate 'CREATE USER test IDENTIFIED EXTERNALLY'; 
END;