Microsoft Visual C# 2010 - Adding Data to Local Database

后端 未结 4 1763
南方客
南方客 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:46

    Here's some code that uses SQLServer to do a direct insert, although you'll need a connection string to your database.

    Include the SQL server database includes.

    using System.Data.SqlClient; 
    using System.Data.SqlTypes;
    

    . . .

    using (SqlConnection cn = new SqlConnection("XXXXX")) // must put a connection string to your database here
    {
        cn.Open();
        using (SqlCommand cmd = new SqlCommand("INSERT INTO Session(field1, field2) VALUES(@Value1, @Value2)"))
        {
            cmd.Parameters.AddWithValue("@Value1", 4);
            cmd.Parameters.AddWithValue("@Value2", "test");
            cmd.ExecuteNonQuery();
        }
    }
    

提交回复
热议问题