Reputation: 13
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.
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();
}
}
}
}
}
}
Upvotes: 1
Views: 12132
Reputation: 63095
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
Upvotes: 1
Reputation: 98810
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]
As a general recomendation, don't use reserved keywords for your identifiers and object names in your database.
Upvotes: 2