2012-06-13 50 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