有难度的问题·

princecharm 2003-08-09 04:44:01
不用SQL语句,只用纯VB的程序,怎么样才能改变表的结构?
用ADO.X要怎么做才行?
...全文
31 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
gpo2002 2003-08-09
  • 打赏
  • 举报
回复
DefinedSize Property
Indicates the stated maximum size of the column.


Settings and Return Values
Sets and returns a Long value that is the maximum length in characters of data values.


Remarks
The default value is zero (0).
This property is read-only for Column objects already appended to a collection.
lele2002 2003-08-09
  • 打赏
  • 举报
回复
TO:gpo2002(gpo2002)

讲得太复杂了
简单点

gpo2002 2003-08-09
  • 打赏
  • 举报
回复
DefinedSize Property Example (VB)
This example demonstrates the DefinedSize property of a Column. The code will redefine the size of the FirstName column of the Employees table of the Northwind database. Then, the change in the values of the FirstName Field of a Recordset based on the Employees table is displayed. Note that by default, the FirstName field becomes padded with spaces after you redefine the DefinedSize property.

' BeginDefinedSizeVB
Public Sub DefinedSizeX()

Dim rstEmployees As ADODB.Recordset
Dim catNorthwind As New ADOX.Catalog
Dim colFirstName As ADOX.Column
Dim colNewFirstName As New ADOX.Column
Dim aryFirstName() As String
Dim i As Integer
Dim strCnn As String

strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\Program Files\" & _
"Microsoft Office\Office\Samples\Northwind.mdb;"

' Open a Recordset for the Employees table.
Set rstEmployees = New ADODB.Recordset
rstEmployees.Open "Employees", strCnn, adOpenKeyset, , adCmdTable
ReDim aryFirstName(rstEmployees.RecordCount)

' Open a Catalog for the Northwind database,
' using same connection as rstEmployees
Set catNorthwind.ActiveConnection = rstEmployees.ActiveConnection

' Loop through the recordset displaying the contents
' of the FirstName field, the field's defined size,
' and its actual size.
' Also store FirstName values in aryFirstName array.
rstEmployees.MoveFirst
Debug.Print " "
Debug.Print "Original Defined Size and Actual Size"
i = 0
Do Until rstEmployees.EOF
Debug.Print "Employee name: " & rstEmployees!FirstName & _
" " & rstEmployees!LastName
Debug.Print " FirstName Defined size: " _
& rstEmployees!FirstName.DefinedSize
Debug.Print " FirstName Actual size: " & _
rstEmployees!FirstName.ActualSize
aryFirstName(i) = rstEmployees!FirstName
rstEmployees.MoveNext
i = i + 1
Loop
rstEmployees.Close

' Redefine the DefinedSize of FirstName in the catalog
Set colFirstName = catNorthwind.Tables("Employees").Columns("FirstName")
colNewFirstName.Name = colFirstName.Name
colNewFirstName.Type = colFirstName.Type
colNewFirstName.DefinedSize = colFirstName.DefinedSize + 1

' Append new FirstName column to catalog
catNorthwind.Tables("Employees").Columns.Delete colFirstName.Name
catNorthwind.Tables("Employees").Columns.Append colNewFirstName

' Open Employee table in Recordset for updating
rstEmployees.Open "Employees", catNorthwind.ActiveConnection, _
adOpenKeyset, adLockOptimistic, adCmdTable

' Loop through the recordset displaying the contents
' of the FirstName field, the field's defined size,
' and its actual size.
' Also restore FirstName values from aryFirstName.
rstEmployees.MoveFirst
Debug.Print " "
Debug.Print "New Defined Size and Actual Size"
i = 0
Do Until rstEmployees.EOF
rstEmployees!FirstName = aryFirstName(i)
Debug.Print "Employee name: " & rstEmployees!FirstName & _
" " & rstEmployees!LastName
Debug.Print " FirstName Defined size: " _
& rstEmployees!FirstName.DefinedSize
Debug.Print " FirstName Actual size: " & _
rstEmployees!FirstName.ActualSize
rstEmployees.MoveNext
i = i + 1
Loop
rstEmployees.Close

' Restore original FirstName column to catalog
catNorthwind.Tables("Employees").Columns.Delete colNewFirstName.Name
catNorthwind.Tables("Employees").Columns.Append colFirstName

' Restore original FirstName values to Employees table
rstEmployees.Open "Employees", catNorthwind.ActiveConnection, _
adOpenKeyset, adLockOptimistic, adCmdTable

rstEmployees.MoveFirst
i = 0
Do Until rstEmployees.EOF
rstEmployees!FirstName = aryFirstName(i)
rstEmployees.MoveNext
i = i + 1
Loop
rstEmployees.Close

Set catNorthwind = Nothing

End Sub
' EndDefinedSizeVB
princecharm 2003-08-09
  • 打赏
  • 举报
回复
就是想把表中字段的长度修改。
但是不用语句写。
用程序来实现。
飘零风 2003-08-09
  • 打赏
  • 举报
回复
对于 MS SQL SERVER,可以使用如下语句:

SqlCmd="alter table tablename alter column fieldname nvarchar(20)"
Conn.Execute SqlCmd

将表 tablename中的fieldname字段修改为长度为20的nvarchar型。
gpo2002 2003-08-09
  • 打赏
  • 举报
回复
再举个简单例子

'删除db1.mdb里面表ab的字段b
Sub DeleteFieldTest()

Dim cat As New ADOX.Catalog

cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\db1.mdb;"

cat.Tables.Item("ab").Columns.Delete ("b")

End Sub
gpo2002 2003-08-09
  • 打赏
  • 举报
回复
The following code demonstrates how to create a new foreign key. It assumes two tables (Customers and Orders) exist.

' BeginCreateKeyVB
Sub CreateKey()

Dim kyForeign As New ADOX.Key
Dim cat As New ADOX.Catalog

' Connect the catalog
cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\Program Files\Microsoft Office\" & _
"Office\Samples\Northwind.mdb;"

' Define the foreign key
kyForeign.Name = "CustOrder"
kyForeign.Type = adKeyForeign
kyForeign.RelatedTable = "Customers"
kyForeign.Columns.Append "CustomerId"
kyForeign.Columns("CustomerId").RelatedColumn = "CustomerId"
kyForeign.UpdateRule = adRICascade

' Append the foreign key
cat.Tables("Orders").Keys.Append kyForeign

'Delete the Key as this is a demonstration
cat.Tables("Orders").Keys.Delete kyForeign.Name
End Sub
' EndCreateKeyVB


1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧