简体   繁体   English

VB.net数据绑定

[英]VB.net data binding

Hey this is an homework assignment and I just cannot figure it out. 嘿,这是一项家庭作业,我无法弄清楚。 Any help would be great! 任何帮助将是巨大的!

Create a SQL Server database named "people" that has the following table. 创建一个具有下表的名为“ people”的SQL Server数据库。 Please use Visual Studio 2012 and create a local service based database 请使用Visual Studio 2012并创建一个基于本地服务的数据库

people_id int PK identity firstname varchar(30) lastname varchar(40) people_id int PK身份名varchar(30)姓varchar(40)

Create a Windows forms program that lists the id, firstname and lastname in a datagridview 创建一个Windows窗体程序,该程序在datagridview中列出ID,名字和姓氏

Provide functionality (in code using command object and parameters, no wizards) to add, update and delete from the database table and reflect the changes in the datagridview. 提供功能(在使用命令对象和参数的代码中,没有向导)来添加,更新和删除数据库表,并在datagridview中反映更改。

This is the code I already have. 这是我已经拥有的代码。 I can run my program and the data will show up in the datagridview, but whenever I go to add update or delete data nothing happens. 我可以运行我的程序,数据将显示在datagridview中,但是每当我去添加更新或删除数据时,都不会发生任何事情。 I am not throwing errors or anything. 我没有抛出任何错误。 I have no idea how to fix this, please help!! 我不知道如何解决这个问题,请帮忙!

Imports System.Data
Imports System.Data.SqlClient

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    Me.PeopleTableAdapter.Fill(Me.PeopleDataSet.people)
End Sub

Private Function getConnString() As String
    Return My.Settings.PeopleConnectionString.ToString()
End Function

Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
    Me.Close()
End Sub

Private Function getConnection() As String
    Return My.Settings.PeopleConnectionString.ToString()
End Function

Private Function getPeople(lName As String) As DataTable
    Dim sql As String
    Dim conn As SqlConnection
    Dim cmd As SqlCommand
    Dim da As SqlDataAdapter
    Dim dt As New DataTable
    Dim plName As New SqlParameter
    plName.ParameterName = "@lName"
    plName.Value = lName
    sql = "select * from people where lName = @lName"
    conn = New SqlConnection(getConnection())
    conn.Open()
    cmd = New SqlCommand(sql, conn)
    cmd.Parameters.Add(plName)
    da = New SqlDataAdapter(cmd)
    da.Fill(dt)
    conn.Close()
    Return dt
End Function

Private Sub AddPerson(fname As String, lname As String, person_ID As Integer)
    Dim sql As String
    Dim conn As SqlConnection
    Dim cmd As SqlCommand
    sql = "insert into people(fName, lName, person_ID) values("
    sql += "@fName, @lName, @person_ID)"
    conn = New SqlConnection(getConnection())
    conn.Open()
    cmd = New SqlCommand(sql, conn)
    cmd.Parameters.AddWithValue("@fName", fname)
    cmd.Parameters.AddWithValue("@lName", lname)
    cmd.Parameters.AddWithValue("@person_ID", person_ID)
    cmd.ExecuteNonQuery()
    conn.Close()
End Sub

Private Sub btnADD_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    Try
        AddPerson(txtfName.Text, txtlName.Text, txtpID.Text)
        MessageBox.Show("Added")
    Catch ex As Exception
        MessageBox.Show("Error" & ex.Message)
    End Try
End Sub

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    If DataGridView1.SelectedRows.Count > 0 Then
        Dim dr As DataGridViewRow = DataGridView1.SelectedRows(0)
        txtfName.Text = dr.Cells(1).Value.ToString()
        txtlName.Text = dr.Cells(2).Value.ToString()
        txtpID.Text = dr.Cells(0).Value.ToString()
    End If
End Sub

Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
    Dim sql As String
    Dim conn As SqlConnection
    Dim cmd As SqlCommand
    sql = "update people set fName=@fName, lName=@lName"
    sql += " where person_ID=@person_ID"
    conn = New SqlConnection(getConnection())
    conn.Open()
    cmd = New SqlCommand(sql, conn)
    cmd.Parameters.AddWithValue("@fName", txtfName.Text)
    cmd.Parameters.AddWithValue("@lName", txtlName.Text)
    cmd.Parameters.AddWithValue("@person_ID", txtpID.Text)
    cmd.ExecuteNonQuery()
    conn.Close()
End Sub

Private Sub btnView_Click(sender As Object, e As EventArgs) Handles btnView.Click
    Dim sql As String
    Dim conn As SqlConnection
    Dim cmd As SqlCommand
    sql = "delete from people where person_ID=@person_ID"
    conn = New SqlConnection(getConnection())
    conn.Open()
    cmd = New SqlCommand(sql, conn)
    cmd.Parameters.AddWithValue("@person_ID", txtpID.Text)
    cmd.ExecuteNonQuery()
    conn.Close()

End Sub
End Class

Sorry for I didn't have 50 reputation. 对不起,我没有50个声誉。

For any connection, u can not forecast the database is running or not, so you must type a try catch and return a message to user/ support to let them know what happen in this issue, for Exception type in vb, please check as below: Introduction to Exception Handling in Visual Basic .NET 对于任何连接,您都无法预测数据库是否正在运行,因此您必须键入try catch并向用户/支持人员返回一条消息,以使他们知道此问题的发生,对于vb中的异常类型,请检查以下内容: Visual Basic .NET中的异常处理简介

Please using 请使用

try{
    ' your action, connection or sql exe
    ' example using your update event
    Dim sql As String
    Dim conn As SqlConnection
    Dim cmd As SqlCommand
    sql = "update people set fName=@fName, lName=@lName"
    sql += " where person_ID=@person_ID"
    conn = New SqlConnection(getConnection())
    conn.Open()
    cmd = New SqlCommand(sql, conn)
    cmd.Parameters.AddWithValue("@fName", txtfName.Text)
    cmd.Parameters.AddWithValue("@lName", txtlName.Text)
    cmd.Parameters.AddWithValue("@person_ID", txtpID.Text)
    cmd.ExecuteNonQuery()
    conn.Close()
}catch(Exception e){
    ' print the error message from here to let u know the function/ event error or not
    ' check the exception message
    ' MsgBox(e.ToString())
    conn.Close()
} finally {
    conn.Close()
}

to check is that the button event was error or not. 检查按钮事件是否错误。

Also I always using 我也一直在用

MsgBox("Alert") 

to help me to check the function or event has been run or not. 帮助我检查功能或事件是否已运行。

Hope help. 希望对您有所帮助。

one more thing... Please type the connection as class so u don't have to type it in so many times. 还有一件事...请以类的形式键入连接,因此您不必多次键入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM