C#连接mysql数据库插入数据后获取自增长主键ID值

时间:2020-12-27 13:53:08

From: http://blog.csdn.net/zbc496218/article/details/51082983

    MySqlConnection conn = new MySqlConnection("连接数据库字符串");  
    conn.Open();  
    MySqlCommand mycmd = new MySqlCommand();  
    mycmd.Connection = conn;  
    mycmd.CommandType = CommandType.Text;  
    mycmd.CommandText = "insert into product(productname) values ("产品名称");";  
    mycmd.ExecuteNonQuery();  
    //获取插入后的数据ID  
    long newid = mycmd.LastInsertedId;