下面是我的项目代码。 希望您能够帮助我。 提前致谢。 :)
这是我的sqlcontrol代码
Imports System.Data
Imports System.Data.SqlClient
Public Class SQLControl
Public SQLCon As New SqlConnection With {.ConnectionString = "Server=xxx\SQLEXPRESS;Database=SQLTest;User=sa;Pwd=xxxx;"}
Public SQLCmd As SqlCommand 'allow us to fire query at the data base
Public SQLDA As SqlDataAdapter
Public SQLDataset As DataSet
Public dtable As New DataTable
Public bs As New BindingSource
'QUERY PARAMETERS
Public Params As New List(Of SqlParameter)
Public RecordCount As Integer
Public Exception As String
Public Function HasConnection() As Boolean
Try
SQLCon.Open()
SQLCon.Close()
Return True
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
Public Sub ExecQuery(ByVal Query As String)
Try
SQLCon.Open()
'CREATE SQL COMMAND
SQLCmd = New SqlCommand(Query, SQLCon)
'LOAD PARAMETER INTO SQL COMMAND
Params.ForEach(Sub(x) SQLCmd.Parameters.Add(x))
'CLEAR PARAMETER LIST
Params.Clear()
'EXCUTE COMMAND FILL MY DATASET
SQLDataset = New DataSet
'EXCUTE COMMAND WIHTOUT ADAPTER
SQLDA = New SqlDataAdapter(SQLCmd)
RecordCount = SQLDA.Fill(SQLDataset)
SQLCon.Close()
Catch ex As Exception
Exception = ex.Message
End Try
If SQLCon.State = ConnectionState.Open Then SQLCon.Close()
End Sub
Public Sub RunQuery(ByVal Query As String)
Try
SQLCon.Open()
SQLCmd = New SqlCommand(Query, SQLCon)
'LOAD SQL RECORDS FOR DATAGRID
SQLDA = New SqlDataAdapter(SQLCmd)
SQLDataset = New DataSet
SQLDA.Fill(SQLDataset)
SQLCon.Close()
Catch ex As Exception
MsgBox(ex.Message)
If SQLCon.State = ConnectionState.Open Then
SQLCon.Close()
End If
End Try
End Sub
Public Sub AddMember(ByVal PC As String, ByVal IP As String, ByVal Name As String, ByVal Email As String, ByVal Department As String, ByVal Location As String,
ByVal Model As String, ByVal Specs As String, ByVal Dt As String, ByVal Asset As String, ByVal Rent As String)
Try
Dim strInsert As String = "INSERT INTO MEMBERS (pc,ip,name,email,department,location,model,specs,date,asset,rent) " & _
"VALUES (" & _
"'" & PC & "'," & _
"'" & IP & "'," & _
"'" & Name & "'," & _
"'" & Email & "'," & _
"'" & Department & "'," & _
"'" & Location & "'," & _
"'" & Model & "'," & _
"'" & Specs & "'," & _
"'" & Dt & "'," & _
"'" & Asset & "'," & _
"'" & Rent & "')"
MsgBox(strInsert)
SQLCon.Open()
SQLCmd = New SqlCommand(strInsert, SQLCon)
SQLCmd.ExecuteNonQuery()
SQLCon.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
这是我的表格代码:
Public Class Form1
Private SQL As New SQLControl
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'EXECUTE QUERY AND POPULATE GRID
SQL.ExecQuery("SELECT * FROM members")
LoadGrid()
'DISABLE SAVE BUTTON
cmdSave.Enabled = False
End Sub
Private Sub LoadGrid()
'IF OUR DATA IS RETURNED AND POPULATE GRID & BUILD UPDATE COMMAND
If SQL.RecordCount > 0 Then
dgvData.DataSource = SQL.SQLDataset.Tables(0)
dgvData.Rows(0).Selected = True
SQL.SQLDA.UpdateCommand = New SqlClient.SqlCommandBuilder(SQL.SQLDA).GetUpdateCommand
End If
End Sub
Private Sub dgvData_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvData.CellValueChanged
cmdSave.Enabled = True
End Sub
Private Sub dgvData_RowsRemoved(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsRemovedEventArgs) Handles dgvData.RowsRemoved
cmdSave.Enabled = True
End Sub
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
'SAVE UPDATE TO THE DATA BASE
Try
SQL.SQLDA.Update(SQL.SQLDataset) ' TO DO: DATA VALIDATION
Catch ex As Exception
MsgBox("Already Exists")
End Try
'REFRESH GRID DATA
LoadGrid()
'DISABLE SAVE BUTTON
cmdSave.Enabled = False
End Sub
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
If Trim(txtPC.Text) = "" Then
MsgBox("Please fill out the pc name.")
Exit Sub
End If
If Trim(txtIP.Text) = "" Then
MsgBox("Please fill out the ip address.")
Exit Sub
End If
'Query for user
SQL.RunQuery("SELECT * FROM members WHERE members.PC = '" & txtPC.Text & "'")
If SQL.SQLDataset.Tables(0).Rows.Count > 0 Then
MsgBox("The name that you have enter enter is already exists")
Exit Sub
End If
SQL.RunQuery("SELECT * FROM members WHERE members.IP = '" & txtIP.Text & "'")
If SQL.SQLDataset.Tables(0).Rows.Count > 0 Then
MsgBox("The IP Address that you have enter is already exists!")
Exit Sub
End If
SQL.RunQuery("SELECT * FROM members WHERE members.Asset = '" & txtAsset.Text & "'")
If SQL.SQLDataset.Tables(0).Rows.Count > 0 Then
MsgBox("The Asset that you have enter is already exists")
Exit Sub
Else
CreateUser()
txtPC.Clear()
txtIP.Clear()
txtName.Clear()
txtEmail.Clear()
txtDepartment.Clear()
txtLocation.Clear()
txtModel.Clear()
txtSpecs.Clear()
txtDt.Clear()
txtAsset.Clear()
txtRent.Clear()
End If
End Sub
Public Sub CreateUser()
SQL.AddMember(txtPC.Text, txtIP.Text, txtName.Text, txtEmail.Text, txtDepartment.Text,
txtLocation.Text, txtModel.Text, txtSpecs.Text, txtDt.Text, txtAsset.Text, txtRent.Text)
End Sub
End Class
我不知道如何刷新datagridview
答案 0 :(得分:1)
这将有效。只是 复制并粘贴到您的加载表单。
Public Sub RefreshUserGrid()
' RUN QUERY
SQL.ExecQuery("SELECT * FROM members")
If SQL.SQLDataset.Tables.Count > 0 Then
dgvData.DataSource = SQL.SQLDataset.Tables(0)
dgvData.Rows(0).Selected = True
End If
End Sub
并在add命令中复制并粘贴此RefreshUserGrid()。