这是连接数据库的文件:
using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Configuration;namespace jbbs
{
/// <summary>
/// Classdata 的摘要说明。
/// </summary>
public class Classdata:System.Web.UI.Page
{   
public string databasepath =ConfigurationSettings.AppSettings["datapath"];
         private OleDbConnection myconn;
         public Classdata()
{   
//
// TODO: 在此处添加构造函数逻辑
//
                 myconn=new OleDbConnection(ConnectionString);  }
private string ConnectionString
{
get 
{
 string dblink="provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath(databasepath)+";";
 return dblink;
}
}
public void open()
{
   myconn.Open();
}
public DataSet getDataSet(string sql)
{
 DataSet ds=new DataSet();
try
{
OleDbDataAdapter odd= new OleDbDataAdapter(sql,this.myconn);
odd.Fill(ds);
}
catch{}
finally{myconn.Close();}
           return ds;
 }
          public bool executeCommand(string sql)
{
        bool ret=false;
try

OleDbCommand odc=new OleDbCommand(sql,this.myconn);
odc.Connection.Open(); int n=odc.ExecuteNonQuery();
if( n>0 )
ret=true;
 }
catch(System.Data.OleDb.OleDbException error)
{
 Response.Write(error.ToString());
}
finally {myconn.Close();}
return ret;

}
}
}
其中还是主要用到了这个函数:
public bool executeCommand(string sql)
如点击注册按钮完成注册:
private void ly_denglu_Click(object sender, System.EventArgs e)
{
if(Page.IsValid)
{
 
              string sql="insert into userinfo(name,password) values('"+Text1.Value+"','"+TextBox1.Text+"')";
  if(obj.executeCommand(sql))
     Response.Write("<script>alert('注册成功');parent.location='index.aspx'</script>");
}
}
可是为什么总是执行到
int n=odc.ExecuteNonQuery();
后就不执行下面的if语句了,所以ret一直为flase根本插入不了数据,请指导一下

解决方案 »

  1.   

    建議先関掉try
    肯定是你這句話執行錯誤.
    讓錯誤顯示出來.
    不要在調試的時候做錯誤處理,除非你確認這段代碼没有錯誤可以發佈的時候才try.
      

  2.   

    你在调试的时候看看你的sql语句是否有问题,怀疑你的sql语句有问题但是系统又无法获取这个异常造成你所说的情况的.
      

  3.   

    一、首先你做调试,看n是什么
    其次把catch的部分先注掉,看看具体错误
     二、把string sql="insert into userinfo(name,password) values('"+Text1.Value+"','"+TextBox1.Text+"')";
    改为::
     string sql="insert into userinfo([name],[password]) values('"+Text1.Value+"','"+TextBox1.Text+"')";
    在access中可能会遇到关键字,所以没有插入成功
      

  4.   

    按照studydotnet(乐于助人,开心接分!) 的改了起先是insert into语句问题,现在是“System.Data.OleDb.OleDbException: 操作必须使用一个可更新的查询”但是我是插入数据
      

  5.   

    obj.executeCommand(sql)有返回值吗?
      

  6.   

    int n = odc.ExecuteNonQuery() 
    n代表返回受影响的行数