从表单C#将数据插入到Access数据库 [英] C# Inserting Data from a form into an access Database

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

问题描述

我开始学习 C#,成为坚持从文本框将信息插入一个访问数据库时点击钮。



我得到的问题是,在添加过程。该代码执行的try ... catch 部分,然后返回一个错误说微软Access数据库引擎,并没有给出任何线索。



下面是代码:

 命名空间WindowsFormsApplication1 
{
公共部分类FormNewUser:表
{
公共FormNewUser()
{
的InitializeComponent();
}

私人无效BTNSave_Click(对象发件人,EventArgs五)
{
OleDbConnection的康恩=新的OleDbConnection();
conn.ConnectionString = @供应商= Microsoft.ACE.OLEDB.12.0;数据源= C:\Users\kenny\Documents\Visual工作室2010\Projects\Copy Cegees\Cegees\\ \\Cegees\Login.accdb;

字符串username = TEXTNewUser.Text;
字符串密码= TEXTNewPass.Text;

OleDbCommand的CMD =新的OleDbCommand(INSERT INTO登录(用户名,密码)VALUES(@username,@Password));
cmd.Connection =康恩;

conn.Open();

如果(conn.State == ConnectionState.Open)
{
cmd.Parameters.Add(@用户名,OleDbType.VarChar).value的=用户名;
cmd.Parameters.Add(@密码,OleDbType.VarChar).value的=密码;


{
cmd.ExecuteNonQuery();
MessageBox.Show(新增的数据);
conn.Close();
}
赶上(OleDbException前)
{
MessageBox.Show(ex.Source);
conn.Close();
}
}
,否则
{
MessageBox.Show(连接失败);
}
}
}
}


解决方案

密码是一个保留字。支架该字段名称,以避免混淆数据库引擎。

 插入到登录(用户名,[密码])


I started learning about C# and have become stuck with inserting information from textboxes into an Access database when a click button is used.

The problem I get is during the adding process. The code executes the Try... Catch part and then returns an error saying "Microsoft Access Database Engine" and doesn't give any clues.

Here is the code:

namespace WindowsFormsApplication1
{
    public partial class FormNewUser : Form
    {
        public FormNewUser()
        {
            InitializeComponent();
        }

        private void BTNSave_Click(object sender, EventArgs e)
        {
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\kenny\Documents\Visual Studio 2010\Projects\Copy Cegees\Cegees\Cegees\Login.accdb";

            String Username = TEXTNewUser.Text;
            String Password = TEXTNewPass.Text;

            OleDbCommand cmd = new OleDbCommand("INSERT into Login (Username, Password) Values(@Username, @Password)");
            cmd.Connection = conn;

            conn.Open();

            if (conn.State == ConnectionState.Open)
            {
                cmd.Parameters.Add("@Username", OleDbType.VarChar).Value = Username;
                cmd.Parameters.Add("@Password", OleDbType.VarChar).Value = Password;

                try
                {
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Data Added");
                    conn.Close();
                }
                catch (OleDbException ex)
                {
                    MessageBox.Show(ex.Source);
                    conn.Close();
                }
            }
            else
            {
                MessageBox.Show("Connection Failed");
            }
        }
    }
}

解决方案

Password is a reserved word. Bracket that field name to avoid confusing the db engine.

INSERT into Login (Username, [Password])

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

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