救命 简单的写入数据库的程序 老插入失败
一个简单的往数据库里插入一行的程序。老是提示插入失败
在web.config中的内容:
<appSettings>
<add key= "Charset " value= "gb2312 " />
<add key= "connString " value= "db/data.mdb " />
</appSettings>
程序内容:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string author= "aaaa ";
string contents = "bbbb ";
string times = "cccc ";
// object times = DateTime.Now;
// Response.Write(author);
// Response.Write(contents);
// Response.Write(times);
string connStr = System.Configuration.ConfigurationSettings.AppSettings[ "connString "];
OleDbConnection MyConnection = new OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0; Data source= " + Server.MapPath(connStr));
//OleDbConnection MyConnection = new OleDbConnection(connStr);
string insertStr = "insert into che values( ' " + author + " ', ' " + contents + " ', ' " + times + " ') ";
OleDbCommand myCommand = new OleDbCommand(insertStr, MyConnection);
// Response.Write(insertStr);
try
{
MyConnection.Open();
myCommand.ExecuteNonQuery();
Label1.Text = "插入成功! ";
}
catch
{
Label1.Text = "插入失败! ";
}
finally
{
MyConnection.Close();
TextBox1.Text = " ";
TextBox2.Text = " ";
}
}
}
[解决办法]
string insertStr = "insert into che values( ' " + author + " ', ' " + contents + " ', ' " + times + " ') ";
==================
string insertStr = "insert into che(author,contents ,times ) values( ' " + author + " ', ' " + contents + " ', ' " + times + " ') ";
[解决办法]
string insertStr = "insert into che values( ' " + author + " ', ' " + contents + " ', ' " + times + " ') ";
=========================
string insertStr = "insert into che values( ' " + author + " ', ' " + contents + " ',# " + times + "#) ";
catch
{
Label1.Text = "插入失败! ";
}
=========================
catch(Exception e)
{
Label1.Text = e.Message;
}
还有标签的出错信息直接输出e.Message看看是什么错误