2012-06-13 49 views
0

任何人都可以通過ADO.Net爲我提供在VB2010 Express中添加數據庫連接的源代碼。包括所有添加,更新,刪除,檢索和修改數據庫字段的命令。如果任何人都可以爲我提供一個帶有源代碼的小型原型工作模型,那將會非常有幫助。通過VB2010中的ADO.Net實現數據庫連接express

+0

檢查這本書: http://evry1falls.freevar.com/VBNet/index.html – 2012-08-19 22:19:31

回答

0

ADO.NET或多或少是基於SQL查詢的。因此,對於CRUD(創建,讀取,更新,刪除),操作查看SQL-Language(查詢語法可能會因您使用的數據庫而有所不同)。

的連接使用實現從System.Data命名空間的IDbConnectionIDbCommandIDbDataAdapterIDbDataParameterIDbTransaction接口專業提供商實體。

存在不同的數據庫提供者(例如Microsoft SQL Server,Oracle,mySQl,OleDb,ODBC等)。其中一些本地支持的.NET框架(MSSQL = System.Data.SqlClient命名空間,OleDb = System.Data.OleDb,ODBC = System.Data.Odbc命名空間),而其他人必須通過外部庫添加(如果你喜歡,你也可以編寫自己的數據庫提供者)。

使用IDBCommand對象(例如System.Data.SqlClient.SqlCommand對象),您可以定義SQL命令。

這裏是一個小樣本片段可能幫助:

Public Class Form1 

    Sub DBTest() 

     '** Values to store the database values in 
     Dim col1 As String = "", col2 As String = "" 

     '** Open a connection (change the connectionstring to an appropriate value 
     '** for your database or load it from a config file) 
     Using conn As New SqlClient.SqlConnection("YourConnectionString") 
     '** Open the connection 
     conn.Open() 
     '** Create a Command object 
     Using cmd As SqlClient.SqlCommand = conn.CreateCommand() 
      '** Set the command text (=> SQL Query) 
      cmd.CommandText = "SELECT ID, Col1, Col2 FROM YourTable WHERE ID = @ID" 
      '** Add parameters 
      cmd.Parameters.Add("@ID", SqlDbType.Int).Value = 100 '** Change to variable 
      '** Execute the value and get the reader object, since we are trying to 
      '** get a result from the query, for INSERT, UPDATE, DELETE use 
      '** "ExecuteNonQuery" method which returns an Integer 
      Using reader As SqlClient.SqlDataReader = cmd.ExecuteReader() 
       '** Check if the result has returned som results and read the first record 
       '** If you have multiple records execute the Read() method until it returns false 
       If reader.HasRows AndAlso reader.Read() Then 
        '** Read the values of the current columns 
        col1 = reader("col1") 
        col2 = reader("col2") 
       End If 
      End Using 
     End Using 

     Debug.Print("Col1={0},Col2={1}", col1, col2) 
     '** Close the connection 
     conn.Close() 
     End Using 
    End Sub 
End Class