使用參數處理 Access 資料庫插入操作中的單引號
在使用 SQL 語句向 Access 資料庫插入資料時,如果文字包含單引號,可能會遇到問題。為了解決這個問題,建議使用參數。
新增圖書評分的原始代碼:
<code>[WebMethod] public void bookRatedAdd(string title, int rating, string review, string ISBN, string userName) { //... cmd.CommandText = @"INSERT INTO bookRated([title], [rating], [review], [frnISBN], [frnUserName])VALUES('" + title + "', '" + rating + "','" + review + "','" + ISBN + "', '" + userName + "')"; //... }</code>
要使用參數,請依照下列步驟操作:
<code>INSERT INTO bookRated([title], [rating], [review], [frnISBN], [frnUserName])VALUES(@title, @rating, @review, @isbn, @username)</code>
<code>[WebMethod] public void bookRatedAdd(string title, int rating, string review, string ISBN, string userName) { //... cmd.Parameters.AddRange(new OleDbParameter[] { new OleDbParameter("@title", title), new OleDbParameter("@rating", rating), //... }); //... }</code>
這種方法可確保在將資料插入 Access 資料庫時,文字中的單引號能夠正確處理。
以上是如何使用參數將帶有單引號的資料安全地插入到 Access 資料庫中?的詳細內容。更多資訊請關注PHP中文網其他相關文章!