.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-tr
.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;">【文章出处:响水网站开发公司 http://www.1234xp.com/xiangshui.html 欢迎留下您的宝贵建议】
</span></span>