How to insert new record in database using c#? Explaination: Say I have one table as USER and I want to add user related data such as login , password, address, email , contactnumber , role then I can use following code snippet. It will create a new user with above mentioned details. As Role is assigned to the user, you can check role at the time of login and assign permissions based on different roles. Insert query is used to insert required values in the database.
public bool CreateNewUser(string login, string password, string address, string email, string contactNo , int role ) { string ConnString = GetConnString(); string sqlString = "INSERT INTO [User] ( Username, [Password], Address, Email, ContactNumber,Role ) VALUES(?, ?, ?, ?, ?, ?,?)"; using (OleDbConnection conn = new OleDbConnection(ConnString)) { using (OleDbCommand cmd = new OleDbCommand(sqlString, conn)) { cmd.CommandType = CommandType.Text; cmd.Parameters.Add("?Username", OleDbType.VarChar).Value = login; cmd.Parameters.Add("?Password", OleDbType.VarChar).Value = password; cmd.Parameters.Add("?Address", OleDbType.VarChar).Value = address; cmd.Parameters.Add("?Email", OleDbType.VarChar).Value = email; cmd.Parameters.Add("?ContactNumber", OleDbType.VarChar).Value = contactNo; cmd.Parameters.Add("?Role", OleDbType.Integer).Value = role; conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); conn.Dispose(); return true; } } }
|
No responses found. Be the first to respond...
|