2013-02-20 41 views
-1

我想要做的是首先檢查表中是否已經存在某個列,如果不添加它。我想通過視覺基礎來實現這一點。如果有人花了一點時間來評論和簡要解釋每一步,我將不勝感激。通過VB添加SQL列的最簡單方法

+0

你知道SQL?如果是這樣的話,您可以使用ADO.NET將SQL語句硬編碼到您的VB.Net代碼 – 2013-02-20 02:21:00

+0

實際上,ado.net正是我必須使用的。請告訴 – morgred 2013-02-20 02:22:41

+0

您是否試圖編寫任何代碼? – 2013-02-20 02:24:18

回答

0

有兩種方法來確定一個列上存在:要麼嘗試使用它,並捕獲的錯誤,如果它不存在,或從數據庫中讀取的元數據看SQL Server: Extract Table Meta-Data (description, fields and their data types)

一旦你知道你需要要添加使用ALTER TABLE命令的列以將該列添加到表中。

+0

在VB中,您還可以打開一個空的記錄集並遍歷列集合。但是你當然可以編寫一個存儲過程來完成Peter Wooster的所有建議。請注意,從應用程序代碼中更改數據庫表通常不被視爲一種好的設計模式。 – 2013-02-20 02:48:11

+0

我已經知道,但教授不是那麼聰明:( – morgred 2013-02-20 02:50:37

0

這裏如果不 vb.net腳本來檢查如果柱存在,,創建它..

「」「摘要 ‘’」檢查,看是否有表中存在數據庫與否。 '' ' ''' 表名檢查 '' '連接字符串來連接到 ''' 與Access工作或SQL '' '

Public Function DoesTableExist(ByVal tblName As String, ByVal cnnStr As String) As Boolean 
    ' For Access Connection String, 
    ' use "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & 
    ' accessFilePathAndName 

' Open connection to the database 
Dim dbConn As New OleDbConnection(cnnStr) 
dbConn.Open() 

' Specify restriction to get table definition schema 
' For reference on GetSchema see: 
' http://msdn2.microsoft.com/en-us/library/ms254934(VS.80).aspx 

Dim restrictions(3) As String 
restrictions(2) = tblName 
Dim dbTbl As DataTable = dbConn.GetSchema("Tables", restrictions) 

If dbTbl.Rows.Count = 0 Then 
    'Table does not exist 
    DoesTableExist = False 
Else 
    'Table exists 
    DoesTableExist = True 
End If 

dbTbl.Dispose() 
dbConn.Close() 
dbConn.Dispose() 

端功能

' '' '''檢查表中是否存在字段。 「」「 ‘’」表名 ‘’檢查「字段名稱檢查 ‘’」連接字符串來連接到 ‘’「 ‘’」

公共功能DoesFieldExist(BYVAL tblName作爲串,_ BYVAL fldName作爲字符串_ BYVAL cnnStr作爲字符串)爲布爾 '對於Access連接字符串, ' 使用 「提供者= Microsoft.Jet.OLEDB.4.0;數據源=」 & 'accessFilePathAndName

' Open connection to the database 
Dim dbConn As New OleDbConnection(cnnStr) 
dbConn.Open() 
Dim dbTbl As New DataTable 

' Get the table definition loaded in a table adapter 
Dim strSql As String = "Select TOP 1 * from " & tblName 
Dim dbAdapater As New OleDbDataAdapter(strSql, dbConn) 
dbAdapater.Fill(dbTbl) 

' Get the index of the field name 
Dim i As Integer = dbTbl.Columns.IndexOf(fldName) 

If i = -1 Then 
    'Field is missing 
    DoesFieldExist = False 
Else 
    'Field is there 
    DoesFieldExist = True 
End If 

dbTbl.Dispose() 
dbConn.Close() 
dbConn.Dispose() 

端功能

0
Dim connString As String = "Data Source=NameOfMachine\InstanceofSQLServer;Initial Catalog=NameOfDataBase;Integrated Security=True" 
    Dim MyCol As String = "NameOfColumn" 
    Dim MyTable As String = "[NameOfTable]" ' or "[Name Of Table]" use brackets if table name contains spaces or other illegal Characters 
    Dim MySql As String = "IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS" & vbCrLf & 
"WHERE TABLE_NAME = '" & MyTable & "' AND COLUMN_NAME = '" & MyCol & "')" & vbCrLf & 
"BEGIN" & vbCrLf & 
"ALTER TABLE [dbo]." & MyTable & " ADD" & vbCrLf & "[" & MyCol & "] INT NULL ;" & vbCrLf & "END" 

    Try 
     ' MsgBox(MySql)- this msg box shows the Query so I can check for errors- Not required for code. 
     Dim dbConn = New SqlConnection(connString)' Note ConnString must be declared in the form class or within this Sub. Connstring is your connection string 
     Dim dbCmd = New SqlCommand(MySql, dbConn) 
     dbConn.Open() 
     dbCmd.ExecuteNonQuery() 
     'MessageBox.Show("Ready To Load Addendums") 

     dbConn.Close() 

    Catch ex As Exception 
     MsgBox("We've encountered an error;" & vbCrLf & ex.Message) 

    End Try 
+0

對不起,「MessageBox.show(」準備加載Addendums「)行也不是必需的。 – 2017-02-27 13:25:18

相關問題