zoukankan      html  css  js  c++  java
  • C#数据库操作特殊字符单引号三种处理方式

    方法一:转义字符

    使用单引号作为转义字符,即连续使用两个单引号。

    select * from jq_jjjl where bt like '%女子''%'


    上述代码会匹配jq_jjjl表中所有bt字段包含 女子'的记录。(注意单引号)

    方法二:SqlDataAdapter

    string constr = "Server=" + DBConfig.DBAPP_IP + ";user id=" + DBConfig.DBAPP_USER + ";password=" + DBConfig.DBAPP_PASSWD + ";Database=" + DBConfig.DBAPP_DBNAME + ";Connect Timeout=30";
    string cmdstr = "SELECT * FROM WIRELESS_POLICE_T";
    
    // Create the adapter with the selectCommand txt and the connection string
    SqlDataAdapter adapter = new SqlDataAdapter(cmdstr, constr);
    
    // Create the builder for the adapter to automatically generate the Command when needed
    SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
    
    // Create and fill the DataSet using the WIRELESS_POLICE_T
    DataSet dataset = new DataSet();
    adapter.Fill(dataset, "WIRELESS_POLICE_T");
    
    // Get the WIRELESS_POLICE_T table from the dataset
    DataTable table = dataset.Tables["WIRELESS_POLICE_T"];
    
    // Indicate DataColumn WLPid is unique, This is required by the SqlCommandBuilder to update the WIRELESS_POLICE_T table
    table.Columns["WLPid"].Unique = true;
    
    // New row from the WIRELESS_POLICE_T table
    DataRow row = table.NewRow();
    
    // Update a column
    //row["xxx"] = xxx;
    // 你的赋值语句
    // Now update the WIRELESS_POLICE_T using the adapter
    // The OracleCommandBuilder will create the UpdateCommand for the adapter to update the WIRELESS_POLICE_T table
    adapter.Update(dataset, "WIRELESS_POLICE_T");
    


    方法三:构造SQL语句(类似java中的PreparedStatement)

                string constr = "Server=" + DBConfig.DBAPP_IP + ";user id=" + DBConfig.DBAPP_USER + ";password=" + DBConfig.DBAPP_PASSWD + ";Database=" + DBConfig.DBAPP_DBNAME + ";Connect Timeout=30";
                SqlConnection conn = new SqlConnection(constr);
    
                // 此处可能存在sql语句中含有单引号的问题
                /**
                string cmdstr = "update WIRELESS_PERSON_T set PersonName='"+person.getPersonName()
                    +"', PersonSex='"+person.getPersonSex()+"', YID='"+person.getYID()
                    +"', caseinfoid='"+person.getCaseinfoid()+"', Kind='"+person.getKind()
                    +"', caseremark='"+person.getCaseremark()+"', ArrivalKind='"+person.getArrivalKind()
                    +"' where personId="+person.getPersonId();
                 * */
    
                string cmdstr = "update WIRELESS_PERSON_T set PersonName=@PersonName, PersonSex='" + person.getPersonSex() 
                    + "', YID=@YID, caseinfoid='" + person.getCaseinfoid() + "', Kind='" + person.getKind()
                    + "', caseremark=@Caseremark, ArrivalKind='" + person.getArrivalKind() + "' where PersonId=" + person.getPersonId();
                Console.WriteLine(cmdstr);
                //SqlCommand command = new SqlCommand(cmdstr, conn);
                SqlCommand command = conn.CreateCommand();
                command.CommandText = cmdstr;
                command.Parameters.Add(new SqlParameter("PersonName", person.getPersonName()));
                command.Parameters.Add(new SqlParameter("YID", person.getYID()));
                command.Parameters.Add(new SqlParameter("Caseremark", person.getCaseremark()));
    
                try
                {
                    conn.Open();
                    command.ExecuteNonQuery();
                    Console.WriteLine("保存信息成功!");
                }
                catch (Exception e2)
                {
                    MessageBox.Show("保存出错!" + e2.Message);
                    return;
    
                }
                finally
                {
                    conn.Close();
                }

    上述代码中person为一个对象实例。


  • 相关阅读:
    链表和数组的区别在哪里 【微软面试100题 第七十八题】
    关于链表问题的面试题目 【微软面试100题 第七十七题】
    复杂链表的复制 【微软面试100题 第七十六题】
    二叉树两个结点的最低公共父结点 【微软面试100题 第七十五题】
    数组中超过出现次数一半的数字 【微软面试100题 第七十四题】
    对称字符串的最大长度 【微软面试100题 第七十三题】
    Singleton模式类 【微软面试100题 第七十二题】
    数值的整数次方 【微软面试100题 第七十一题】
    旋转数组中的最小元素 【微软面试100题 第六十九题】
    把数组排成最小的数 【微软面试100题 第六十八题】
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3030692.html
Copyright © 2011-2022 走看看