来自浏览器的输入数据不存储在表中

时间:2018-01-31 16:40:17

标签: c# sql asp.net localdb

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Web.UI.WebControls.WebParts;
 using System.Web.UI.HtmlControls;
 using System.Data.SqlClient;
 public partial class _Default : System.Web.UI.Page
 {
   SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\DELL\Documents\reg.mdf;Integra
ted Security=True;Connect Timeout=90");
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)

{
    con.Open();
    SqlCommand cmd = new SqlCommand("insert into signup values" + "
(@id,@name)", con);
    cmd.Parameters.AddWithValue("@id", TextBox1.Text);
    cmd.Parameters.AddWithValue("@name", TextBox2.Text);
     }
}        

我使用了上面的代码。它没有错误,但是当我点击提交按钮时它不会在表格中保存数据。我正在使用visual studio 2015 C#并将数据存储在localhost中。请帮忙。

2 个答案:

答案 0 :(得分:0)

您需要调用ExecuteNonQuery

if(a==b) cout<<"a";
else cout<<"b";

答案 1 :(得分:0)

步骤-1

在cs文件中插入代码

protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conectionStr"].ConnectionString);
    SqlCommand cmd = new SqlCommand("sp_insert_signup", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add(new SqlParameter("@id", SqlDbType.Int)).Value = Convert.ToInt32(TextBox1.Text);
    cmd.Parameters.Add(new SqlParameter("@name", SqlDbType.VarChar, 50)).Value = TextBox2.Text;
    conn.Open();
    try
    {
        int isSave = cmd.ExecuteNonQuery();
        if (isSave > 0)
        {
            Response.Write("data save successfully");

        }
        else
        {
            Response.Write("data save operation failed");
        }
    }
    catch (Exception ex)
    {

        Response.Write(ex.Message.ToString());
    }
    finally
    {
        conn.Close();
        cmd.Dispose();
    }

}