使用数据集从文本框插入数据到数据库

时间:2013-11-20 08:45:34

标签: c# asp.net ado.net

我有一个带有文本框和按钮的页面,我在尝试使用数据库将文本插入sqldatabase,但它无法正常工作。我看到按钮点击时数据库中没有任何反映,并且没有抛出任何错误。请帮忙

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


public partial class CM : System.Web.UI.Page
{
    DataSet ds = new DataSet();
    SqlDataAdapter da;
    DataTable dt;
    SqlConnection con = new SqlConnection("server =consulting76\\SQLEXPRESS; database = msdb; Integrated Security=True");

    protected void Page_Load(object sender, EventArgs e)
    {
        da = new SqlDataAdapter("Select * from NOTESMAKER", con);
        da.Fill(ds, "NOTESMAKER");
        dt = ds.Tables["NOTESMAKER"];
    }

    protected void Button1_Click(object sender, EventArgs e)
    { 
        SqlCommand cmd = new SqlCommand("Insert into NOTESMAKER(NOTESMAKER) Values(@text1)", con);
        cmd.Parameters.Add(new SqlParameter("@text1", SqlDbType.NText)).Value = TextBox1.Text;
        da.InsertCommand = cmd;
        //cmd.ExecuteNonQuery();
    }

}

1 个答案:

答案 0 :(得分:0)