我有一个学校作业,我们要在我们从老师那里得到的代码中填写一些 SQL 语句。我们还将树不同的表变成一个已经存在的数据库。当我尝试创建一个新类来添加这些表时,我认为我做错了什么。这些表从未创建过。为什么?!
数据库连接:
Public Class ClassConnection
'Mot citrix
Dim Koppling As New System.Data.SqlClient.SqlConnection("Initial Catalog=EmbeddedVB; Data Source=LUiis02; Integrated Security=true")
'Mot access
'Dim Koppling As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.jet.OLEDB.4.0;Data source=Poster.mdb")
'lokalt mot SQL-server
'Dim Koppling As New System.Data.SqlClient.SqlConnection("Initial Catalog=Testar;Data Source=(local); Integrated Security=true;")
Public ReadOnly Property ReturneraKoppling() As SqlClient.SqlConnection
Get
Return Koppling
End Get
End Property
Sub open()
Koppling.Open()
End Sub
Sub close()
Koppling.Close()
End Sub
End Class
表(用于创建)
Public Class Tables
Private mySqlConnection As New ClassConnection
Private myAdapter As SqlDataAdapter
Private mySqlCommand As New SqlCommand
Private ds As DataSet
Private gammaltPnr As String
Public Sub fyllDB()
mySqlCommand.Connection = mySqlConnection.ReturneraKoppling()
mySqlCommand.CommandType = CommandType.Text
mySqlConnection.open()
mySqlCommand.CommandText = "CREATE TABLE IF NOT EXIST Kurs4918(" &
"kursnamn char(30) NOT NULL PRIMARY KEY " &
")"
myAdapter = New SqlDataAdapter(mySqlCommand)
mySqlCommand.ExecuteNonQuery()
mySqlConnection.close()
mySqlConnection.open()
mySqlCommand.CommandText = "CREATE TABLE IF NOT EXIST Elev4918 (" &
"pnr char(11) NOT NULL PRIMARY KEY, " &
"fornamn char(20), " &
"efternamn char(20), " &
"ort char(15) " &
") "
myAdapter = New SqlDataAdapter(mySqlCommand)
mySqlCommand.ExecuteNonQuery()
mySqlConnection.close()
mySqlConnection.open()
mySqlCommand.CommandText = "CREATE TABLE IF NOT EXIST KursElev4918(" &
"pnr char(11) NOT NULL PRIMARY KEY, " &
"kursnamn char(30) " &
") "
myAdapter = New SqlDataAdapter(mySqlCommand)
mySqlCommand.ExecuteNonQuery()
mySqlConnection.close()
End Sub
启动 Form1.vb (这应该从表中运行 fyllDB() 对吗?....
Public Class frmMain
Inherits System.Windows.Forms.Form
Private db As New Tables
Private Sub fill()
db.fyllDB()
End Sub