插入访问数据库 [英] insert into access database

查看:86
本文介绍了插入访问数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将文本框中的数据插入ms Access数据库时遇到问题,我收到错误消息 INSERT INTO中的语法错误。



有人可以帮我吗?这是代码:

  public void button1_Click(对象发送者,EventArgs e)//保存
{
使用(OleDbConnection conn =新
OleDbConnection(@ Provider = Microsoft.Jet.OLEDB.4.0; Data
Source = | DataDirectory | \productdb.mdb))
{
OleDbCommand CmdSql = new OleDbCommand(插入[产品](型号,名称,
价格,类型,数量,制造商,进口商)[b]在这里输入代码
{
conn。 Open();
CmdSql.Parameters.AddWithValue( @ Kod,textBox1.Text);
CmdSql.Parameters.AddWithValue( @ names,textBox2.Text);
CmdSql。 Parameters.AddWithValue( @ price,textBox3.Text);
CmdSql.Parameters.AddWithValue( @ type,textBox4.Text);
CmdSql.Parameters.AddWithValue( @ volume,textBox5 .Text);
CmdSql.Parameters.AddWithValue( @ manufacturer,textBox6.Text);
CmdSql.Parameters.AddWithValue( @ importer,textBox7.Text);
CmdSql。 ExecuteNonQuery(); //我在这里得到错误<<<
conn.Close();
}
}

解决方案

您缺少插入语句的 VALUES 部分:

  OleDbCommand CmdSql = new OleDbCommand(插入[product](Kod,[names],价格,类型,数量,制造商,进口商) (@ Kod,@ names,@ price,@ type,@ volume,@ manufacturer,@ importer),conn); 

您正在使用Access和OldeDbCommand ...因此您实际上需要使用而不是命名参数:

  OleDbCommand CmdSql = new OleDbCommand(插入[product ](Kod,[名称],价格,类型,数量,制造商,进口商)VALUES(?,?,?,?,?,?,?),conn); 

请参见此问题以获取更多信息。



注意:请确保将所有保留的关键字都放在方括号中。


i have trouble inserting data from textbox into ms access database, I get an error "Syntax error in INSERT INTO."

Can someone help me out please? here's the code:

public void button1_Click(object sender, EventArgs e)//save
{ 
using (OleDbConnection conn = new   
OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data 
Source=|DataDirectory|\productdb.mdb"))
{
OleDbCommand CmdSql = new OleDbCommand("Insert into [product](Kod, names, 
price,type,volume,manufacturer,importer)
enter code here
{
conn.Open();
CmdSql.Parameters.AddWithValue("@Kod", textBox1.Text);
CmdSql.Parameters.AddWithValue("@names", textBox2.Text);
CmdSql.Parameters.AddWithValue("@price", textBox3.Text);
CmdSql.Parameters.AddWithValue("@type", textBox4.Text);
CmdSql.Parameters.AddWithValue("@volume", textBox5.Text);
CmdSql.Parameters.AddWithValue("@manufacturer", textBox6.Text);
CmdSql.Parameters.AddWithValue("@importer", textBox7.Text);
CmdSql.ExecuteNonQuery();// i get the error here<<<
conn.Close();
}
}

解决方案

You are missing the VALUES portion of your insert statement:

OleDbCommand CmdSql = new OleDbCommand("Insert into [product] (Kod, [names], price, type, volume, manufacturer, importer) VALUES (@Kod, @names, @price, @type, @volume, @manufacturer, @importer)", conn);

And you are using Access and OldeDbCommand... so you actually need to use ? instead of a named parameter:

OleDbCommand CmdSql = new OleDbCommand("Insert into [product] (Kod, [names], price, type, volume, manufacturer, importer) VALUES (?, ?, ?, ?, ?, ?, ?)", conn);

See this question for more information.

A side note: Ensure you wrap any reserved keywords in square brackets.

这篇关于插入访问数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆