private void button1_Click_1(object sender, EventArgs e)
        {
            string connection = "datasource=localhost;username=root;password=shi123+;charset=utf8;database=sjz";

            MySqlConnection sqlConnection = new MySqlConnection(connection);
            try
            {

                sqlConnection.Open();


                string sql = null;

                IList list = ((BindingSource)dC.database()).List;//这地方是获取控件的datasource,控件绑定的就是DCB

                MySqlTransaction tx = sqlConnection.BeginTransaction(IsolationLevel.ReadCommitted);
                MySqlCommand command = new MySqlCommand();

                command.Connection = sqlConnection;
                command.Transaction = tx;
                for (int i = 0; i < list.Count; i++)
                {
                    sql = "INSERT INTO `CN_DCB` VALUES (" + "'" + ((DCB)list[i]).HSCZ + "'" + "," + "'" + ((DCB)list[i]).JIE + "'" + "," + "'" + ((DCB)list[i]).XI + "'" + "," + "'" + ((DCB)list[i]).TONG + "'" + "," + "'" + ((DCB)list[i]).J_JIE + "'" + "," + "'" + ((DCB)list[i]).ZU + "'" + "," + "'" + ((DCB)list[i]).HSCMC + "'" + "," + "'" + ((DCB)list[i]).XMMC + "'"+")";
                    command.CommandText = sql;
                    command.ExecuteNonQuery();
                }
                tx.Commit();
                MessageBox.Show("插入成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }


}

public class DCB
    {
        public string HSCZ { get; set; }
        public string JIE { get; set; }
        public string XI { get; set; }
        public string TONG { get; set; }
        public string J_JIE { get; set; }
        public string ZU { get; set; }
        public string HSCMC { get; set; }
        public string XMMC { get; set; }

    }

 

posted on 2018-11-27 23:14  xiaoshibushiyue  阅读(261)  评论(0)    收藏  举报