Share registration form in vb.net with database,

redpeacock368

New member
#vb.net #database #Registration Mẫu #form ## Cách tạo biểu mẫu đăng ký trong VB.NET với cơ sở dữ liệu

Trong hướng dẫn này, tôi sẽ chỉ cho bạn cách tạo biểu mẫu đăng ký trong VB.NET bằng cơ sở dữ liệu.Chúng tôi sẽ sử dụng Khung thực thể ADO.NET để kết nối với cơ sở dữ liệu SQL Server.

### 1. Tạo một dự án mới

Đầu tiên, chúng ta cần tạo một dự án mới trong Visual Studio.Để làm điều này, hãy mở Visual Studio và chọn tệp **> mới> Dự án **.Trong hộp thoại ** New Project **, chọn ** Visual Basic> Windows Forms Ứng dụng ** và nhấp vào ** OK **.

### 2. Thêm cơ sở dữ liệu

Tiếp theo, chúng tôi cần thêm một cơ sở dữ liệu vào dự án của chúng tôi.Để thực hiện việc này, nhấp chuột phải vào ** Giải pháp Explorer ** và chọn ** Thêm> Mục mới **.Trong hộp thoại ** Thêm mục mới **, chọn ** cơ sở dữ liệu> mô hình dữ liệu thực thể ado.net ** và nhấp ** Thêm **.

Trong ** Trình hướng dẫn mô hình dữ liệu thực thể **, chọn ** MỚI ** và nhấp vào ** Tiếp theo **.Trong hộp thoại ** Chọn cơ sở dữ liệu **, chọn ** Microsoft SQL Server ** và nhấp vào ** Tiếp theo **.

Trong hộp thoại ** chuỗi kết nối **, hãy nhập chuỗi kết nối vào cơ sở dữ liệu của bạn.Bạn có thể tìm thấy chuỗi kết nối trong hộp thoại ** Máy chủ ** trong SQL Server Management Studio.

Nhấp vào ** Tiếp theo ** và chọn các bảng mà bạn muốn đưa vào mô hình của mình.Nhấp vào ** Kết thúc ** để tạo mô hình.

### 3. Tạo biểu mẫu

Bây giờ chúng ta cần tạo biểu mẫu cho mẫu đăng ký của chúng tôi.Để làm điều này, kéo một hình thức ** ** từ hộp công cụ ** ** lên bề mặt ** thiết kế **.

Nhấp đúp vào biểu mẫu để mở Trình thiết kế mẫu ** **.Trong cửa sổ ** Thuộc tính **, đặt thuộc tính ** văn bản ** thành ** Mẫu đăng ký **.

### 4. Thêm các điều khiển

Chúng ta cần thêm các điều khiển sau vào biểu mẫu:

*A ** Nhãn ** Điều khiển cho tên người dùng
*A ** Hộp văn bản ** Điều khiển cho tên người dùng
*A ** Nhãn ** Điều khiển cho mật khẩu
*A ** Hộp văn bản ** Điều khiển cho mật khẩu
*Nút ** ** Điều khiển để gửi biểu mẫu

Để thêm điều khiển, hãy kéo nó từ hộp công cụ ** ** lên bề mặt ** Thiết kế **.

### 5. Liên kết các điều khiển với mô hình

Bây giờ chúng ta cần liên kết các điều khiển với mô hình.Để thực hiện việc này, nhấp chuột phải vào biểu mẫu ** ** trong ** Giải pháp Explorer ** và chọn ** Thêm> Mục hiện có **.Trong hộp thoại ** Thêm mục hiện có **, chọn tệp **. EDMX ** cho mô hình của bạn và nhấp vào ** Thêm **.

Tệp **. EDMX ** sẽ được thêm vào dự án của bạn.Trong cửa sổ ** thuộc tính **, đặt thuộc tính ** dataContext ** của biểu mẫu thành tệp **. Edmx **.

Bây giờ chúng ta cần liên kết các điều khiển với mô hình.Để làm điều này, nhấp đúp vào biểu mẫu ** ** trong ** Giải pháp Explorer ** để mở Trình thiết kế mẫu ** **.Trong cửa sổ ** Thuộc tính **, đặt thuộc tính ** DataBindings ** của mỗi điều khiển thành thuộc tính tương ứng trong mô hình.

### 6. Viết mã

Bây giờ chúng ta cần viết mã cho mẫu đăng ký của chúng tôi.Mã sau đây hiển thị mã cho nút ** Gửi **:

`` `VBNet
Sub Sub Subbutton_Click (Người gửi dưới dạng Đối tượng, E As As EventArgs) xử lý Subbutton.Click

'Xác thực đầu vào.
Nếu không isvalidinput () thì
trở lại
Kết thúc nếu

'Tạo một người dùng mới.
Người dùng dim là người dùng mới ()
user.username = usernametextbox.text
user.password = passwordTextBox.text

'Lưu người dùng vào cơ sở dữ liệu.
Bối cảnh mờ như myContext mới ()
bối cảnh.users.add (người dùng)
bối cảnh.savechanges ()

'Chuyển hướng người dùng đến trang đăng nhập.
Forms.login.show ()
Kết thúc phụ

Chức năng riêng isvalidinput () là boolean

'Kiểm tra xem tên người dùng có trống không.
Nếu usernametextbox.text = ""
=======================================
#vb.net #database #Registration form #form ## How to Create a Registration Form in VB.NET with Database

In this tutorial, I will show you how to create a registration form in VB.NET with a database. We will use the ADO.NET Entity Framework to connect to a SQL Server database.

### 1. Create a New Project

First, we need to create a new project in Visual Studio. To do this, open Visual Studio and select **File > New > Project**. In the **New Project** dialog box, select **Visual Basic > Windows Forms Application** and click **OK**.

### 2. Add a Database

Next, we need to add a database to our project. To do this, right-click on the **Solution Explorer** and select **Add > New Item**. In the **Add New Item** dialog box, select **Database > ADO.NET Entity Data Model** and click **Add**.

In the **Entity Data Model Wizard**, select **New** and click **Next**. In the **Choose a Database** dialog box, select **Microsoft SQL Server** and click **Next**.

In the **Connection String** dialog box, enter the connection string to your database. You can find the connection string in the **Server Properties** dialog box in SQL Server Management Studio.

Click **Next** and select the tables that you want to include in your model. Click **Finish** to create the model.

### 3. Create the Form

Now we need to create the form for our registration form. To do this, drag a **Form** from the **Toolbox** onto the **Design** surface.

Double-click on the form to open the **Form Designer**. In the **Properties** window, set the **Text** property to **Registration Form**.

### 4. Add the Controls

We need to add the following controls to the form:

* A **Label** control for the username
* A **TextBox** control for the username
* A **Label** control for the password
* A **TextBox** control for the password
* A **Button** control for submitting the form

To add a control, drag it from the **Toolbox** onto the **Design** surface.

### 5. Bind the Controls to the Model

Now we need to bind the controls to the model. To do this, right-click on the **form** in the **Solution Explorer** and select **Add > Existing Item**. In the **Add Existing Item** dialog box, select the **.edmx** file for your model and click **Add**.

The **.edmx** file will be added to your project. In the **Properties** window, set the **DataContext** property of the form to the **.edmx** file.

Now we need to bind the controls to the model. To do this, double-click on the **form** in the **Solution Explorer** to open the **Form Designer**. In the **Properties** window, set the **DataBindings** property of each control to the corresponding property in the model.

### 6. Write the Code

Now we need to write the code for our registration form. The following code shows the code for the **Submit** button:

```vbnet
Private Sub SubmitButton_Click(sender As Object, e As EventArgs) Handles SubmitButton.Click

' Validate the input.
If Not IsValidInput() Then
return
End If

' Create a new user.
Dim user As New User()
user.Username = UsernameTextBox.Text
user.Password = PasswordTextBox.Text

' Save the user to the database.
Dim context As New MyContext()
context.Users.Add(user)
context.SaveChanges()

' Redirect the user to the login page.
Forms.Login.Show()
End Sub

Private Function IsValidInput() As Boolean

' Check if the username is empty.
If UsernameTextBox.Text = ""
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top