您好我想基本上将标签中的值插入到ms访问中的表中。 我已经为文本框做了它并且它存储但是对于Label当我尝试存储它时没有出现错误但是它不存储在数据库中我应该怎么做?我使用以下代码
static OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\New folder\Project 1.0\WebSite1\New Microsoft Office Access 2007 Database.accdb");
OleDbDataAdapter ada = new OleDbDataAdapter();
OleDbCommand cmd = new OleDbCommand();
OleDbDataReader dr;
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string str = "insert into Orders (Products, Amount)" + " values (@p1, @p2)";
con.Open();
cmd = new OleDbCommand(str, con);
cmd.Parameters.AddWithValue("@p1", Label18.Text);
cmd.Parameters.AddWithValue("@p2", Label16.Text);
cmd.ExecuteNonQuery();
con.Close();
}
catch
{
Console.WriteLine("Exception Occured");
}
finally
{
if (con != null && con.State != ConnectionState.Closed)
{ con.Close(); }
}
}
此外,我尝试将文本框值存储到“地址”列下的相同表“订单”中,但面临相同的上述问题...表格未更新。我之前使用过这段代码用于其他文本框等不同的表格,并且它运行良好..
static OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\New folder\Project 1.0\WebSite1\New Microsoft Office Access 2007 Database.accdb");
OleDbDataAdapter ada = new OleDbDataAdapter();
OleDbCommand cmd = new OleDbCommand();
OleDbDataReader dr;
protected void Button1_Click(object sender, EventArgs e)
{
{
string str = "insert into Orders (Address)" + " values (@p1)";
con.Open();
cmd = new OleDbCommand(str, con);
cmd.Parameters.AddWithValue("@p1", TextBox1.Text);
cmd.ExecuteNonQuery();
con.Close();
}
}
答案 0 :(得分:0)
OLE DB .NET提供程序不支持传递的命名参数 SQL语句的参数
尝试
string str = "insert into Orders (Products, Amount) values (?,?)";
您可以更改以下代码
protected void Button1_Click(object sender, EventArgs e)
{
try
{
using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\New folder\Project 1.0\WebSite1\New Microsoft Office Access 2007 Database.accdb"))
using (OleDbCommand cmd = new OleDbCommand("insert into Orders (Products, Amount) values (?,?)", con))
{
cmd.Parameters.AddWithValue("@p1", Label18.Text);
cmd.Parameters.AddWithValue("@p2", int.Parse(Label16.Text));
con.Open();
int no = cmd.ExecuteNonQuery();
Console.WriteLine("number of rows affected = " + no);
}
}
catch (Exception ex)
{
Console.WriteLine("Exception Occured :" ex.ToString());
}
}