Oracle PreparedStatement /参数错误

时间:2018-09-27 12:13:51

标签: c# oracle

使用此代码,我可以从Oracle数据库中获取数据。

 public static ArrayList CheckIfPrinterExists()
        {
            string printerName = @"PRINTER1234";
            ArrayList colValues = new ArrayList();
            try
            {

                string constr = @"DATA SOURCE=someSource;PERSIST SECURITY INFO=True;USER ID=root;password=root";
                OracleConnection con = new OracleConnection(constr);
                con.Open();
                OracleCommand cmd = con.CreateCommand();
                cmd.CommandText = @"select * from print_spooler where spoolername = 'Printer1234'";
                OracleDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    colValues.Add(reader["id"] + ";" + reader["id2"] + ";" + reader["id3"]);
                }
                con.Dispose();
                reader.Dispose();
                return colValues;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error : {0}", ex);
                return null;
            }
        }

我尝试使用不起作用的预备语句修改代码。我在这里做错了什么?

public static ArrayList CheckIfPrinterExists()
        {
            string printerName = @"printer1234";
            ArrayList colValues = new ArrayList();
            try
            {

                string constr = @"DATA SOURCE=someSource;PERSIST SECURITY INFO=True;USER ID=root;password=root";
                OracleConnection con = new OracleConnection(constr);
                con.Open();
                OracleCommand cmd = con.CreateCommand();
                cmd.CommandText = @"select * from print_spooler where spoolername = @ParamPrinterName";
                cmd.Parameters.Add(new OracleParameter("@ParamPrinterName", printerName));
                OracleDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    colValues.Add(reader["id"] + ";" + reader["id2"] + ";" + reader["id3"]);
                }
                con.Dispose();
                reader.Dispose();
                return colValues;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error : {0}", ex);
                return null;
            }
        }

以下是有关更改的更好视图(如果我没错,由于SQL注入,第二个无法工作的optin更好使用)。

enter image description here

0 个答案:

没有答案