insert into 语法错误
各位大神们,这个问题一直没解决,我在图书馆和实验室参考了很多资料,无奈我们都是菜鸟。。。vs可以运行,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;
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 strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Z:\\数据库\\135.mdb";
OleDbConnection myConn = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;Data Source=" + strCon + ";");
myConn.Open();
if (myConn.State == ConnectionState.Open)
{
Label1.Text = "Access数据库打开";
myConn.Close();
}
if (myConn.State == ConnectionState.Closed)
{
Label2.Text = "Access数据库关闭";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Z:\\数据库\\135.mdb";
OleDbConnection myConn = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;Data Source=" + strCon + ";");
string strsql = "insert into 135.mdb_表1(姓名,年龄)values ('" + TextBox1.Text + "','" + TextBox2.Text + "')";
OleDbCommand comm = new OleDbCommand(strsql, myConn);
if (myConn.State == ConnectionState.Closed)
{
myConn.Open();
}
if (Convert.ToInt32(comm.ExecuteNonQuery()) > 0)//这里是提示出错的地方
{
Label3.Text = "添加成功!";
}
else
{
Label3.Text = "添加失败!";
}
myConn.Close();
}
}
------解决方案-------------------- 135.mdb_表1(姓名,年龄)能这样写表名字么?
可以在strsql个地方放一个断点,看看sql语句是什么然后放到access里面进行一下调试!
------解决方案--------------------string strsql = "insert into [135.mdb_表1](姓名,年龄) values (@name,@age)";
OleDbCommand comm = new OleDbCommand(strsql, myConn);
comm.Parameters.AddWithValue("@name",TextBox1.Text);
comm.Parameters.AddWithValue("@age",TextBox2.Text)
另外,你的
([姓名],[年龄])
中全角的括号,要改成半角的
------解决方案--------------------tring strsql = "insert into [135.mdb_表1]([姓名],[年龄])values ('" + TextBox1.Text + "','" + TextBox2.Text + "')";
确实这是个中文括号,改成英文括号
------解决方案--------------------另外,你的
([姓名],[年龄])
中全角的括号,要改成半角的