在C#中,向表person插入一条数据(表person包括两列:id和name),使用MySqlParameter定义表中各列的值。
static void Main(string[] args) { string connectionString = "Server=‘localhost‘;Database=‘test‘;UId=‘root‘;Password=‘123456‘"; MySqlConnection mycon = new MySqlConnection(connectionString); mycon.Open(); string str = "insert into person(id,name) values(@id,@name)"; MySqlCommand cmd = new MySqlCommand(str, mycon); cmd.CommandType = CommandType.Text; MySqlParameter p1 = new MySqlParameter("@id", MySqlDbType.Int32); p1.Value = 1; MySqlParameter p2 = new MySqlParameter("@name", MySqlDbType.VarChar); p2.Value = "Dave"; cmd.Parameters.Add(p1); cmd.Parameters.Add(p2); if (cmd.ExecuteNonQuery() > 0) { Console.WriteLine("数据插入成功!"); } Console.ReadLine(); mycon.Close(); }