返回首页
当前位置: 主页 > 编程语言 > C#教程 >

NET连接SQL数据库代码大全

时间:2016-11-03 15:42来源:电脑教程学习网 www.etwiki.cn 编辑:admin

一、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()

顶一下
(0)
0%
踩一下
(0)
0%
标签(Tag):SQL数据库
------分隔线----------------------------
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:点击我更换图片
推荐内容