【发布时间】:2013-01-05 09:57:31
【问题描述】:
我有这个 ASPX 代码:
<form name="AddArticle" action="Register.aspx" method="post">
<b>
title :<br /></b><input id="Text1" type="text" name="ArticleTitle"/><p></p>
<b>
date: <br /></b><input id="Text2" type="text" name="ArticleDate"/><p></p>
<b>
author : <br /></b><input id="Text3" type="text" name="ArticleAuthor"/><p></p>
<b>
text: <br /></b> <textarea rows="10" cols="60" name="ArticleBody"></textarea>
<br />
<input id="Reset1" type="reset" value="clean" />
<input id="Submit1" type="submit" value="send" /></form>
我有这个 ASPX.C# 代码:
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.OleDb;
public partial class Register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string ArticleTitle = "", ArticleBody = "", ArticleAuthor = "", ArticleDate = "";
if (IsPostBack)
{
try
{
ArticleTitle = Request.Form["ArticleTitle"].ToString();
ArticleDate = Request.Form["ArticleDate"].ToString();
ArticleAuthor = Request.Form["ArticleAuthor"].ToString();
ArticleBody = Request.Form["ArticleBody"].ToString();
string dpath = Server.MapPath(@"App_Data") + "/MySite.mdb";
string connectionstring = @"Data source='" + dpath + "';Provider='Microsoft.Jet.OLEDB.4.0';";
OleDbConnection con = new OleDbConnection(connectionstring);
string QuaryString = string.Format("insert into tblArticles(ArticleTitle,ArticleBody, ArticleAuthor, PostDate) values ('{0}','{1}','{2}','{3}')", ArticleTitle, ArticleBody, ArticleAuthor, ArticleDate);
OleDbCommand cmd = new OleDbCommand(QuaryString, con);
con.Open();
cmd.ExecuteNonQuery();
}
catch
{
Response.Redirect("Default.aspx");
}
}
}
}
我的问题是为什么当我单击“发送”按钮时,表单中的信息(标题、作者...)没有插入到数据库中?我该如何解决? 希望得到帮助,谢谢。
【问题讨论】:
-
认真的吗?至少阅读参数化查询...
-
旁注:
insert into tblArticles(ArticleTitle, PostDate) values ('{0}','{1}','{2}','{3}')- 当您指定 2 列“(ArticleTitle, PostDate)”但想要插入 4 个值('{0}','{1}','{2}','{3}')时应该抛出异常 -
你写的,部分文字没有复制成功,我现在修复一下。谢谢
标签: c# asp.net database forms webforms