使用 C# 连接到 SQL Server 2012 数据库 (Visual Studio 2012)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17265068/
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
Connect to SQL Server 2012 Database with C# (Visual Studio 2012)
提问by thefragileomen
Evening all,
晚上大家,
I'm trying to connect to a SQL Server 2012 database from C#. My connection settings when using SQL Server Management Studio are as below:-
我正在尝试从 C# 连接到 SQL Server 2012 数据库。我在使用 SQL Server Management Studio 时的连接设置如下:-
Server Type: Database Engine
Server Name: Paul-PC\SQLEXPRESS
Authentication: Windows Authentication
Username: Greyed out
Password: Greyed out
The name of the database I'm trying to connect to is "testDB".
我试图连接的数据库的名称是“testDB”。
Here's my code:
这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace DatabaseConnection
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection("server=localhost;" +
"Trusted_Connection=yes;" +
"database=testDB; " +
"connection timeout=30");
try
{
myConnection.Open();
MessageBox.Show("Well done!");
}
catch(SqlException ex)
{
MessageBox.Show("You failed!" + ex.Message);
}
}
}
}
Unfortunately, my code fails to connect with the following error:-
不幸的是,我的代码无法连接,出现以下错误:-
"You failed!A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections."
“您失败了!与 SQL Server 建立连接时发生与网络相关或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 ”
Any suggestions? SQL Server is running locally.
有什么建议?SQL Server 正在本地运行。
回答by Dennis Traub
Replacing server=localhostwith server=.\SQLEXPRESSmight do the job.
替换server=localhostwithserver=.\SQLEXPRESS可能会完成这项工作。
回答by Milen
In your connection string replace server=localhostwith "server = Paul-PC\\SQLEXPRESS;"
在您的连接字符串中替换server=localhost为“ server = Paul-PC\\SQLEXPRESS;”
回答by Ben Narube
Try:
尝试:
SqlConnection myConnection = new SqlConnection("Database=testDB;Server=Paul-PC\SQLEXPRESS;Integrated Security=True;connect timeout = 30");
回答by russian
use this style
使用这种风格
@"server=.\sqlexpress;"
回答by user3143076
I tested all the answers here, but for me, none worked. So I studied a bit the problem, and finally I found the connection string needed. To get this string, you do:
1. in you project name:
a. right click the project name,
b. click Add,
c. select SQL Server Database (obviously you can rename it as you wish).
Now the new desired database will be added to your project.
2. The database is visible in the Server Explorer window.
3. Left click the database name in the Server Explorer window; now check the Solution Explorer window, and you will find the "Connection String", along side with Provider, State, Type, Version.
4. Copy the connection string provided, and put it in the Page_Load method:
我在这里测试了所有答案,但对我来说,没有一个有效。于是研究了一下问题,终于找到了需要的连接字符串。为了得到这个字符串,你这样做:
1.在你项目名称:
一。右键单击项目名称,
b。单击添加,
c。选择 SQL Server 数据库(显然您可以根据需要重命名)。
现在新的所需数据库将添加到您的项目中。
2. 数据库在服务器浏览器窗口中可见。
3. 在服务器资源管理器窗口左键单击数据库名称;现在检查解决方案资源管理器窗口,您将找到“连接字符串”,以及提供程序、状态、类型、版本。
4、复制提供的连接字符串,放到Page_Load方法中:
string source = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\x\x\documents\visual studio 2013\Projects\WebApplication3\WebApplication3\App_Data\Product.mdf;Integrated Security=True";
SqlConnection conn = new SqlConnection(source);
conn.Open();
//your code here;
conn.Close();
I renamed my database as Product. Also, in the "AttachDbFilename", you must replace "c:\x\x\documents\" with your path to the phisical address of the .mdf file.
我将我的数据库重命名为 Product。此外,在“AttachDbFilename”中,您必须将“c:\x\x\documents\”替换为 .mdf 文件物理地址的路径。
It worked for me, but I must mention this method works for VS2012 and VS2013. Don't know about other versions.
它对我有用,但我必须提到这种方法适用于 VS2012 和 VS2013。其他版本不知道。
回答by ali
Note to under
注意下
connetionString =@"server=XXX;Trusted_Connection=yes;database=yourDB;";
Note: XXX = . OR .\SQLEXPRESS OR .\MSSQLSERVER OR (local)\SQLEXPRESS OR (localdb)\v11.0 &...
注:XXX = . OR .\SQLEXPRESS OR .\MSSQLSERVER OR (local)\SQLEXPRESS OR (localdb)\v11.0 &...
you can replace 'server' with 'Data Source'
您可以将“服务器”替换为“数据源”
too you can replace 'database' with 'Initial Catalog'
你也可以用“初始目录”替换“数据库”
Sample:
样本:
connetionString =@"server=.\SQLEXPRESS;Trusted_Connection=yes;Initial Catalog=books;";

