Dzwebs.Net

撰写电脑技术杂文十余年

NET连接SQL数据库代码大全

Admin | 2007-10-24 19:26:34 | 被阅次数 | 8956

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

  一、SQL Server 连接代码示例

  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:

  二、C#连接代码实例

  using System.Data.SqlClient;
  SqlConnection oSQLConn = new SqlConnection();
  oSQLConn.ConnectionString="my connectionstring";
  oSQLConn.Open();

  三、VB.NET连接SQL数据库代码实例

  Imports System.Data.SqlClient
  Dim oSQLConn As SqlConnection = New SqlConnection()
  oSQLConn.ConnectionString="my connectionstring"
  oSQLConn.Open()


该杂文来自: 网站开发杂文

上一篇:在NET中如何使用FreeTextBox

下一篇:使用NET如何实现文件下载(以流的形式输出下载)

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计