从 C# 应用程序到 Ms Access 的数据传输不正确

Data from c# application into Ms Access transferring incorrectly

...
using System.Data;
using System.Data.OleDb;

namespace accessloginapp
{
    public partial class Ramen : Form
    {
        private OleDbConnection connection = new OleDbConnection();

        public Ramen()
        {
            InitializeComponent();
            connection.ConnectionString =
                @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\...\Users.accdb;Persist Security Info=False;";
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            try{
                connection.Open();

                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                command.CommandText =
                    "insert into userdata (Username,[Password]) values('" +
                    txt_Username + "','" + txt_Password + "')";

                command.ExecuteNonQuery();
                MessageBox.Show("Users added and saved");
                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex);
            }
        }
    }
}

对不起,如果我不太了解,我对此还很陌生。当我在我的应用程序中保存用户名和密码等数据时,数据将按照我输入的内容插入,但会添加文本,示例:我会发送要插入的用户名 "Mark",但是当我去查看我的数据库,它被输入为 "System.Windows.Forms.TextBox, Text: Mark"。如何将其更改为仅插入我输入的用户名?

您需要使用 Text 属性 文本框控件,以获取实际存储的文本:-

 command.CommandText = "insert into userdata (Username,[Password]) 
           values('" + txt_Username.Text + "','" + txt_Password.Text + "')";

除此之外,请注意您的查询是针对 SQL Injection attack 开放的。

因此,您应该使用类似这样的参数化查询:-

command.CommandText = "insert into userdata (Username,[Password]) 
               values(?,?)";
command.Parameters.Add("?",OleDbType.VarChar,20).Value = txt_Username.Text;

并类似地为 @Password 添加参数。