Net connection SQL database code Daquan

Source: Internet
Author: User

I. SQL server connection code example

  ODBC

Standard Security:

"Driver = {SQL Server}; server = aron1; database = pubs; uid = sa; Pwd = asdasd ;"

  Trusted connection:

"Driver = {SQL Server}; server = aron1; database = pubs; trusted_connection = yes ;"

  Prompt for username and password:

  Oconn. properties ("prompt") = adpromptalways
Oconn. Open "driver = {SQL Server}; server = aron1; database = pubs ;"

  Oledb, oledbconnection (. NET)

  Standard Security:

"Provider = sqloledb; Data Source = aron1; initial catalog = pubs; user id = sa; Password = asdasd ;"

  Trusted connection:

  "Provider = sqloledb; Data Source = aron1; initial catalog = pubs; Integrated Security = sspi ;"
(Use servername \ InstanceName as data source to use an specifik sqlserver instance, only sqlserver2000)
Prompt for username and password:
Oconn. provider = "sqloledb"
Oconn. properties ("prompt") = adpromptalways
Oconn. Open "Data Source = aron1; initial catalog = pubs ;"

  Connect via an IP Address:

  "Provider = sqloledb; Data Source = 190.190.200.100, 1433; network library = dbmssocn; initial catalog = pubs; user id = sa; Password = asdasd ;"
(Dbmssocn = TCP/IP instead of named pipes, at the end of the data source is the port to use (1433 is the default ))
Sqlconnection (. NET)

  Standard Security:

"Data Source = aron1; initial catalog = pubs; user id = sa; Password = asdasd ;"

  Trusted connection:

"Data Source = aron1; initial catalog = pubs; Integrated Security = sspi ;"
(Use servername \ InstanceName as data source to use an specifik sqlserver instance, only sqlserver2000)

  Connect via an IP Address:

"Data Source = 190.190.200.100, 1433; network library = dbmssocn; initial catalog = pubs; user id = sa; Password = asdasd ;"
(Dbmssocn = TCP/IP instead of named pipes, at the end of the data source is the port to use (1433 is the default ))
Declare the sqlconnection:

  II. C # connect to the Code instance

Using system. Data. sqlclient;
Sqlconnection osqlconn = new sqlconnection ();
Osqlconn. connectionstring = "My connectionstring ";
Osqlconn. open ();

  Iii. Connecting VB. NET to SQL database code instance

Imports system. Data. sqlclient
Dim osqlconn as sqlconnection = new sqlconnection ()
Osqlconn. connectionstring = "My connectionstring"
Osqlconn. open ()

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.