標籤:des style blog io color ar os 使用 sp
ASP.NET操作ORACLE資料庫之模糊查詢
一、ASP.NET MVC利用OracleHelper輔助類操作ORACLE資料庫
1 //串連Oracle資料庫的連接字串 2 string connectionString = @"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP) 3 (HOST=localhost) (PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=TestDB))); 4 User Id=developer; Password=developer"; 5 6 /// <summary> 7 /// 製作商品查詢頁面 條件:根據商品名稱模糊查詢 8 /// </summary> 9 /// <returns></returns>10 public ActionResult product(string ProName)11 {12 /*法一、直接使用like模糊查詢13 string sql = "select * from product where chvProName like ‘%‘ || upper(:ProName) || ‘%‘";14 */15 //法二、使用concat函數查詢16 string sql = "select * from product where chvProName like concat(concat(‘%‘, upper(:ProName)), ‘%‘)";17 18 ViewBag.name = ProName;19 //使用OracleHelper輔助類20 DataSet ds = OracleHelper.ExecuteDataset(connectionString, CommandType.Text, sql,21 new OracleParameter(":ProName", OracleType.NVarChar) { Value = ProName});22 return View(ds.Tables[0]);23 }
一、ASP.NET Web直接操作ORACLE資料庫
//串連Oracle資料庫的連接字串
string connectionString = @"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)
(HOST=localhost) (PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=TestDB)));
User Id=developer; Password=developer";
protected void Button1_Click(object sender, EventArgs e) { string proName = this.txtProName.Text.Trim(); /*法一、直接使用like模糊查詢 string sql = "select * from product where chvProName like ‘%‘|| upper(:proName) ||‘%‘"; */ //法二、使用concat函數查詢 string sql = "select * from product where chvProName like concat(concat(‘%‘, upper(:proName)), ‘%‘)"; OracleConnection connection = new OracleConnection(connectionString); OracleCommand cmd = new OracleCommand(sql, connection); cmd.Parameters.Add(new OracleParameter(":proName", proName) { OracleType = OracleType.NVarChar }); OracleDataAdapter adapter = new OracleDataAdapter(cmd); DataSet ds = new DataSet(); adapter.Fill(ds); this.GvPro.DataSource = ds.Tables[0].DefaultView; this.GvPro.DataBind(); }
說明:
在做該例子的時候,由於是剛接觸Oracle,所以很多語句和方法一直都停留在MSSQLServer裡面,所以剛開始做的時候寫模糊查詢like語句的時候居然把“||”寫成了“+”,還調試了好幾遍,後來被同學指出來了還被他笑了老半天!
呵呵,,關於Oracle中字元之間的連結我想估計這輩子我都忘不了了:
Oracle中字元之間的連結用"||"和函數CONCAT(),而非“+”;
ASP.NET操作ORACLE資料庫之模糊查詢