using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;namespace WebApplication1
{
/// <summary>
/// _default 的摘要说明。
/// </summary>
public class _default : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Button btnok;
protected System.Web.UI.WebControls.TextBox tbzhuti;
protected System.Web.UI.WebControls.TextBox tbmemo;
protected System.Web.UI.WebControls.Button btncancel;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.btnok.Click += new System.EventHandler(this.btnok_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void btnok_Click(object sender, System.EventArgs e)
{
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" +Server.MapPath("lyb.mdb");
string strSQL = "insert into gb(zhuti,memo) values('" +tbzhuti.Text.ToString()+ "','" +tbmemo.Text.ToString+ "')";
OleDbConnection objConnection=new OleDbConnection(strConnection);
OleDbCommand objCommand = new OleDbCommand(strSQL , objConnection); 
objCommand.Connection.Open();
objCommand.ExecuteNonQuery();
objConnection.Close(); }
}
}
请问为什么个添加了,向数据库里的插入数据的功能,我在浏览网页的时候,确不能把用户输入的信息加入到数据库里呢?

解决方案 »

  1.   

    我用的是  ACCESS数据库
      

  2.   

    你跟踪下你的程序,看看每步的执行情况。
    看看你的SQL是否真确
      

  3.   

    string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" +Server.MapPath("lyb.mdb");你的数据库的信息正确??
      

  4.   

    '" +tbzhuti.Text.ToString()+ "'看样子应该是个文本框,你用'"& Request["tbzhuti"] &"'看看,其他输入框也作类似修改。
      

  5.   

    tbzhuti.Text.ToString()
    这是无效转换,实际并不执行tbzhuti.Text即可
      

  6.   

    string strSQL = "insert into gb(zhuti,memo) values('" +tbzhuti.Text.ToString()+ "','" +tbmemo.Text.ToString+ "')";-------------->>>>>>>上面语法有错误string strSQL = "insert into gb(zhuti,memo) values('" +tbzhuti.Text.ToString()+ "','" + tbmemo.Text.ToString() + "')";
      

  7.   

    private void btnok_Click(object sender, System.EventArgs e)
    {
    string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" +Server.MapPath("lyb.mdb");
    string strSQL = "insert into gb(zhuti,memo) values('" +tbzhuti.Text.ToString()+ "','" +tbmemo.Text.ToString()+ "')";
    OleDbConnection objConnection=new OleDbConnection(strConnection);
    objConnection.Open();
    OleDbCommand objCommand = new OleDbCommand(strSQL , objConnection); 

    objCommand.ExecuteNonQuery();
    objConnection.Close(); }
      

  8.   

    楼上的几位已经指出来啦  连接字符串的语法错误  再就是access要已独占方式打开才行
      

  9.   

    '" +tbzhuti.Text.ToString()+ "'看样子应该是个文本框,你用'"& Request["tbzhuti"] &"'看看,其他输入框也作类似修改。
      

  10.   

    private void btnok_Click(object sender, System.EventArgs e)
    {
    string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" +Server.MapPath("lyb.mdb");
    string strSQL = "insert into gb(zhuti,memo) values('" +tbzhuti.Text.ToString()+ "','" +tbmemo.Text.ToString+ "')";
    上面的代码有处错误,应该是tbmemo.Text.ToString(),你的
    tbmemo.Text看样子应该是个文本框,如果这样的话就不用.ToString()了,它不起作用的