How to insert new record in database using c#?

You can create new user and password with this code. Credentials will be set for users. You can use this code in role based projects. You can add many users by placing this code in for-loop or calling this method multiple times. I hope it will be useful for you all.

How to insert new record in database using c#?
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;
return true;


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: