public class vb.net to connect SQL Server

Source: Internet
Author: User
Tags exit dbase define function tostring trim

This article describes the public class vb.net that connects SQL Server.

*************************************************************************


'//start date: May 27, 2002


'//End Date: May 27, 2002


  '***********************************************************************


Option Explicit on


' Option Strict on


  '//***********************************************************************


Imports System.Data.SqlClient


  '//*************************************************************************


'//begin defined namespace


Namespace Sunerp.CommClass.Comm


'//defined Structure publicapplicationval


'//structure descriptoin


'//This structure accesses database server name (dbserver), database user ID (UserId),


'//database user password (userpwd), database name (Dbase), which provides parameters for the joined database.


'//In order to meet the needs of some simple queries, the extended variables commonly used table names (dbtable),


'//query field name (queryfieldname), query field value


'//begin define Structure publicapplicationval


Public Structure Publicapplicationval


' public dbserver, UserId userpwd Dbase account.


public DBServer as String


public UserId as String


public userpwd as String


public Dbase as String


public dbtable as String


public Queryfieldvale as String


public Queryfieldname as String


End Structure


'//end define Structure publicapplicationval


  '//********************************************************************


'//begin defined Class


Public Class Dbasebindcomm


  ''*****************************************************************


'//** defines a common function to connect to SQL Server database **//


'//This function creates a connection object to the specified database SqlConnection


'//This function has 1 parameters, Ipublicapplication is a public data structure publicapplicationval,


  


'//begin define Function linkedsqlserver


public Overloads Function linkedsqlserver () as System.Data.SqlClient.SqlConnection


Dim strconn as String


Dim Conn as New SqlConnection ()


strconn = "server=" & "ERP" & _


"; User id=" & "Erpsa" & _


";p assword=" & "Erpsa" & _


";d atabase=" & "Erpdata" & "
"

Try


conn = New SqlConnection (strconn)


Conn. Open ()


stateval = True


linkedsqlserver = conn


Catch ex as Exception


' MsgBox (ex. ToString)


stateval = False


Linkedsqlserver = Nothing


Exit Function


End Try


End Function


' End defined Linkedsqlserver


  ''***********************************************************************


'//** defines a common function to connect to SQL Server database **//


'//This function creates a connection object to the specified database SqlConnection


'//This function has 1 parameters, Ipublicapplication is a public data structure publicapplicationval,


'//begin define Function linkedsqlserver


public Overloads Function Linkedsqlserver (_


ByVal Ipublicapplication as Publicapplicationval) _


as System.Data.SqlClient.SqlConnection


Dim strconn as String


Dim Conn as New SqlConnection ()


strconn = "server=" & Ipublicapplication.dbserver & _


"; user id=" & Ipublicapplication.userid & _


";p assword=" & Ipublicapplication.userpwd & _


";d atabase=" & Ipublicapplication.dbase & ";"


Try


conn = New SqlConnection (strconn)


Conn. Open ()


stateval = True


linkedsqlserver = conn


Catch ex as Exception


' MsgBox (ex. ToString)


stateval = False


Linkedsqlserver = Nothing


Exit Function


End Try


End Function


' End defined Linkedsqlserver


  ''**************************************************************


' defines the ReadOnly property state of the class Dbasebindcomm (the status of the connection being created)


' True (creation succeeded), False (create failed)


Private Stateval as Boolean


Public ReadOnly Property State () as Boolean


Get


return Stateval


End Get


End Property


  


End Class


  


Public Class Organcomm


  


'//defined Structure publicorganidstructure


'//structure descriptoin


'//This structure accesses enterprise organization ID


'//Top level (DEPTTOPID), Level (Deptmidid), level two ID (DEPTLOWID)


'//begin define Structure publicorganidstructure


Public Structure publicorganidstructure


public depttopid as String


public Deptmidid as String


public Deptlowid as String


End Structure


'//end define Structure publicorganidstructure


  '//******************************************************************


'//** defines a common function to connect to SQL Server database **//


'//This function creates a connection object to the specified database SqlConnection


'//This function has 1 parameters, Ipublicapplication is a public data structure publicapplicationval,


'//begin define Function iparsedeptorgid


public Function Iparsedeptorgid (ByVal ideptorgid as String) as Publicorganidstructure


Dim Organid as New publicorganidstructure ()


Dim OrgID as String


  


Ideptorgid = Trim (ideptorgid)


OrgID = Microsoft.VisualBasic.Left (Trim (Ideptorgid), 12)


  


organid.depttopid = Microsoft.VisualBasic.Left (OrgID, 6)


Organid.deptmidid = Microsoft.VisualBasic.Right (OrgID, 6)


Organid.deptlowid = Microsoft.VisualBasic.Right (Ideptorgid, 6)


Iparsedeptorgid = Organid


End Function


'//end define Function iparsedeptorgid


End Class


End Namespace


  '*******************************************************************************

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.