Esempio n. 1
0
        /// <summary>
        /// 批量插入数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGo_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show("确认批量插入数据?", "逆天友情提醒", MessageBoxButtons.YesNo);

            if (result == DialogResult.Yes)
            {
                var typeStr   = shopMenuType.SelectedItem as string;
                var shopModel = shopType.SelectedItem as ShopModel;
                if (!string.IsNullOrEmpty(typeStr) && shopModel != null)
                {
                    ShopMenuModel model = new ShopMenuModel()
                    {
                        MType = typeStr, MShopId = shopModel.SId, MCityId = tempModel.CityId
                    };
                    bool b = ShopMenuHelper.GetInsertCount(tempModel.ConnStr, path, model);
                    if (b)
                    {
                        ShowLog("批量插入成功!");
                    }
                    else
                    {
                        ShowLog("批量插入失败!");
                    }
                }
                else
                {
                    ShowLog("请选择分类!");
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 插入数据库
        /// </summary>
        /// <param name="connStr">连接字符串</param>
        /// <param name="path">文件路径</param>
        /// <param name="model">超市菜单模型</param>
        /// <returns></returns>
        public static bool GetInsertCount(string connStr, string path, ShopMenuModel model)
        {
            if (model == null)
            {
                return(false);
            }
            var dics = GetDictionary(path);

            using (var conn = new SqlConnection(connStr))
            {
                conn.Open();
                string sql = "insert into ShopMenu values(@MName,@MPrice,@MType,@MShopId,@MCityId,0,@MCreateTime)";
                using (var cmd = new SqlCommand(sql, conn))
                {
                    cmd.Transaction = conn.BeginTransaction();
                    try
                    {
                        foreach (var item in dics)
                        {
                            var pms = new SqlParameter[]
                            {
                                new SqlParameter("@MName", item.Key),
                                new SqlParameter("@MPrice", item.Value),
                                new SqlParameter("@MType", model.MType),
                                new SqlParameter("@MShopId", model.MShopId),
                                new SqlParameter("@MCityId", model.MCityId),
                                new SqlParameter("@MCreateTime", DateTime.Now)
                            };
                            cmd.Parameters.AddRange(pms);
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                        }
                        cmd.Transaction.Commit();
                    }
                    catch
                    {
                        cmd.Transaction.Rollback();
                        return(false);
                    }
                }
            }
            return(true);
        }