Microsoft Visual C# 2010 - Adding Data to Local Database

后端 未结 4 1765
南方客
南方客 2021-01-27 08:58

I\'m coming over from PHP and am having a hard time with storing information into my newly created local database. I\'m using Microsoft Visual C# 2010 to help me learn and devel

4条回答
  •  醉梦人生
    2021-01-27 10:00

    It depends on your requirments, but for most situations, I would highly recommend you use Entity Framework or Linq to Sql data classes. You'd be much better off... go with the latter as a start... hope it helps.

    [Edited]

    If you want to see how easy an ORM can be:

    1. right-click on your project
    2. select Add New Item
    3. Choose Linq to Sql Data Classes
    4. When you've added it, you'll have a blank .dbml file
    5. Go to server explorer and add a connection to the sql db
    6. Drag and drop the tables wherever you like
    7. Start using the entities like this:

      using (DataClasses1DataContext db = new DataClasses1DataContext("Data Source=localhost\sqlexpress; Initial Catalog=myDBName; Integrated Security=true")) { IEnumerable citiesForUSA = db.Cities.Where(x => x.Country.Name == "United States");

      City city = new City();
      city.Name = "Metropolis";
      //etc
      db.Cities.InsertOnSubmit(city);
      db.SubmitChanges(); // <-- INSERT INTO completed
      
      //etc
      

      }

    Good luck!

    :-)

提交回复
热议问题