ASP.NET操作ORACLE資料庫之模糊查詢

來源:互聯網
上載者:User

標籤: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資料庫之模糊查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.