/// <summary> /// 获得中奖人 /// </summary> /// <param name="store">参与摇奖的对象( k - 摇奖对象标记, v - 摇奖对象中奖概率)</param> /// <param name="custom">自定义随机公式</param> /// <param name="seed">随机种子</param> /// <returns>中奖对象标记</returns> public static T GetWinner <T> (ListKV <T, int> store, CustomRandomFormula custom = null, int seed = -1) { int jackpot = 1; for (int i = 0; i < store.Count; i++) { jackpot += store[i].Val; } int target = GetRandom(0, jackpot, custom, seed); Debug.LogFormat("[Dice] random : {0}", target); for (int i = 0; i < store.Count; i++) { int _minVal = i == 0 ? 0 : store[i - 1].Val; int _maxVal = 0; for (int j = 0; j <= i; j++) { _maxVal += store[j].Val; } if (target >= _minVal && target < _maxVal) { return(store[i].Key); } } throw new System.Exception("获取摇奖结果失败,请检查"); }
void get() { try { SqlConnection con = new SqlConnection(strcon); if (con.State == ConnectionState.Closed) { con.Open(); } //truy vấn và cập nhật dữ liệu SqlCommand cmd = new SqlCommand("SELECT * from Tour WHERE Matour='" + TxtMT.Text.Trim() + "';", con); // SqlDataAdapte sử dụng để điền vào DataSet và cập nhật cơ sở dữ liệu SQL Server SqlDataAdapter da = new SqlDataAdapter(cmd); // Hỗ trợ tạo bảng có phân trang sắp xếp nhiều cột DataTable dt = new DataTable(); da.Fill(dt); if (dt.Rows.Count >= 1) { Txtten.Text = dt.Rows[0]["Tentour"].ToString(); Txtdate.Text = dt.Rows[0]["NgayDI"].ToString(); TextSN.Text = dt.Rows[0]["SoNgay"].ToString(); TextDem.Text = dt.Rows[0]["SoDem"].ToString().Trim(); Textmin.Text = dt.Rows[0]["Min"].ToString().Trim(); Textmax.Text = dt.Rows[0]["Max"].ToString().Trim(); Textgiamgia.Text = dt.Rows[0]["GiamGia"].ToString().Trim(); TextGC.Text = dt.Rows[0]["GhiChu"].ToString(); TextTT.Text = dt.Rows[0]["GiaTour"].ToString(); DropPT.SelectedValue = dt.Rows[0]["PhuongTien"].ToString().Trim(); DropKS.SelectedValue = dt.Rows[0]["Hotel"].ToString().Trim(); DropMien.SelectedValue = dt.Rows[0]["Mien"].ToString().Trim(); ListKV.ClearSelection(); string[] genre = dt.Rows[0]["KhuVuc"].ToString().Trim().Split(','); for (int i = 0; i < genre.Length; i++) { for (int j = 0; j < ListKV.Items.Count; j++) { if (ListKV.Items[j].ToString() == genre[i]) { ListKV.Items[j].Selected = true; } } } Max = Convert.ToInt32(dt.Rows[0]["Max"].ToString().Trim()); GG = Convert.ToInt32(dt.Rows[0]["GiamGia"].ToString().Trim()); TT = Max - GG; Img = dt.Rows[0]["Hinh"].ToString(); } else { Response.Write("<script>alert('Không tìm thấy mã');</script>"); } } catch (Exception ex) { } }
/// <summary> /// 获得中奖人 /// </summary> /// <param name="store">参与摇奖的对象(的概率)</param> /// <param name="custom">自定义随机公式</param> /// <param name="seed">随机种子</param> /// <returns>中奖对象的索引</returns> public static int GetWinner(int[] store, CustomRandomFormula custom = null, int seed = -1) { ListKV <int, int> _store = new ListKV <int, int> (); for (int i = 0; i < store.Length; i++) { _store.Add(i, store[i]); } return(GetWinner(_store, custom, seed)); }
void add() { try { // Khai báo khu vực string kv = ""; foreach (int i in ListKV.GetSelectedIndices()) { kv = kv + ListKV.Items[i] + ","; } kv = kv.Remove(kv.Length - 1); //Khai báo hình string filepath = "~/hinh/"; string filename = Path.GetFileName(FileUpload1.PostedFile.FileName); FileUpload1.SaveAs(Server.MapPath("hinh/" + filename)); filepath = "~/hinh/" + filename; SqlConnection con = new SqlConnection(strcon); if (con.State == ConnectionState.Closed) { con.Open(); } //Thực thi truy vấn SqlCommand cmd = new SqlCommand("INSERT INTO Tour(Matour,Tentour,KhuVuc,Mien,Hotel,NgayDi,PhuongTien,SoNgay,SoDem,Min,GhiChu,Max,GiamGia,GiaTour,Hinh) values(@Matour,@Tentour,@KhuVuc,@Mien,@Hotel,@NgayDi,@PhuongTien,@SoNgay,@SoDem,@Min,@GhiChu,@Max,@GiamGia,@GiaTour,@Hinh)", con); cmd.Parameters.AddWithValue("@Matour", TxtMT.Text.Trim()); cmd.Parameters.AddWithValue("@Tentour", Txtten.Text.Trim()); cmd.Parameters.AddWithValue("@KhuVuc", kv); cmd.Parameters.AddWithValue("@Mien", DropMien.SelectedItem.Value); cmd.Parameters.AddWithValue("@Hotel", DropKS.SelectedItem.Value); cmd.Parameters.AddWithValue("@NgayDi", Txtdate.Text.Trim()); cmd.Parameters.AddWithValue("@PhuongTien", DropPT.SelectedItem.Value); cmd.Parameters.AddWithValue("@SoNgay", TextSN.Text.Trim()); cmd.Parameters.AddWithValue("@SoDem", TextDem.Text.Trim()); cmd.Parameters.AddWithValue("@Min", Textmin.Text.Trim()); cmd.Parameters.AddWithValue("@GhiChu", TextGC.Text.Trim()); cmd.Parameters.AddWithValue("@Max", Textmax.Text.Trim()); cmd.Parameters.AddWithValue("@GiamGia", Textgiamgia.Text.Trim()); cmd.Parameters.AddWithValue("@GiaTour", TextTT.Text.Trim()); cmd.Parameters.AddWithValue("@Hinh", filepath); cmd.ExecuteNonQuery(); con.Close(); Response.Write("<script>alert('Thêm thành công.');</script>"); GridView1.DataBind(); } catch (Exception ex) { Response.Write("<script>alert('" + ex.Message + "');</script>"); } }
void update() { if (check()) { try { string kv = ""; foreach (int i in ListKV.GetSelectedIndices()) { kv = kv + ListKV.Items[i] + ","; } kv = kv.Remove(kv.Length - 1); //Dùng để lưu hình ảnh string filepath = "~/hinh/"; string filename = Path.GetFileName(FileUpload1.PostedFile.FileName); FileUpload1.SaveAs(Server.MapPath("hinh/" + filename)); filepath = "~/hinh/" + filename; SqlConnection con = new SqlConnection(strcon); if (con.State == ConnectionState.Closed) { con.Open(); } SqlCommand cmd = new SqlCommand("update Tour set Tentour=@Tentour, KhuVuc=@KhuVuc, Mien=@Mien, Hotel=@Hotel, NgayDi=@NgayDi, PhuongTien=@PhuongTien, SoNgay=@SoNgay, SoDem=@SoDem, Min=@Min, GhiChu=@GhiChu, Max=@Max, GiamGia=@GiamGia, GiaTour=@GiaTour, Hinh=@Hinh where Matour='" + TxtMT.Text.Trim() + "'", con); cmd.Parameters.AddWithValue("@Tentour", Txtten.Text.Trim()); cmd.Parameters.AddWithValue("@KhuVuc", kv); cmd.Parameters.AddWithValue("@Mien", DropMien.SelectedItem.Value); cmd.Parameters.AddWithValue("@Hotel", DropKS.SelectedItem.Value); cmd.Parameters.AddWithValue("@NgayDi", Txtdate.Text.Trim()); cmd.Parameters.AddWithValue("@PhuongTien", DropPT.SelectedItem.Value); cmd.Parameters.AddWithValue("@SoNgay", TextSN.Text.Trim()); cmd.Parameters.AddWithValue("@SoDem", TextDem.Text.Trim()); cmd.Parameters.AddWithValue("@Min", Textmin.Text.Trim()); cmd.Parameters.AddWithValue("@GhiChu", TextGC.Text.Trim()); cmd.Parameters.AddWithValue("@Max", Textmax.Text.Trim()); cmd.Parameters.AddWithValue("@GiamGia", Textgiamgia.Text.Trim()); cmd.Parameters.AddWithValue("@GiaTour", TextTT.Text.Trim()); cmd.Parameters.AddWithValue("@Hinh", filepath); cmd.ExecuteNonQuery(); con.Close(); GridView1.DataBind(); Response.Write("<script>alert('Cập nhập thành công');</script>"); } catch (Exception ex) { Response.Write("<script>alert('Thất bại');</script>"); } } }