0

我有一个学校作业,我们要在我们从老师那里得到的代码中填写一些 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
4

1 回答 1

0

您的 CREATE TABLE 和 DROP TABLE 语句不太正确。如果您有 SQL Server Management Studio,则可以使用它来查看如何创建这些语句:右键单击“对象资源管理器”窗格中的现有表并选择“将表编写为...”->“DROP 和 CREATE to”- >“新查询编辑器窗口”。你会得到类似的东西

USE [testing]
GO

/****** Object:  Table [dbo].[testTable]    Script Date: 11/21/2012 20:35:02 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[testTable]') AND type in (N'U'))
DROP TABLE [dbo].[testTable]
GO

USE [testing]
GO

/****** Object:  Table [dbo].[testTable]    Script Date: 11/21/2012 20:35:02 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[testTable](
    [number] [int] NULL,
    [str] [nchar](10) NULL,
    [tim] [time](7) NULL
) ON [PRIMARY]

GO

您应该能够从中挑选出您需要的零件。

要执行 DROP/CREATE 语句,请使用SqlCommand.ExecuteNonQuery 方法

编辑:

因此,您的 FyllDb 方法将更像这样:

Public Sub FyllDb()
    Dim sqlCmd As New SqlCommand
    Dim sqlConn = New SqlConnection("Server=.\SQLEXPRESS;Database=testing;Trusted_Connection=True;")
    sqlCmd.Connection = sqlConn

    sqlCmd.CommandText = "IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[testTable2]') AND type in (N'U')) DROP TABLE [dbo].[testTable2]"

    Try
        sqlConn.Open()
        sqlCmd.ExecuteNonQuery()
    Catch ex As Exception
        MsgBox("Error deleting table: " & ex.Message)
    Finally
        sqlConn.Close()
    End Try

    sqlCmd.CommandText = "CREATE TABLE [dbo].[testTable2]( [number] [int] NULL, [str] [nchar](10) NULL, [tim] [time](7) NULL) ON [PRIMARY]"

    Try
        sqlConn.Open()
        sqlCmd.ExecuteNonQuery()
    Catch ex As ArgumentException
        MsgBox("Error creating table: " & ex.Message)
    Finally
        sqlConn.Close()
    End Try

End Sub

-- 经测试可与 VS2012/SQL Server 2008 R2 Express/Windows 7 x64 一起使用。

请根据您的表格定义进行调整。

于 2012-11-21T20:45:28.620 回答