Web.config
-----------------------------------------------------------------------------------------------------------------
<connectionStrings>
<!--連線字串--> <add name="ConnectionString1" connectionString="Data Source=mycomputer; Initial Catalog=AdventureWorksLT2008;Persist Security Info=True; User ID=xxxx;Password=xxxxxxxx" providerName="System.Data.SqlClient"/> </connectionStrings>-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
<body> <body> <div> <telerik:RadXmlHttpPanel ID = "RadXmlHttpPanel1" runat="server"> <%-- TextBox 用來查詢該Table內資料 --%> <telerik:RadTextBox ID = "tbFirstName" runat="server" Skin="MetroTouch"></telerik:RadTextBox> <%-- Btn --%> <telerik:RadButton ID = "btnSearch" runat="server" Text="Search" Skin="MetroTouch" OnClick="btnSearch_Click"></telerik:RadButton> </telerik:RadXmlHttpPanel> <%-- GridView--%> <telerik:RadGrid ID = "RadGrid1" runat="server" AllowPaging="True" Skin="Glow" AllowSorting="True" Culture="zh-TW" GroupPanelPosition="Top"></telerik:RadGrid> </div> <body>-----------------------------------------------------------------------------------------------------------------
WebForm1.aspx.cs
-----------------------------------------------------------------------------------------------------------------
protected void Page_Load(object sender, EventArgs e) protected void Page_Load(object sender, EventArgs e) { //Fumction DataBind_GV(); } public void DataBind_GV() { DBConnect dbc = new DBConnect(); //New Class DataTable dt = new DataTable(); //New DataTable dbc.DbSelect(dt); this.RadGrid1.DataSource = dt; //指定DataTable給DataSource this.RadGrid1.DataBind(); //綁訂資料 } //查詢 protected void btnSearch_Click(object sender, EventArgs e) { string FirstName = tbFirstName.Text; DBConnect dbc = new DBConnect(); DataTable dse = new DataTable(); //記得拿DataTable來裝資料 dse = dbc.SearchDB(FirstName ); this.RadGrid1.DataSource = dse; this.RadGrid1.DataBind(); } ----------------------------------------------------------------------------------------------------------------- DBConnect.cs-----------------------------------------------------------------------------------------------------------------
public class DBConnect public class DBConnect { public string _conn = ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString; public DataTable DbSelect(DataTable ds) { string selectDB = "SELECT * FROM SalesLT.Customer"; SqlConnection db = new SqlConnection(_conn); SqlCommand cmd = new SqlCommand(selectDB, db); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); return (ds); } //存放Sql語法的地方 public DataTable SearchDB(string FirstName) { string searchCmd = "SELECT * FROM SalesLT.Customer WHERE FirstName like @FirstName"; //string searchCmd = "SELECT * FROM SalesLT.Customer"; SqlCommand cmd = new SqlCommand(searchCmd); // cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar); //cmd.Parameters["@FirstName"].Value = "%" + FirstName + "%"; cmd.Parameters.AddWithValue("@FirstName", "%" + FirstName + "%"); return QueryToDataTable(_conn, cmd); } //執行語法並傳遞DataTable public DataTable QueryToDataTable(string connstr, SqlCommand cmd) { DataTable dt = new DataTable(); SqlConnection conn = new SqlConnection(connstr); conn.Open(); cmd.Connection = conn; SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt); conn.Close(); return dt; }-----------------------------------------------------------------------------------------------------------------