C#資料庫操作特殊字元單引號三種處理方式

來源:互聯網
上載者:User

方法一:逸出字元

使用單引號作為逸出字元,即連續使用兩個單引號。

 

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 stringSqlDataAdapter adapter = new SqlDataAdapter(cmdstr, constr);// Create the builder for the adapter to automatically generate the Command when neededSqlCommandBuilder builder = new SqlCommandBuilder(adapter);// Create and fill the DataSet using the WIRELESS_POLICE_TDataSet dataset = new DataSet();adapter.Fill(dataset, "WIRELESS_POLICE_T");// Get the WIRELESS_POLICE_T table from the datasetDataTable table = dataset.Tables["WIRELESS_POLICE_T"];// Indicate DataColumn WLPid is unique, This is required by the SqlCommandBuilder to update the WIRELESS_POLICE_T tabletable.Columns["WLPid"].Unique = true;// New row from the WIRELESS_POLICE_T tableDataRow 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 tableadapter.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為一個對象執行個體。

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.