Microsoft Visual C# 2010 - Adding Data to Local Database

后端 未结 4 1768
南方客
南方客 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 09:41

    Well, if you want a quick, almost close to the wire code like the way you used to have with PHP, the following code should work.

    var conn = new SqlConnection("Your Connection String");
    var command = conn.CreateCommand();
    command.CommandText = "insert into sessions (id, name) values (@id, @name)";
    command.Parameters.AddWithValue("@id", "");
    command.Parameters.AddWithValue("@name", "test");
    conn.Open();
    command.ExecuteNonQuery();
    command.Dispose();
    conn.Close();
    

    In the long run, it would be better if you get accustomed to one of the data-related / ORM frameworks such as Entity Framework, NHibernate and the likes. That would really help a lot in data manipulation and make your life a whole lot easier.

提交回复
热议问题