怎么将输入在Textbox的值按添加按钮后将数据更新到数据库中?
下面是我的代码。我好像实现不了。。。。下面是后台代码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;
namespace stumanger
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void add_Click(object sender, EventArgs e)
{
try
{
SqlConnection Con = new SqlConnection("Data Source=localhost; Initial Catalog=stuinfo;User ID='sa';Pwd=''");
string sql = "insert into stuinfo (xh,xm,xb,zy,bj,dept) values('" + xh.Text + "','" + xm.Text + "','" + xb.Text + "','" + zy.Text + "','" + bj.Text + "','" + dept.Text + "')";
SqlCommand addCommand = new SqlCommand(sql, Con);
addCommand.Connection.Open();
addCommand.ExecuteNonQuery();
addCommand.Connection.Close();
msg.Text = "编辑完成";
}
catch
{
msg.Text = "发生错误,该学生信息已经提交或请确认你的输入是否正确";
}
}
}
}
下面是前台代码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;
namespace stumanger
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void add_Click(object sender, EventArgs e)
{
try
{
SqlConnection Con = new SqlConnection("Data Source=localhost; Initial Catalog=stuinfo;User ID='sa';Pwd=''");
string sql = "insert into stuinfo (xh,xm,xb,zy,bj,dept) values('" + xh.Text + "','" + xm.Text + "','" + xb.Text + "','" + zy.Text + "','" + bj.Text + "','" + dept.Text + "')";
SqlCommand addCommand = new SqlCommand(sql, Con);
addCommand.Connection.Open();
addCommand.ExecuteNonQuery();
addCommand.Connection.Close();
msg.Text = "编辑完成";
}
catch
{
msg.Text = "发生错误,该学生信息已经提交或请确认你的输入是否正确";
}
}
}
}
程序代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="add.aspx.cs" Inherits="stumanger.WebForm1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www."> <html xmlns="http://www." > <head runat="server"> <title>添加学生信息</title> </head> <body > <form id="addc" runat="server" method ="post"> </br></br></br></br></br></br> <table width="778" align="center" border="0" bgcolor="#3366CC" > <tr><td> <div align="center"><FONT face="宋体">编辑学生信息</FONT></div> </td></tr> <tr><td> <div align="center"><FONT face="宋体">学号: <asp:TextBox ID="xh" runat ="server"></asp:TextBox></FONT></div> </td></tr> <tr><td> <div align="center"><FONT face="宋体">姓名: <asp:TextBox ID="xm" runat ="server"></asp:TextBox></FONT></div> </td></tr> <tr><td> <div align="center"><FONT face="宋体">性别: <asp:TextBox ID="xb" runat ="server"></asp:TextBox></FONT></div> </td></tr> <tr><td> <div align="center"><FONT face="宋体">专业: <asp:TextBox ID="zy" runat ="server"></asp:TextBox></FONT></div> </td></tr> <tr><td> <div align="center"><FONT face="宋体">班级: <asp:TextBox ID="bj" runat ="server"></asp:TextBox></FONT></div> </td></tr> <tr><td> <div align="center"><FONT face="宋体">院系: <asp:TextBox ID="dept" runat ="server"></asp:TextBox></FONT></div> </td></tr> <tr><td> <div align="center"> <asp:Button ID="add" runat ="server" Text ="提交" onclick="add_Click"></asp:Button><FONT face="宋体"> <input type ="reset" value ="重写"</FONT></div> </td></tr> <tr><td> <div align="center"> <asp:Label ID="msg" runat="server"></asp:Label> </td></tr> </table> <div> </div> </form> </body> </html>麻烦各位看看,很急啊。。要交了。谢谢