当前位置:编程学习 > C#/ASP.NET >>

html表单怎么实现点击提交按钮提交到sql数据库

html表单怎么实现点击提交按钮提交到sql数据库, 尽量写得详细一点,我是新手,急求急求,万分感谢

比如在Default.aspx中写html表单,要输入学号,姓名,出生日期,点击提交,提交到数据库中的xs表里,
还有这段代码是要写在Default.aspx页中,还是要写在后台.cs页中

表单我写好了,就是不会提交到数据库的代码

--------------------编程问答-------------------- html 表单中加 runat="server" 可以在后台直接使用html表单的id --------------------编程问答--------------------
引用 1 楼  的回复:
html 表单中加 runat="server" 可以在后台直接使用html表单的id

能说的再详细点吗,我刚学好多不太懂,“可以在后台直接使用html表单的id”
我是想把信息存数据库中,我应该在哪里或者哪个方法里写存到数据库的代码,而且存到数据库中的代码我也不太会,帮帮我吧 --------------------编程问答--------------------
//create table xs(studentId varchar(25) primary key,name nvarchar(20),birth datetime)

            //学号
            string studentId = Request.Form["studentId"];
            //姓名
            string name = Request.Form["name"];
            //出生日期
            DateTime birth = DateTime.Parse(Request.Form["birth"]);

            string dbConnStr = "你的数据库链接字符";
            SqlConnection dbConn = new SqlConnection(dbConnStr);

            SqlCommand dbCmd = conn.CreateCommand();

            dbCmd.CommandText = "insert into xs(studentId,name,birth) values(@studentId,@name,@birth);";


            dbCmd.Parameters.AddWithValue("studentId", studentId);
            dbCmd.Parameters.AddWithValue("name", name);
            dbCmd.Parameters.AddWithValue("birth", birth);
            dbCmd.ExecuteNonQuery();
--------------------编程问答--------------------
引用 3 楼  的回复:
C# code
//create table xs(studentId varchar(25) primary key,name nvarchar(20),birth datetime)

            //学号
            string studentId = Request.Form["studentId"];
            //姓名
           ……

请问这段代码要放在哪里 --------------------编程问答--------------------
引用 4 楼  的回复:
引用 3 楼 的回复:

C# code
//create table xs(studentId varchar(25) primary key,name nvarchar(20),birth datetime)

//学号
string studentId = Request.Form["studentId"];
//姓名
……

请问这段代码要放在哪里

.cs  文件中、、 --------------------编程问答-------------------- 你的 提交事件里面
SqlConnection sqlCon = new SqlConnection();
                sqlCon.ConnectionString = "Data Source=.;User Id=sa;Password=123456;Initial Catalog=mytest";
                sqlCon.Open();

                SqlCommand sqlCmd = sqlCon.CreateCommand();

                string strSQL = "insert into 表名称(用户名字段名称,密码字段名称)values('" + textbox1.text+ "','" + textbox2.text+ "')";

                sqlCmd.CommandText = strSQL;
                sqlCmd.ExecuteNonQuery();
                sqlCon.Close();
--------------------编程问答--------------------

string strSQL;
string strConn;
private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
    }
}

构造好sql和conn,在按钮的btnclick事件,调用上面的方法。 --------------------编程问答-------------------- 这种问题 你自己找本初级编程书看都比问人快
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,