如何將我的數據添加到我的數據庫?我使用訪問數據庫,在這一刻我有一個列表視圖,並從我的數據庫中獲取我的數據到我的列表視圖。我做了第二個表單應用程序,用於在這種情況下添加用戶。C#將數據保存到訪問數據庫
因此:我想用我製作的第二個表單將我的數據添加到我的數據庫中。
這是我的代碼。你能幫我麼?
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();
}
}
}
}
}
}
你得到任何異常或錯誤信息的連接? –