Visual Studio: 当数据库结构发生变化时重新加载数据集和数据网格
Visual Studio: Reload Dataset and Datagrid when there's structural change in Database
我是新手,我正在尝试使用 Visual Studio 2013 在 .vb 中创建一个应用程序
此应用程序可与 SQl SERVER EXPRESS 女士制作的本地数据库(我拥有的书籍、Cd、Dvd 等)配合使用,我想知道我是否可以找到一种方法通过该应用程序重新加载我的 DatagridView 和我的数据集当我使用应用程序向我的数据库添加一列时。事实上,我正试图找到一种方法在我的数据网格视图中显示这个新列,我不想总是在 VS 中,因为我想在小型笔记本电脑上使用我的应用程序。
好吧,我不知道从哪里开始,所以我尝试了这个:
Imports System.Data.SqlClient
...
Private Sub Form8_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cn As New SqlConnection("Data Source=PC-ME\SQLEXPRESS;Initial Catalog=Database;Integrated Security=True")
Dim cmd As SqlCommand = cn.CreateCommand()
cmd.CommandText = "Select * FROM CONSULTE"
Dim adapter As New SqlDataAdapter()
Dim ds As New DataSet()
adapter.SelectCommand = cmd
cn.Open()
adapter.Fill(ds, "CONSULTE")
cn.Close()
CONSULTEDataGridView.DataSource = ds
CONSULTEDataGridView.DataMember = "CONSULTE"
End Sub
但是没用。
我的 windows 表单,我可以在其中更改 TABLE:
Imports System.Data.SqlClient
Public Class Form7
Dim cn As New SqlConnection("Data Source=PC-ME\SQLEXPRESS;Initial Catalog=Database;Integrated Security=True")
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Private Sub Form7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cmd.Connection = cn
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If table.Text <> "" And colonne.Text <> "" And typede.Text <> "" Then
cn.Open()
cmd.CommandText = "ALTER TABLE " & table.Text & " ADD " & colonne.Text & " " & typede.Text & ""
cmd.ExecuteNonQuery()
cn.Close()
table.Text = ""
colonne.Text = ""
typede.Text = ""
Else
MsgBox("Vous devez remplir les trois champs!")
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If table.Text <> "" And colonne.Text <> "" Then
cn.Open()
cmd.CommandText = "ALTER TABLE " & table.Text & " DROP COLUMN " & colonne.Text & ""
cmd.ExecuteNonQuery()
cn.Close()
table.Text = ""
colonne.Text = ""
Else
MsgBox("Vous devez remplir correctement les deux premiers champs!")
End If
End Sub
End Class
感谢您的帮助!
PS:对不起我的英语,不是我的母语
我不是专业人士,但想尝试一下。我使用了 WithEvents 语句。
请尝试以下代码。我假设,您正在以两种形式编写代码,对吗? Form7 和 Form 8
在表格 7 中,执行此操作
Imports System.Data.SqlClient
Public Class Form7
...
Public Event LoadDataGridView(sender As System.Object)
Private Sub Form7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cmd.Connection = cn
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If table.Text <> "" And colonne.Text <> "" And typede.Text <> "" Then
--- Your stuff here. After completion of code, following event will be raised
RaiseEvent LoadDataGridView(sender)
Else
MsgBox("Vous devez remplir les trois champs!")
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If table.Text <> "" And colonne.Text <> "" Then
-- Your stuff here. After completion of code, following event will be raised
RaiseEvent LoadDataGridView(sender)
Else
MsgBox("Vous devez remplir correctement les deux premiers champs!")
End If
End Sub
End Class
现在,
在表格 8 中,执行此操作
Imports System.Data.SqlClient
Public Class Form8
Private WithEvents form7 As New Form7
Private Sub Form8_Load(sender As Object, e As EventArgs) Handles MyBase.Load
-- You stuff
End Sub
Private Sub form7_LoadDataGridView(sender As Object) Handles form7.LoadDataGridView
'Call load procedure to reload datagridview
Form8_Load(Nothing,Nothing)
End Sub
End Class
所以我找到了一个方法!如果有人想知道,这里有一个例子:
Datagridview1 是我在 form1
上的 datagridview 的名称
表格 1:
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cn As New SqlConnection("Data Source=PC-ME\SQLEXPRESS;Initial Catalog=BDD;Integrated Security=True")
cn.Open()
Dim cmd As SqlCommand = cn.CreateCommand()
Dim adapter As New SqlDataAdapter()
Dim ds As New DataSet()
Dim sql As String
sql = "SELECT * FROM CONSULTE"
adapter.SelectCommand = New SqlCommand(sql, cn)
adapter.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
End Sub
End Class
表格 3,我可以在其中添加或删除列:
Imports System.Data.SqlClient
Public Class Form3
Dim cn As New SqlConnection("Data Source=PC-ME\SQLEXPRESS;Initial Catalog=BDD;Integrated Security=True")
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cmd.Connection = cn
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If table.Text <> "" And colonne.Text <> "" And typede.Text <> "" Then
cn.Open()
cmd.CommandText = "ALTER TABLE " & table.Text & " ADD " & colonne.Text & " " & typede.Text & ""
cmd.ExecuteNonQuery()
cn.Close()
table.Text = ""
colonne.Text = ""
typede.Text = ""
Else
MsgBox("Vous devez remplir les trois champs!")
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If table.Text <> "" And colonne.Text <> "" Then
cn.Open()
cmd.CommandText = "ALTER TABLE " & table.Text & " DROP COLUMN " & colonne.Text & ""
cmd.ExecuteNonQuery()
cn.Close()
table.Text = ""
colonne.Text = ""
Else
MsgBox("Vous devez remplir correctement les deux premiers champs!")
End If
End Sub
End Class
其中"table"(=Table)、"colonne"(=列)和"typede"(=类型)是三个文本框的名称
我是新手,我正在尝试使用 Visual Studio 2013 在 .vb 中创建一个应用程序 此应用程序可与 SQl SERVER EXPRESS 女士制作的本地数据库(我拥有的书籍、Cd、Dvd 等)配合使用,我想知道我是否可以找到一种方法通过该应用程序重新加载我的 DatagridView 和我的数据集当我使用应用程序向我的数据库添加一列时。事实上,我正试图找到一种方法在我的数据网格视图中显示这个新列,我不想总是在 VS 中,因为我想在小型笔记本电脑上使用我的应用程序。
好吧,我不知道从哪里开始,所以我尝试了这个:
Imports System.Data.SqlClient
...
Private Sub Form8_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cn As New SqlConnection("Data Source=PC-ME\SQLEXPRESS;Initial Catalog=Database;Integrated Security=True")
Dim cmd As SqlCommand = cn.CreateCommand()
cmd.CommandText = "Select * FROM CONSULTE"
Dim adapter As New SqlDataAdapter()
Dim ds As New DataSet()
adapter.SelectCommand = cmd
cn.Open()
adapter.Fill(ds, "CONSULTE")
cn.Close()
CONSULTEDataGridView.DataSource = ds
CONSULTEDataGridView.DataMember = "CONSULTE"
End Sub
但是没用。
我的 windows 表单,我可以在其中更改 TABLE:
Imports System.Data.SqlClient
Public Class Form7
Dim cn As New SqlConnection("Data Source=PC-ME\SQLEXPRESS;Initial Catalog=Database;Integrated Security=True")
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Private Sub Form7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cmd.Connection = cn
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If table.Text <> "" And colonne.Text <> "" And typede.Text <> "" Then
cn.Open()
cmd.CommandText = "ALTER TABLE " & table.Text & " ADD " & colonne.Text & " " & typede.Text & ""
cmd.ExecuteNonQuery()
cn.Close()
table.Text = ""
colonne.Text = ""
typede.Text = ""
Else
MsgBox("Vous devez remplir les trois champs!")
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If table.Text <> "" And colonne.Text <> "" Then
cn.Open()
cmd.CommandText = "ALTER TABLE " & table.Text & " DROP COLUMN " & colonne.Text & ""
cmd.ExecuteNonQuery()
cn.Close()
table.Text = ""
colonne.Text = ""
Else
MsgBox("Vous devez remplir correctement les deux premiers champs!")
End If
End Sub
End Class
感谢您的帮助!
PS:对不起我的英语,不是我的母语
我不是专业人士,但想尝试一下。我使用了 WithEvents 语句。
请尝试以下代码。我假设,您正在以两种形式编写代码,对吗? Form7 和 Form 8
在表格 7 中,执行此操作
Imports System.Data.SqlClient
Public Class Form7
...
Public Event LoadDataGridView(sender As System.Object)
Private Sub Form7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cmd.Connection = cn
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If table.Text <> "" And colonne.Text <> "" And typede.Text <> "" Then
--- Your stuff here. After completion of code, following event will be raised
RaiseEvent LoadDataGridView(sender)
Else
MsgBox("Vous devez remplir les trois champs!")
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If table.Text <> "" And colonne.Text <> "" Then
-- Your stuff here. After completion of code, following event will be raised
RaiseEvent LoadDataGridView(sender)
Else
MsgBox("Vous devez remplir correctement les deux premiers champs!")
End If
End Sub
End Class
现在, 在表格 8 中,执行此操作
Imports System.Data.SqlClient
Public Class Form8
Private WithEvents form7 As New Form7
Private Sub Form8_Load(sender As Object, e As EventArgs) Handles MyBase.Load
-- You stuff
End Sub
Private Sub form7_LoadDataGridView(sender As Object) Handles form7.LoadDataGridView
'Call load procedure to reload datagridview
Form8_Load(Nothing,Nothing)
End Sub
End Class
所以我找到了一个方法!如果有人想知道,这里有一个例子: Datagridview1 是我在 form1
上的 datagridview 的名称表格 1:
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cn As New SqlConnection("Data Source=PC-ME\SQLEXPRESS;Initial Catalog=BDD;Integrated Security=True")
cn.Open()
Dim cmd As SqlCommand = cn.CreateCommand()
Dim adapter As New SqlDataAdapter()
Dim ds As New DataSet()
Dim sql As String
sql = "SELECT * FROM CONSULTE"
adapter.SelectCommand = New SqlCommand(sql, cn)
adapter.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
End Sub
End Class
表格 3,我可以在其中添加或删除列:
Imports System.Data.SqlClient
Public Class Form3
Dim cn As New SqlConnection("Data Source=PC-ME\SQLEXPRESS;Initial Catalog=BDD;Integrated Security=True")
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cmd.Connection = cn
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If table.Text <> "" And colonne.Text <> "" And typede.Text <> "" Then
cn.Open()
cmd.CommandText = "ALTER TABLE " & table.Text & " ADD " & colonne.Text & " " & typede.Text & ""
cmd.ExecuteNonQuery()
cn.Close()
table.Text = ""
colonne.Text = ""
typede.Text = ""
Else
MsgBox("Vous devez remplir les trois champs!")
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If table.Text <> "" And colonne.Text <> "" Then
cn.Open()
cmd.CommandText = "ALTER TABLE " & table.Text & " DROP COLUMN " & colonne.Text & ""
cmd.ExecuteNonQuery()
cn.Close()
table.Text = ""
colonne.Text = ""
Else
MsgBox("Vous devez remplir correctement les deux premiers champs!")
End If
End Sub
End Class
其中"table"(=Table)、"colonne"(=列)和"typede"(=类型)是三个文本框的名称