存儲過程

--後臺代碼


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;


namespace _11複習
{
    public partial class Web1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataLoad();
            }
        }
        private void DataLoad()
        {
            string strcon = @"Data Source=PC-20130306VXLI;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=yqq";
            SqlConnection conn = new SqlConnection(strcon);
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            //cmd.CommandText = "Pro_GetNewsByTitle";
            //cmd.CommandType = CommandType.StoredProcedure;
            //cmd.Parameters.AddWithValue("@newstitle", "%" + txttitle.Text + "%");
            cmd.CommandText = "Pro_GetNewsByTitle";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@newstitle", "%" + txttitle.Text + "%");


            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            cmd.Dispose();
            conn.Dispose();


            #region 將數據拼接成
            StringBuilder sb1 = new StringBuilder();
            string newstitle = string.Empty;
            string newscontent = string.Empty;
            string createtime = string.Empty;
            sb1.Append("<table border=2>");
            foreach (DataRow row in dt.Rows)
            {
                sb1.Append("<tr>");
                newstitle = row["NewsTitle"].ToString();
                sb1.Append("<td>" + newstitle + "</td>");


                newscontent = row["NewsContent"].ToString();
                sb1.Append("<td>" + newscontent + "</td>");


                createtime = row["CreateTime"].ToString();
                sb1.Append("<td>" + createtime + "</td>");


                sb1.Append("</tr>");
            }
            sb1.Append("</table>");
            divResult.InnerHtml = sb1.ToString();
            #endregion




        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            DataLoad();
        }
    }
}

--前臺代碼

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Web1.aspx.cs" Inherits="_11複習.Web1" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:TextBox ID="txttitle" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="搜索" onclick="Button1_Click" />
    <div id="divResult" runat="server">
    </div>
    </div>
    </form>
</body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章