标题:怎么将输入在Textbox的值按添加按钮后将数据更新到数据库中?
取消只看楼主
编程2011
Rank: 2
来 自:湖南
等 级:论坛游民
帖 子:73
专家分:19
注 册:2011-3-3
结帖率:92%
已结贴  问题点数:8 回复次数:1 
怎么将输入在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 = "发生错误,该学生信息已经提交或请确认你的输入是否正确";
            }
        }
    }
}
  下面是前台代码
程序代码:
<%@ 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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">&nbsp;
          <asp:Button ID="add" runat ="server" Text ="提交" onclick="add_Click"></asp:Button><FONT face="宋体">&nbsp;&nbsp;
          <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>
  麻烦各位看看,很急啊。。要交了。谢谢

  
搜索更多相关主题的帖子: 数据库 public 
2011-11-20 21:04
编程2011
Rank: 2
来 自:湖南
等 级:论坛游民
帖 子:73
专家分:19
注 册:2011-3-3
得分:0 
谢谢大家 问题解决了
2011-11-21 23:46



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-355789-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.052126 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved