ASP最简单的留言板制作【新手】 - Danx

时间:2024-03-05 17:04:59

ASP最简单的留言板制作【新手】

在设计界面制作Txtbox,Button,GridView
建立数据库,命名随意,一列,varchar 字节随意,一个字2字节,20表示不能超过10字

cs
protected void Page_Load(object sender, EventArgs e)//网页加载触发
{
    SqlConnection con = DB.conn();//引用DB类里面的方法connew()
    con.Open();
    string sqlstr = "select * from liuyan";//新建查询语句
    SqlDataAdapter sda = new SqlDataAdapter(sqlstr, con);
    DataSet ds = new DataSet();//新建数据集合
    sda.Fill(ds);//往数据库集合填充数据
    this.GridView1.DataSource = ds;
    this.GridView1.DataBind();
    con.Close();
}
      
protected void Button1_Click(object sender, EventArgs e)//单击按钮触发
{
    try
    {
        SqlConnection con = DB.conn();
        con.Open();
        string strsql = "insert into liuyan values(\'" + this.TextBox1.Text + "\')";
        SqlCommand cmd = new SqlCommand(strsql, con);
        cmd.ExecuteNonQuery();
        con.Close();
        Response.Redirect("ly.aspx");
    }
    catch (Exception ex)
    {
        Response.Write("<script>alert(\'留言失败!连接数据库失败 或 留言字数超过10字。\');</script>");
    }
          
}
using
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

示例:http://lvthms.qcaiyun.cn/ly.aspx