如何连接到本地 SQL Server 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8395481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:32:39  来源:igfitidea点击:

How to connect to local SQL Server database?

asp.netsqlsql-serverdatabasedatabase-connection

提问by user1082821

I am creating a web application/website using ASP.net in visual studio 2010. We have our basic website and I even created a SQL Server database which is in the App_Datafolder of my web application folder.

我正在 Visual Studio 2010 中使用 ASP.net 创建一个 Web 应用程序/网站。我们有我们的基本网站,我什至在App_Data我的 Web 应用程序文件夹的文件夹中创建了一个 SQL Server 数据库。

I created tables and a few procedures, but I do not know how to have my web forms or their controller (C#) classes access the tables. Below is my rough setup to access it. I don't know what to set the string to equal. The database is in webapplication1/App_Data/database.mdf.

我创建了表和一些过程,但我不知道如何让我的 Web 表单或其控制器 (C#) 类访问这些表。以下是我访问它的粗略设置。我不知道如何将字符串设置为相等。数据库在webapplication1/App_Data/database.mdf.

The file I want to access it is webapplication/App_Code/DataConnect.cs. What should the string equal. What do I need to do to test it?

我要访问的文件是webapplication/App_Code/DataConnect.cs. 字符串应该等于什么。我需要做什么来测试它?

{ 
SqlConnection _sqlConn = null;
string _connectionString = ?
 _sqlConn2 = new SqlConnection(_connectionString);
 _sqlConn.Open();
}

回答by adatapost

You may use following connection string.

您可以使用以下连接字符串。

string _connectionString =@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"

You can also add the connection string into web.config's connectionString section and later use it in code.

您还可以将连接字符串添加到 web.config 的 connectionString 部分,然后在代码中使用它。

<connectionStrings>
  <add name="CnStr" 
       connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True" 
       providerName="System.Data.SqlClient"/>
</connectionStrings>

To retrieve connectionString from web.config

从 web.config 检索 connectionString

string _connectionString=System.Configuration.ConfigurationManager.ConnectionStrings["CnStr"].ConnectionString;

回答by Code Scratcher

You can manually write connection string into your code...

您可以手动将连接字符串写入代码...

string strcon = @"Data Source=SERVERNAME; Initial Catalog=DATABASENAME; Integrated Security=True";

OR

或者

Follow below steps to connect local SQL Server database...

按照以下步骤连接本地 SQL Server 数据库...

  1. Go to View > Server Explorer/Database Explorer
  2. Right click on Data Connections > Add Connection...
  3. Select Server name, Choose authentication type, Select your created database.
  4. Test your connection and then OK.
  5. Right click on database > Properties and use connection string...
  1. 转到查看 > 服务器资源管理器/数据库资源管理器
  2. 右键单击数据连接> 添加连接...
  3. 选择服务器名称,选择身份验证类型,选择您创建的数据库。
  4. 测试您的连接,然后确定。
  5. 右键单击数据库> 属性并使用连接字符串...

Check below link for more understanding....

查看以下链接以获取更多理解....

回答by imzrh

the connection string just like

连接字符串就像

string _connectionString =@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True"

if you have any connection string problem, please refer to http://www.connectionstrings.com/

如果您有任何连接字符串问题,请参考http://www.connectionstrings.com/

回答by Elias Hossain

You can try with below ways:

您可以尝试以下方法:

string _connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename="+ 
        Server.MapPath("~/App_Data") 
        +@"\database.mdf;Integrated Security=True;User Instance=True"

Or

或者

string _connectionString =@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\database.mdf;Integrated Security=True;User Instance=True"

回答by user2431623

string _connectionString =@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True"

string _connectionString =@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True"