在变量中存储包含单个,&符号等字符的字符串

时间:2017-01-22 08:36:16

标签: string oracle plsql clob

我的程序将接收字符串(电子邮件正文消息)作为输入参数,该参数将分配给变量。它可以包含单引号和&符号等字符。如何处理这种情况并将字符串分配给clob变量。

int rows = 0;
using (MySqlConnection mySqlConnection = MySQL.iniHandle())
{
    using (MySqlCommand mySqlCommand = mySqlConnection.CreateCommand())
    {
        if (!MySQL.open(mySqlConnection))
        {
            return -1;
        }
        else
        {
            mySqlCommand.CommandText = "SELECT COUNT(*) FROM `users`";
            rows = Convert.ToInt32(mySqlCommand.ExecuteScalar());
            mySqlCommand.CommandText = "SELECT * FROM `users`";
            using (MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader())
            {
                if (mySqlDataReader.HasRows)
                {
                    try
                    {
                        while (mySqlDataReader.Read())
                        {
                            for (int i = 0; i<rows; i++)
                            {
                                int Id = (int)mySqlDataReader["Id"];
                                string Username = (string)mySqlDataReader["Username"];
                                string CPU = (string)mySqlDataReader["CPU"];
                                string LastIP = (string)mySqlDataReader["LastIP"];
                                string ProjectName = "";
                                string ProjectStatus = "";

                                using (MySqlConnection mySqlConnection1 = MySQL.iniHandle())
                                {
                                    if (!MySQL.open(mySqlConnection1))
                                    {
                                        return -1;
                                    }
                                    else
                                    {
                                        using (MySqlCommand mySqlCommand1 = mySqlConnection1.CreateCommand())
                                        {
                                            mySqlCommand1.CommandText = "SELECT COUNT(*) FROM `authing` WHERE UserId=@IDD";
                                            mySqlCommand1.Parameters.AddWithValue("@IDD", Id);
                                            int rows1 = Convert.ToInt32(mySqlCommand1.ExecuteScalar());
                                            mySqlCommand1.CommandText = "SELECT * FROM `authing` WHERE UserId=@ID";
                                            mySqlCommand1.Parameters.AddWithValue("@ID", Id);
                                            List<String> projects = new List<String>();

                                            using (MySqlDataReader mySqlDataReader1 = mySqlCommand1.ExecuteReader())
                                            {
                                                if (mySqlDataReader1.HasRows)
                                                {
                                                    try
                                                    {
                                                        while (mySqlDataReader1.Read())
                                                        {
                                                            for (int j = 0; j < rows1; j++)
                                                            {
                                                                int ProjectId = (int)mySqlDataReader1["ProjectId"];
                                                                ProjectStatus = getProjectStatus((int)mySqlDataReader1["Status"]);

                                                                using (MySqlConnection mySqlConnection2 = MySQL.iniHandle())
                                                                {
                                                                    if (!MySQL.open(mySqlConnection2))
                                                                    {
                                                                        return -1;
                                                                    }
                                                                    else
                                                                    {
                                                                        using (MySqlCommand mySqlCommand2 = mySqlConnection2.CreateCommand())
                                                                        {
                                                                            mySqlCommand2.CommandText = "SELECT * FROM `projects` WHERE Id=@ProjectID";
                                                                            mySqlCommand2.Parameters.AddWithValue("@ProjectID", ProjectId);

                                                                            using (MySqlDataReader mySqlDataReader2 = mySqlCommand2.ExecuteReader())
                                                                            {
                                                                                if (mySqlDataReader2.HasRows)
                                                                                {
                                                                                    try
                                                                                    {
                                                                                        while (mySqlDataReader2.Read())
                                                                                        {
                                                                                            ProjectName = (string)mySqlDataReader2["Name"];
                                                                                        }
                                                                                    }
                                                                                    catch (MySqlException excp)
                                                                                    {
                                                                                        return -2;
                                                                                    }
                                                                                }
                                                                            }
                                                                            projects.Add(ProjectName + " => " + ProjectStatus);
                                                                        }
                                                                    }
                                                                }
                                                            try
                                                            {
                                                                DataGridViewComboBoxCell dropDown = (DataGridViewComboBoxCell)userTable.Rows[i].Cells[2];
                                                                dropDown.DataSource = projects;
                                                                dropDown.ValueType = typeof(string);
                                                                this.userTable.Rows.Add(Username, CPU, dropDown, LastIP);
                                                            }
                                                            catch(Exception e)
                                                            {
                                                                this.logServerActivity(e.ToString());
                                                            }
                                                        }
                                                    }
                                                }
                                                catch (MySqlException excp)
                                                {
                                                    return -2;
                                                }
                                            }
                                        }
                                    }
                                }
                            }   
                        }
                    }
                }
                catch (MySqlException excp)
                {
                    return -2;
                }
            }
        }
    }
    }
mySqlConnection.Close();
}
return rows;
  

错误:

     

ORA-06550:第7行,第22栏:PLS-00103:遇到符号“S”   期待以下之一:

1 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:

SET SERVEROUTPUT ON
/* disable variable substitution */
SET DEFINE OFF 

DECLARE
email_body CLOB;
BEGIN
email_body:=to_clob(q'[Hel's M&ssage]'); /* Q operator to avoid issues with quotes */

    DBMS_OUTPUT.PUT_LINE(length(email_body));

EXCEPTION
    WHEN OTHERS THEN
    DBMS_OUTPUT.PUt_LINE(SQLCODE||' '||SQLERRM);
END;
/

在这里,我使用SET DEFINE OFF来阻止变量替换,并使用Oracle Q quote operator来处理带引号的字符串。