2012-06-13 4 views
0

아무에게도 VB2010 Express에서 ADO.Net을 통해 데이터베이스 연결을 추가 할 수있는 소스 코드를 제공 할 수 있습니까? 데이터베이스 필드를 추가, 업데이트, 삭제, 검색 및 수정하는 모든 명령을 포함합니다. 누군가가 저에게 소스 코드로 작은 프로토 타입 작업 모델을 제공 할 수 있다면 정말 도움이 될 것입니다.VB2010에서 ADO.Net을 통한 데이터베이스 연결

+0

확인 : http://evry1falls.freevar.com/VBNet/index.html 여기

는 도움이 될 수도 작은 샘플 조각입니다 –

답변

0

ADO.NET은 SQL 쿼리를 기반으로합니다. 따라서 CRUD (작성, 읽기, 업데이트, 삭제)에 대한 작업은 SQL-Language입니다 (쿼리 구문은 사용하는 데이터베이스에 따라 약간의 차이가있을 수 있습니다).

연결은 System.Data 네임 스페이스에서 IDbConnection, IDbCommand, IDbDataAdapter, IDbDataParameterIDbTransaction 인터페이스를 구현하는 전문 업체 엔티티를 사용합니다.

다른 데이터베이스 제공 업체 (예 : Microsoft SQL Server, Oracle, mySQl, OleDb, ODBC 등)가 있습니다. 일부는 .NET Framework (.NET Framework) (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 
관련 문제