MySql中MySqlParameter的用法

在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();
}

 

posted @ 2020-12-18 23:22  rainbow70626  阅读(1584)  评论(0编辑  收藏  举报