none
在VB2005如何用代码实现数据库连接 RRS feed

答案

  • 数据库连接关键是数据库连接字符串写对了就没有问题
    下面是一个例子


    Dim ConnectionString As String = "Data Source=127.0.0.1;Initial Catalog=Northwind;User ID=sa;Password=sa的密码"
    Dim cn As System.Data.SqlClient.SqlConnection = New SqlClient.SqlConnection(ConnectionString)
    cn.Open()
    Dim ad As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter("select * from Orders;select * from Customers ", cn)
    Dim ds As Data.DataSet = New Data.DataSet()
    ad.Fill(ds)

    Dim myDataRelation As DataRelation
    myDataRelation = New DataRelation("CustOrd", ds.Tables(1).Columns("CustomerID"), ds.Tables(0).Columns("CustomerID"))
    ds.Relations.Add(myDataRelation)

    Dim d As DataGrid = New DataGrid()
    d.Width = 800
    d.Height = 500
    Me.Controls.Add(d)
    d.DataSource = ds
    d.DataMember = ds.Tables(1).TableName



    2008年12月12日 7:22
    版主