设为首页 收藏本站
查看: 567|回复: 0

[经验分享] SQL Server 2005数据库连接串

[复制链接]
YunVN网友  发表于 2015-7-4 12:23:18 |阅读模式
SQL Native Client ODBC Driver


  • 标准连接:

    "Driver={SQL Native Client};Server=Aron1;Database=pubs;UID=sa;PWD=asdasd;"
  • 信任连接:

    "Driver={SQL Native Client};Server=Aron1;Database=pubs;Trusted_Connection=yes;"
    等同于
    Integrated Security=SSPI equals Trusted_Connection=yes

  • 提示用户名和密码:

    oConn.Properties("Prompt") = adPromptAlways
    oConn.Open "Driver={SQL Native Client};Server=Aron1;DataBase=pubs;"
  • Enabling MARS (multiple active result sets):

    "Driver={SQL Native Client};Server=Aron1;Database=pubs;Trusted_Connection=yes;MARS_Connection=yes"
    等同于
    MultipleActiveResultSets=true equals MARS_Connection=yes


  • 通过网络发送数据时加密:

    "Driver={SQL Native Client};Server=Aron1;Database=pubs;Trusted_Connection=yes;Encrypt=yes"
  • 连接SQL实例时附加一个数据文件:

    "Driver={SQL Native Client};Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;"
       - 或 -
    "Driver={SQL Native Client};Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;"
       (use |DataDirectory| when your database file resides in the data directory)
    Why is the "Database" parameter needed? Answer: If the database was previously attached, SQL Server does not reattach it (it uses the attached database as the default for the connection).

SQL Native Client OLE DB Provider


  • Standard security:

    "Provider=SQLNCLI;Server=Aron1;Database=pubs;UID=sa;PWD=asdasd;"
  • Trusted connection:

    "Provider=SQLNCLI;Server=Aron1;Database=pubs;Trusted_Connection=yes;"
    Equivalents
    Integrated Security=SSPI equals Trusted_Connection=yes

  • Prompt for username and password:

    oConn.Properties("Prompt") = adPromptAlways
    oConn.Open "Provider=SQLNCLI;Server=Aron1;DataBase=pubs;"
  • Enabling MARS (multiple active result sets):

    "Provider=SQLNCLI;Server=Aron1;Database=pubs;Trusted_Connection=yes;MarsConn=yes"
    Equivalents
    MarsConn=yes equals MultipleActiveResultSets=true equals MARS_Connection=yes

  • Encrypt data sent over network:

    "Provider=SQLNCLI;Server=Aron1;Database=pubs;Trusted_Connection=yes;Encrypt=yes"
  • Attach a database file on connect to a local SQL Server Express instance:

    "Provider=SQLNCLI;Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;"
       - or -
    "Provider=SQLNCLI;Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;"
       (use |DataDirectory| when your database file resides in the data directory)
    Why is the "Database" parameter needed? Answer: If the database was previously attached, SQL Server does not reattach it (it uses the attached database as the default for the connection).

SqlConnection (.NET)


  • Standard Security:

    "Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;"
       - or -
    "Server=Aron1;Database=pubs;User ID=sa;Password=asdasd;Trusted_Connection=False"
       (both connection strings produces the same result)
  • Trusted Connection:

    "Data Source=Aron1;Initial Catalog=pubs;Integrated Security=SSPI;"
       - or -
    "Server=Aron1;Database=pubs;Trusted_Connection=True;"
       (both connection strings produces the same result)
    (use serverName\instanceName as Data Source to use an specifik SQLServer instance)

  • 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))

  • Enabling MARS (multiple active result sets):

    "Server=Aron1;Database=pubs;Trusted_Connection=True;MultipleActiveResultSets=true"
    Note! Use ADO.NET 2.0 for MARS functionality. MARS is not supported in ADO.NET 1.0 nor ADO.NET 1.1

  • Attach a database file on connect to a local SQL Server Express instance:

    "Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;"
       - or -
    "Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;"
       (use |DataDirectory| when your database file resides in the data directory)
    Why is the "Database" parameter needed? Answer: If the database was previously attached, SQL Server does not reattach it (it uses the attached database as the default for the connection).

  • Using "User Instance" on a local SQL Server Express instance:

    "Data Source=.\SQLExpress;integrated security=true;attachdbfilename=|DataDirectory|\mydb.mdf;user instance=true;"
    The "User Instance" functionality creates a new SQL Server instance on the fly during connect. This works only on a local SQL Server 2005 instance and only when connecting using windows authentication over local named pipes. The purpose is to be able to create a full rights SQL Server instance to a user with limited administrative rights on the computer. To enable the functionality: sp_configure 'user instances enabled','1' (0 to disable)

Context Connection - connecting to "self" from within your CLR stored prodedure/function


  • C#:

    using(SqlConnection connection = new SqlConnection("context connection=true"))
    {
        connection.Open();
        // Use the connection
    }
  • Visual Basic:

    Using connection as new SqlConnection("context connection=true")
        connection.Open()
        ' Use the connection
    End Using


    The context connection lets you execute Transact-SQL statements in the same context (connection) that your code was invoked in the first place.


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-83068-1-1.html 上篇帖子: sql server身份验证即sa用户名登录时,却出现以下错误提示"provider: 共享内存提供程序, error: 0 下篇帖子: 将数据库导入SQL Server
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表