c#保存数据以访问数据库

时间:2022-12-21 15:38:35

How can I add my data to my database? I use an access database, at this moment I have a listview and get my data from my database into my listview. I made a second form application which is for adding users in this case.

如何将数据添加到数据库中?我使用一个access数据库,此时我有一个listview并将我的数据从数据库中获取到listview中。我做了第二个表单应用程序,用于在这个例子中添加用户。

So: I want to add my data to my database with the second form I have made.

所以:我想用我制作的第二种表格将我的数据添加到我的数据库中。

This is my code. Can you please help me?

这是我的代码。你能帮我一下吗?

namespace Test_login
{
    public partial class AddUser : Form
    {
        public AddUser()
        {
            InitializeComponent();
        }
        private void BtnSaveUser_Click(object sender, EventArgs e)
        {
            {
                OleDbConnection connect = new OleDbConnection();
                connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\Daniel\Dropbox\Project Barroc-IT\Database\Barroc-IT Database.accdb";
                string QueryText = "INSERT INTO Users (Name,Surname,Department,Function,Staffcode,Password) values (@Name,@Surname,@Department,@Function,@Staffcode,@Password)";
                connect.Open();
                using (OleDbCommand command = new OleDbCommand(QueryText))
                {
                    try
                    {
                        OleDbDataAdapter da = new OleDbDataAdapter("INSERT INTO Users", connect);

                        String Name = Name_textbox.Text;
                        String Surname = Surname_textbox.Text;
                        String Department = Department_textbox.Text;
                        String Function = Function_textbox.Text;
                        String Staffcode = Staffcode_textbox.Text;
                        String Password = Password_textbox.Text;

                        command.Parameters.AddWithValue("@Name", this.Name_textbox.Text);
                        command.Parameters.AddWithValue("@Surname", this.Surname_textbox.Text);
                        command.Parameters.AddWithValue("@Department", this.Department_textbox.Text);
                        command.Parameters.AddWithValue("@Function", this.Function_textbox.Text);
                        command.Parameters.AddWithValue("@Staffcode", this.Staffcode_textbox.Text);
                        command.Parameters.AddWithValue("@Password", this.Password_textbox.Text);

                        command.ExecuteNonQuery();
                        connect.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        connect.Close();
                    }
                }
            }
        }
    }
}

2 个解决方案

#1


2  

You didn't mentioned about your problem but,

你没有提到你的问题,

Name, Function and Password are reserved keywords on Microsoft Access. You should use them with square brackets like; [Name], [Function] and [Password]

名称、功能和密码是在Microsoft Access上保留的关键字。你应该用方括号像;[名字]、[功能]和[密码)

As a general recomendation, don't use reserved keywords for your identifiers and object names in your database.

一般来说,不要为数据库中的标识符和对象名使用保留的关键字。

#2


1  

you can do as below

你可以这样做。

string sqlQuery = "INSERT INTO Users (`Name`,`Surname`,Department,`Function`,Staffcode,`Password`) values (?,?,?,?,?,?)";
using (OleDbConnection conn = new OleDbConnection("your connection string"))
using(OleDbCommand cmd = new OleDbCommand(sqlQuery, conn))
{
    conn.Open();
    cmd.Parameters.AddWithValue("@Name", this.Name_textbox.Text);
    cmd.Parameters.AddWithValue("@Surname", this.Surname_textbox.Text);
    cmd.Parameters.AddWithValue("@Department", this.Department_textbox.Text);
    cmd.Parameters.AddWithValue("@Function", this.Function_textbox.Text);
    cmd.Parameters.AddWithValue("@Staffcode", this.Staffcode_textbox.Text);
    cmd.Parameters.AddWithValue("@Password", this.Password_textbox.Text);

    cmd.ExecuteNonQuery();

}

for reserved keywords use tilde mark and also you need to set connection for the command object

对于保留的关键字,使用倾斜标记,还需要为命令对象设置连接

#1


2  

You didn't mentioned about your problem but,

你没有提到你的问题,

Name, Function and Password are reserved keywords on Microsoft Access. You should use them with square brackets like; [Name], [Function] and [Password]

名称、功能和密码是在Microsoft Access上保留的关键字。你应该用方括号像;[名字]、[功能]和[密码)

As a general recomendation, don't use reserved keywords for your identifiers and object names in your database.

一般来说,不要为数据库中的标识符和对象名使用保留的关键字。

#2


1  

you can do as below

你可以这样做。

string sqlQuery = "INSERT INTO Users (`Name`,`Surname`,Department,`Function`,Staffcode,`Password`) values (?,?,?,?,?,?)";
using (OleDbConnection conn = new OleDbConnection("your connection string"))
using(OleDbCommand cmd = new OleDbCommand(sqlQuery, conn))
{
    conn.Open();
    cmd.Parameters.AddWithValue("@Name", this.Name_textbox.Text);
    cmd.Parameters.AddWithValue("@Surname", this.Surname_textbox.Text);
    cmd.Parameters.AddWithValue("@Department", this.Department_textbox.Text);
    cmd.Parameters.AddWithValue("@Function", this.Function_textbox.Text);
    cmd.Parameters.AddWithValue("@Staffcode", this.Staffcode_textbox.Text);
    cmd.Parameters.AddWithValue("@Password", this.Password_textbox.Text);

    cmd.ExecuteNonQuery();

}

for reserved keywords use tilde mark and also you need to set connection for the command object

对于保留的关键字,使用倾斜标记,还需要为命令对象设置连接