求大神找错,为什么我的代码运行后提示System.Data.OleDb.OleDbException: 没有为命令对象设置命令。
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSys...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data.OleDb;
using System.Web.Configuration;
using System.Web.UI.WebControls;
public partial class 注册 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("登陆界面.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
string dbpath = AppDomain.CurrentDomain.BaseDirectory + "access\\注册信息.accdb";
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + dbpath); //定义数据库连接对象
con.Open(); //打开数据库连接
OleDbCommand cmd = new OleDbCommand(sql, con); //定义Command对象
string accessstr = "insert into admin values('" + TextBox1.Text.Trim() + "','" + TextBox2.Text.Trim() + "','" + TextBox3.Text.Trim() + "','" + TextBox4.Text.Trim() + "','" + TextBox5.Text.Trim() + "','" + TextBox6.Text.Trim() + "')";
cmd.ExecuteNonQuery(); //执行Command命令
con.Close(); //关闭数据库连接
}
public string sql { get; set; }
} 展开
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data.OleDb;
using System.Web.Configuration;
using System.Web.UI.WebControls;
public partial class 注册 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("登陆界面.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
string dbpath = AppDomain.CurrentDomain.BaseDirectory + "access\\注册信息.accdb";
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + dbpath); //定义数据库连接对象
con.Open(); //打开数据库连接
OleDbCommand cmd = new OleDbCommand(sql, con); //定义Command对象
string accessstr = "insert into admin values('" + TextBox1.Text.Trim() + "','" + TextBox2.Text.Trim() + "','" + TextBox3.Text.Trim() + "','" + TextBox4.Text.Trim() + "','" + TextBox5.Text.Trim() + "','" + TextBox6.Text.Trim() + "')";
cmd.ExecuteNonQuery(); //执行Command命令
con.Close(); //关闭数据库连接
}
public string sql { get; set; }
} 展开
展开全部
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"F:\\student.mdb;" ;
这里错了
应该这样写
string MyConnString = "Provider = Microsoft.jet.OLEDB.4.0; Data Source =" + Server.MapPath("相对路径");
Server.MapPath这个里面应该是写相对路径,他会返回服务器上的绝对路径
这里错了
应该这样写
string MyConnString = "Provider = Microsoft.jet.OLEDB.4.0; Data Source =" + Server.MapPath("相对路径");
Server.MapPath这个里面应该是写相对路径,他会返回服务器上的绝对路径
追问
你这个是我的代码吗?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询