Top Ad unit 728 × 90

Thêm, xóa, sửa CSDL dùng Web Service trong ASP.Net

1. Tạo data base
mo-ta-csdl-demo-web-service

2. Tạo web services
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
SqlConnection con;
SqlDataAdapter da;
SqlCommand cmd;
DataSet ds;
[WebMethod]
public DataSet GetAccount()
{
    con = new SqlConnection("server=.; database=AccountDB; integrated security = true;");
    da = new SqlDataAdapter("Select * From NguoiDung", con);
    ds = new DataSet();
    da.Fill(ds);
    return ds;
}
[WebMethod]
public void InsertAccount(string mand, string hoten, string tendangnhap, string matkhau, bool gioitinh, string email)
{
    con = new SqlConnection("server=.; database=AccountDB; integrated security = true;");
    con.Open();
    cmd = new SqlCommand("INSERT INTO NguoiDung VALUES('" + mand + "', '" + hoten + "', '" + tendangnhap + "', '" + matkhau + "', '" + gioitinh + "', '" + email + "')", con);
    cmd.ExecuteNonQuery();
    con.Close();
}
[WebMethod]
public void UpdateAccount(string mand, string hoten, string tendangnhap, string matkhau, bool gioitinh, string email)
{
    con = new SqlConnection("server=.; database=AccountDB; integrated security = true;");
    con.Open();
    cmd = new SqlCommand("Update NguoiDung Set HoTen='" + hoten + "', TenDangNhap='" + tendangnhap + "', MatKhau='" + matkhau + "', GioiTinh='" + gioitinh + "', Email='" + email + "' Where MaND='" + mand + "'", con);
    cmd.ExecuteNonQuery();
    con.Close();
}
[WebMethod]
public void DeleteAccount(int mand)
{
    con = new SqlConnection("server=.; database=AccountDB; integrated security = true;");
    con.Open();
    cmd = new SqlCommand("Delete From NguoiDung Where MaND='" + mand + "'", con);
    cmd.ExecuteNonQuery();
    con.Close();
}
3.Gọi web services
Lấy dữ liệu dùng Web Service
Trong sự kiện PageLoad mình sẽ thực hiện chức năng lấy dữ liệu lên bằng cách khai báo đối tượng Web Service và sử dụng hàm GetAccount() từ đó đổ vào trong GridView (gvNguoiDung) qua DataSource.
1
2
3
4
5
6
7
8
9
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        account.Service1 sv1 = new account.Service1();
        gvNguoiDung.DataSource = sv1.GetAccount();
        gvNguoiDung.DataBind();
    }
}
Thêm mới 1 dòng
Giao-dien-demo-them-xoa-sua
1
2
3
4
5
6
7
8
9
10
11
12
protected void btThem_Click(object sender, EventArgs e)
{
    account.Service1 sv1 = new account.Service1();
    string mand = txtMaNguoiDung.Text;
    string hoten = txtHovaTen.Text;
    string tendn = txtTenDangNhap.Text;
    string matkhau = txtMatKhau.Text;
    bool gioitinh = bool.Parse(radioGioiTinh.SelectedValue);
    string email = txtEmail.Text;
    sv1.InsertAccount(mand, hoten, tendn, matkhau, gioitinh, email);
Thêm, xóa, sửa CSDL dùng Web Service trong ASP.Net Reviewed by 20Me Reviews on 09:45 Rating: 5
Powered By Blogger, Share by Star Tuan

Biểu mẫu liên hệ

Tên

Email *

Thông báo *

Được tạo bởi Blogger.