留言板,添加新记录

//张也留言板,添加留言
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;

 

protected void Page_Load(object sender, EventArgs e)
    {

        Label2.Text = Profile.UserName;
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "" || TextBox1.Text == "")
        {
            Label1.Text = "<b>操作提示:</b><br />";
            Label1.Text += "标题和内容不能为空";
            return;
        }
        string insertSQL;
        insertSQL = "INSERT INTO Gbook(Gbook_Title,Gbook_Content,Gbook_User) VALUES (@Gbook_Title,@Gbook_Content,@Gbook_User)";
     

        string connString;
        connString = WebConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
        SqlConnection con = new SqlConnection(connString);
        SqlCommand cmd = new SqlCommand(insertSQL, con);

 

        cmd.Parameters.AddWithValue("@Gbook_Title", TextBox1.Text);
        cmd.Parameters.AddWithValue("@Gbook_Content", TextBox2.Text);
        cmd.Parameters.AddWithValue("@Gbook_User", Profile.UserName);

        int added = 0;

        try
        {
            con.Open();
            added = cmd.ExecuteNonQuery();
            Label1.Text = "<b>操作提示:</b><br />";
            Label1.Text += added.ToString() + "条记录被添加成功。";
            Label1.Text += con.ServerVersion;
        }
        catch (Exception err)
        {
            Label1.Text = "<b>操作提示:</b><br />";
            Label1.Text += err.Message;
        }
        finally
        {
            con.Close();
            Label1.Text += "<b>操作提示:</b><br />";
            Label1.Text += con.State.ToString();

        }
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        Response.Redirect("Gbook_index.aspx");
    }

posted @ 2010-08-19 12:36  midisong  阅读(148)  评论(0编辑  收藏  举报