Last active
May 24, 2016 12:09
-
-
Save s0ren/5b4c69d59511d15d5e2613eccf1208f7 to your computer and use it in GitHub Desktop.
Simpelt, men dynamisk sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.UI; | |
using System.Web.UI.WebControls; | |
public partial class Seach : System.Web.UI.Page | |
{ | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
} | |
protected void ButtonSearch_Click(object sender, EventArgs e) | |
{ | |
string sql = "select * from stol inner join type on stol.fk_type = type.Id"; | |
string searchStr = TextBoxSearch.Text; | |
string[] words = searchStr.Split(' '); | |
string sqlFilters = ""; | |
foreach (string word in words) | |
{ | |
if(sqlFilters.Length > 0) | |
{ | |
sqlFilters += " OR"; | |
} | |
sqlFilters += " stol.titel LIKE '%" + word + "%'"; | |
sqlFilters += " OR stol.designer LIKE '%" + word + "%'"; | |
} | |
if(sqlFilters.Length > 0) | |
{ | |
sql += " WHERE " + sqlFilters; | |
} | |
SqlDataSource1.SelectCommand = sql; | |
GridView1.DataBind(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.UI; | |
using System.Web.UI.WebControls; | |
public partial class search2 : System.Web.UI.Page | |
{ | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
} | |
protected void ButtonSearch_Click(object sender, EventArgs e) | |
{ | |
string sql = "Select * from stol inner join type on stol.fk_type = type.Id"; | |
sql += " WHERE stol.titel LIKE '%" + TextBoxSearch.Text + "%'"; | |
SqlDataSource1.SelectCommand = sql; | |
GridView1.DataBind(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment