Access SQL Server database class from self-written ADO. Net [VB. NET]

Source: Internet
Author: User

All those who have worked on Database Development know that developing a database system requires a good design to make our work smooth. In the outline design, we need to access the database, obviously, developing a class to access data is an important implementation basis for our code. How can we develop such a class? The following code is a wrinkle. It can be said that it is not very mature, but it plays a major role in the process of database development.

I started my database system for the first time. I am not experienced in analysis and design. The following classes are not suitable for your needs. They are an encapsulation of data access, it is an embodiment of reusable code. I believe that after you read my code, you will write a more powerful class to repeat your data! I am happy to learn from you. If you write a powerful class, please do not forget to share your happiness with me! Let me know your code too! Haha ^_^

Imports system. Data
Imports system. Data. sqlclient

Public class sqldb
Private servername as string
Private databasename as string
Private username as string
Private userpassword as string
Private CNN as sqlconnection

'Constructor
Public sub new ()
Mybase. New ()
Servername = "localhost"
Username = "sa"
Userpassword = "sa"
End sub

Public sub new (byval server as string, byval database as string, byval UID as string, byval PWD as string)
Servername = Server
Databasename = Database
Username = uid
Userpassword = pwd
End sub

Public Function open (byref MSG as string) as Boolean
CNN = new sqlconnection

Dim cnnstr as string = "Data Source =" + servername + "; initial catalog =" + databasename + "; user id =" + username + "; Pwd =" + userpassword
CNN. connectionstring = cnnstr

Try
CNN. open ()
Catch ex as exception
MSG = ex. Message

Return false
End try

Return true

End Function

Public Function selectdb (byval SQL as string, byref MSG as string) as Dataset
Dim ds as Dataset
DS = new dataset
Try
If CNN. State = connectionstate. Closed then
CNN. open ()
End if

Dim da As sqldataadapter = new sqldataadapter (SQL, CNN)
Da. Fill (DS)
CNN. Close ()
Catch ex as exception
MSG = ex. Message
DS = nothing
Return DS
End try

Return DS
End Function

Public Function upadatedb (byval SQL as string, byref MSG as string) as Boolean
Dim CMA as sqlcommand = new sqlcommand
Try
If CNN. State = connectionstate. Closed then
CNN. open ()
End if

CMA. Connection = CNN
CMA. commandtext = SQL
CMA. commandtype = commandtype. Text
CMA. executenonquery ()
CNN. Close ()
Catch ex as exception
MSG = ex. Message
Return false
End try

Return true
End Function
End Class

When designing this class, I only consider my own usage, because I don't know much about the stored procedure, and I have never written much about it, let alone it! However, we strive to use this class for the next time we develop the database, so that we can use ADO. Net for convenience!

                                   

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.