C#文本写入数据库(未解决)
Default2.aspx
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>无标题页</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 姓名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
13 性别:
14 <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
15 <br />
16 QQ:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
17 <br />
18 邮箱:<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
19 <br />
20 <br />
21 <br />
22 <asp:Button ID="Button1" runat="server" Text="Button" /></div>
23 </form>
24</body>
25</html>
26
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>无标题页</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 姓名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
13 性别:
14 <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
15 <br />
16 QQ:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
17 <br />
18 邮箱:<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
19 <br />
20 <br />
21 <br />
22 <asp:Button ID="Button1" runat="server" Text="Button" /></div>
23 </form>
24</body>
25</html>
26
Default2.aspx.cs
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Data.OleDb; //这里要引用OleDb空间
12
13public partial class Default2 : System.Web.UI.Page
14{
15 //数据库连接字符串
16 static string strCon = @"provider = Microsoft.Jet.OLEDB.4.0;Data source = D:\liuyan\App_Data\db.mdb";
17
18 //申明连接数据源的对象和对数据源进行操作的对象
19 OleDbConnection con = new OleDbConnection(strCon);
20 OleDbCommand cmd = new OleDbCommand();
21
22 //点击按钮添加记录
23 protected void Button1_Click(object sender, EventArgs e)
24 {
25 string name = TextBox1.Text.Trim(); //姓名
26 string sex = TextBox2.Text.Trim(); //性别
27 string qq = TextBox3.Text.Trim(); //QQ
28 string email = TextBox4.Text.Trim(); //Email
29
30 //要插入记录的sql语句
31 string insertText = "insert into biao(name,sex,qq,email) values('" + name + "','" + sex + "','" + qq + "','" + email + "')";
32
33 //数据源打开
34 con.Open();
35
36 //设置操作对象的数据源
37 cmd.Connection = con;
38
39 //要执行的SQL语句
40 cmd.CommandText = insertText;
41
42 //执行操作
43 cmd.ExecuteNonQuery();
44
45 //数据源关闭
46 con.Close();
47 }
48
49 protected void Page_Load(object sender, EventArgs e)
50 {
51
52 }
53}
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Data.OleDb; //这里要引用OleDb空间
12
13public partial class Default2 : System.Web.UI.Page
14{
15 //数据库连接字符串
16 static string strCon = @"provider = Microsoft.Jet.OLEDB.4.0;Data source = D:\liuyan\App_Data\db.mdb";
17
18 //申明连接数据源的对象和对数据源进行操作的对象
19 OleDbConnection con = new OleDbConnection(strCon);
20 OleDbCommand cmd = new OleDbCommand();
21
22 //点击按钮添加记录
23 protected void Button1_Click(object sender, EventArgs e)
24 {
25 string name = TextBox1.Text.Trim(); //姓名
26 string sex = TextBox2.Text.Trim(); //性别
27 string qq = TextBox3.Text.Trim(); //QQ
28 string email = TextBox4.Text.Trim(); //Email
29
30 //要插入记录的sql语句
31 string insertText = "insert into biao(name,sex,qq,email) values('" + name + "','" + sex + "','" + qq + "','" + email + "')";
32
33 //数据源打开
34 con.Open();
35
36 //设置操作对象的数据源
37 cmd.Connection = con;
38
39 //要执行的SQL语句
40 cmd.CommandText = insertText;
41
42 //执行操作
43 cmd.ExecuteNonQuery();
44
45 //数据源关闭
46 con.Close();
47 }
48
49 protected void Page_Load(object sender, EventArgs e)
50 {
51
52 }
53}
数据库db.mdb中表:biao
以上是代码文件,运行不出错,可是就是写不进去数据库(ACCESS),极度郁闷,敬请高人指正!不慎感激!