SQL表中新增数

.aspx代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="数据增删查改.aspx.cs" Inherits="数据增删查改" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    姓名:<asp:TextBox ID="txt_name" runat="server"></asp:TextBox><br />
    语文:<asp:TextBox ID="txt_chinese" runat="server"></asp:TextBox><br />
    数学:<asp:TextBox ID="txt_math" runat="server"></asp:TextBox><br />
    物理:<asp:TextBox ID="txt_physics" runat="server"></asp:TextBox><br />
    化学:<asp:TextBox ID="txt_chemstry" runat="server"></asp:TextBox><br />
    英语:<asp:TextBox ID="txt_english" runat="server"></asp:TextBox><br />
        <asp:Button ID="btn_add" runat="server" Text="新增" οnclick="btn_add_Click" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="姓名" HeaderText="姓名" SortExpression="姓名" />
                <asp:BoundField DataField="语文" HeaderText="语文" SortExpression="语文" />
                <asp:BoundField DataField="数学" HeaderText="数学" SortExpression="数学" />
                <asp:BoundField DataField="物理" HeaderText="物理" SortExpression="物理" />
                <asp:BoundField DataField="化学" HeaderText="化学" SortExpression="化学" />
                <asp:BoundField DataField="英语" HeaderText="英语" SortExpression="英语" />
            </Columns>
        </asp:GridView>
    
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:userinfoConnectionString %>" 
            SelectCommand="SELECT * FROM [成绩]"></asp:SqlDataSource>
    
    </div>
    </form>
</body>
</html>

.

.aspx.cs代码如下:

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

public partial class 数据增删查改 : System.Web.UI.Page
{
  string con = "server=localhost\\SQL2005 ;uid=sa;pwd=1111qq;database=userinfo";
    SqlConnection conn;
    SqlCommand cmd;
    //SqlDataReader datar;
    string str_sql;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn_add_Click(object sender, EventArgs e)
    {
        try
        {
            conn = new SqlConnection(con);
            conn.Open();

            str_sql = makestr();
            cmd = new SqlCommand(str_sql, conn);
            int i=cmd.ExecuteNonQuery();
            if (i > 0) { Response.Write("<script>alert('添加成功!')</script>"); GridView1.DataBind(); }
           
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());

        }

        finally
        {
            conn.Close();
        }
       
       //数据绑定,可以马上显示在表中

    }
    protected string makestr() {
        string _result = "";
        string _name, _chinese, _math, _physics, _chemstry, _english;
        _name = txt_name.Text;
        _chinese = txt_chinese.Text;
        _math = txt_math.Text;
        _physics = txt_physics.Text;
        _chemstry = txt_chemstry.Text;
        _english = txt_english.Text;
        //下面SQL语句极易出错,注意里面所用的的()和,都必须是英文格式下的,不然会报错!!!!!!!!!!!!!!
        _result = "Insert into 成绩(姓名,语文,数学,物理,化学,英语)values('" + _name + "','" + _chinese + "','" + _math + "','" + _physics + "','" + _chemstry + "','" + _english + "')";

        return _result;
    }
    
}

易错点:SQL语句的“()”、“,”等所有字符必须是英文格式下的,否则会报错,此处应注意!!!



<span style="font-family:Arial, Helvetica, sans-serif;"><span style="white-space: normal;">
</span></span>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值