如何通过访问2007在c#中编写插入按钮程序? [英] how to write insert button program in c# via access 2007?

查看:60
本文介绍了如何通过访问2007在c#中编写插入按钮程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写一些代码来填充我的访问数据库通过文本框形式,但它给出错误为EXECUTE后的预期查询名称。在BOLD线以下,问题是什么?

非常感谢



im trying to write write some codes to fill my access database via textbox form but it giving error as "TheExpected query name after EXECUTE." on below BOLD line, whats the problem?
many thanks

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication35
{
    public partial class Form1 : Form
    {
      
        
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        { 
            OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Persian\\Desktop\\a.accdb");
            con.Open();
            OleDbCommand cmd = new OleDbCommand ( "INSERT INTO 1(id,name) VALUES(@a,@b)", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@a", textBox1.Text);
            cmd.Parameters.AddWithValue("@b", textBox2.Text);
            int re = cmd.ExecuteNonQuery(); // error is here
            con.Close();
            if (re > 0)
            {
                MessageBox.Show("sss");
            }

        }
    }
}

推荐答案

可能,这是你的桌名:1不是桌子的好名字!如果你把它括在括号中,你可能会放弃它:

Probably, it's your table name: "1" is not a good name for a table! You might get away with it if you enclose it in brackets:
OleDbCommand cmd = new OleDbCommand ( "INSERT INTO [1] (id,name) VALUES(@a,@b)", con);

但是我会更改它并使用反映您将使用它的东西:用户或客户。

But I'd change it and use something that reflects the use you will put it to: "Users" or "Customers" perhaps.


这篇关于如何通过访问2007在c#中编写插入按钮程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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