我是新手,我正试图在.vb中使用Visual Studio 2013创建应用程序 这个应用程序适用于使用SQl SERVER EXPRESS女士制作的本地数据库(书籍,Cd,Dvd等),我想知道我是否可以通过APP重新加载DatagridView和我的数据集。使用App将列添加到我的数据库。事实上,我试图找到一种方法在我的数据网格视图中显示这个新列,我不想总是在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
但它确实有效。
我的窗口可以在哪里形成ALTER 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:对不起我的英语,这不是我的母语。
答案 0 :(得分:0)
我不是专业人士,但想尝试一下。我使用了 WithEvents 语句。
请尝试以下代码。我假设你正在以两种形式编写代码吗? Form7 和表单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
答案 1 :(得分:0)
所以我找到了办法!如果有人想知道,这是一个例子: 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”(= Column)和“typede”(= type)是三个文本框的名称