dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online Membersnikhil
Padma
Nanda Kishore
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » General

How to insert new record in database using c#?


Posted Date:     Category: General    
Author: Member Level: Gold    Points: 7


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#?
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;
}
}
}





Did you like this resource? Share it with your friends and show your love!


Responses to "How to insert new record in database using c#?"

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

Feedbacks      

Post 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:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to free the COM object which holds references to resources in a timely manner?
    Previous Resource: How to check whether the record exists or not in the database while inserting new record using C#?
    Return to Resources
    Post New Resource
    Category: General


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.