简体   繁体   English

与SQL Server的基本连接

[英]Basic connection to sql server

I have read many tutorial and examples, but still can't connect to the sql server. 我已经阅读了许多教程和示例,但是仍然无法连接到sql服务器。

I'm using window authentication and this simple code (stripped down to highlight the connection part) 我正在使用窗口身份验证和此简单代码(向下突出显示连接部分)

    SqlConnection myConnection = new SqlConnection(@"Data Source=localhost;Initial Catalog=MyDatabase;Trusted_Connection=True;connection timeout=5");

    try
    {
    myConnection.Open();
    "connected"
    }
    catch(SqlException ex)
    {
    "Send curriculum to mcdonald" + ex.Message
    }

I've tried localhost\\sqlexpress, computername\\sqlexpress and a lot of other settings, the service are on, the database is there and reacheable from sql server management and so on. 我尝试了localhost \\ sqlexpress,computername \\ sqlexpress和许多其他设置,该服务已启用,数据库已存在并且可以从sql服务器管理中访问,等等。 It's the first time that I try to use sql server so probably I'm forgetting something fundamental, what must I check to be sure to make it work? 这是我第一次尝试使用sql server,因此我可能忘记了一些基本知识,必须检查哪些内容才能确保它能正常工作?

SqlConnection myConnection = new SqlConnection("Data Source=localhost\\InstanceName;Initial Catalog=MyDatabase;Trusted_Connection=True;connection timeout=5");

OR 要么

SqlConnection myConnection = new SqlConnection("Data Source=.\\InstanceName;Initial Catalog=MyDatabase;Trusted_Connection=True;connection timeout=5");

OR 要么

SqlConnection myConnection = new SqlConnection("Data Source=ServerName\\InstanceName;Initial Catalog=MyDatabase;Trusted_Connection=True;connection timeout=5");

After reading you comments try this.. 阅读您的评论后,请尝试此。

SqlConnection myConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=MyDatabase;Trusted_Connection=True;connection timeout=5");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM