日期:2014-05-18  浏览次数:20758 次

关于数据库的修改问题
小弟初学vs2010,制作一个网页,实现数据库的添加和修改,运行的时候都没出错,但是access数据库里的内容却没有真正地被修改,求高手指教






这是代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Text;
using System.ComponentModel;
using System.Drawing;


public partial class _Default : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
  string strPath = "db1.mdb";
  OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +strPath+";");
  myConn.Open();
  if (myConn.State == ConnectionState.Open)
  {
  Label1.Text = "数据库连接开启!";
  myConn.Close();

  }
  if (myConn.State == ConnectionState.Closed)
  {
  Label2.Text = "数据库连接关闭!";
  } 
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
  string strPath = "db1.mdb";
  OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strPath + ";");




  string strsql = "insert into db1_ND(N_姓名,N_密码) values(" + TextBox1.Text + "," + TextBox2.Text + ")";
  // SqlCommand comm = new SqlCommand(strsql, myConn);//实例化SqlCommand对象
  if (myConn.State == ConnectionState.Closed)//判断连接是否关闭
  {
  myConn.Open();//打开数据库连接
  }
  //判断ExecuteNonQuery方法返回的参数是否大于0,大于0表示添加成功
  //if (Convert.ToInt32(myConn.ExecuteNonQuery()) > 0)
  {
  Label3.Text = "添加成功!";
  }
  // else
  {
  // Label3.Text = "添加失败!";
  }
  myConn.Close();//关闭数据库连接
  }
}


access数据库在c盘的一个文件夹里,并没有和vs的生成文件在一起

------解决方案--------------------
参考
http://www.cnblogs.com/airwolf2004/articles/313974.html
http://blog.csdn.net/dfkurama/article/details/523350
http://technet.microsoft.com/zh-tw/library/system.data.oledb.oledbcommand.commandtext%28zh-cn,VS.90%29.aspx