2013-02-20 65 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