简体   繁体   中英

How do I insert values into existing rows in SQL server in default asp.net mvc project?

I have opened visual studio and created a new default mvc asp.net 4 project. And this is my database table data after adding few extra columns(that I want to use) as shown in the picture below.

在此处输入图片说明

Updated Question:

Basically I used a default mvc project and in the default database, I added few extra columns and also in my register view from account controller class, I added extra html content to let the users add extra information for the extra columns that I added like FirstName, LastName etc. Now the problem is that only my email and password is getting stored. So where do I add database code that tells that whatever the user enters in the form, must be added to the database.

Here is the form and here are the details that got added. In this picture I am clicking Register button.

在此处输入图片说明

Now in my database, only email and password got added.

在此处输入图片说明

Of course I only added html content for "FirstName", "LastName" etc. So where do I make these changes so that they get added to my database as well?

And I did add properties for my "FirstName" etc in my RegisterView model.

Odds are that you have to find where your ApplicationUser class is defined, and modify the properties there

It might look something like this:

public class ApplicationUser
{
    [Key]
    public int UserId { get; set; }
    public string UserName { get; set; }
}

To add a first name, simply add this

public string FirstName { get; set; }

you also have to actually populate the values in your user as you create it.

change this:

var user = new ApplicationUser { UserName = model.Email, Email = model.Email };

to this

var user = new ApplicationUser 
{ 
    UserName = model.Email, 
    Email = model.Email,
    FirstName = model.FirstName, 
    //etc...
};

If you want the data to be stored in the database, and you're going to write the query yourself as you're doing, then you'll need to do something like the following:

using(SqlConnection connection = new SqlConnection(connectionString))
{
    var command = new SqlCommand("INSERT INTO AspNetUsers (email, emailconfirmed, passwordhash, securitystamp, phonenumber, ..., firstname, lastname, gender, aboutuser) VALUES ('email@email.com', '1', a hash, securitystampValue, '555-666-5555', ..., 'Ingenious', 'hax', 1, 'some description';"))
    command.ExecuteNonQuery();
}

And you'll want to use paramaterized values as Tony Morris did in his post.

If you want to update the table with the new information, it's very similar.

using(SqlConnection connection = new SqlConnection(connectionString))
{
    var command = new SqlCommand("UPDATE AspNetUsers SET firstname = 'ingenious', lastname='hax', gender=1, aboutuser='some description about the user' WHERE firstname='ingenious';");
    command.ExecuteNonQuery();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM