简体   繁体   English

使用C#连接到SQL Server 2012数据库(Visual Studio 2012)

[英]Connect to SQL Server 2012 Database with C# (Visual Studio 2012)

Evening all, 整个晚上

I'm trying to connect to a SQL Server 2012 database from C#. 我正在尝试从C#连接到SQL Server 2012数据库。 My connection settings when using SQL Server Management Studio are as below:- 使用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在本地运行。

在您的连接字符串中,将server=localhost替换为“ server = Paul-PC\\\\SQLEXPRESS;

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: 1.在您的项目名称中:
a. 一种。 right click the project name, 右键单击项目名称,
b. b。 click Add, 点击添加,
c. C。 select SQL Server Database (obviously you can rename it as you wish). 选择“ SQL Server数据库”(显然,您可以根据需要对其重命名)。
Now the new desired database will be added to your project. 现在,新的所需数据库将添加到您的项目中。
2. The database is visible in the Server Explorer window. 2.该数据库在“服务器资源管理器”窗口中可见。
3. Left click the database name in the Server Explorer window; 3.在“服务器资源管理器”窗口中左击数据库名称; 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: 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. 另外,在“ AttachDbFilename”中,必须将“ c​​:\\ x \\ x \\ documents \\”替换为.mdf文件物理地址的路径。

It worked for me, but I must mention this method works for VS2012 and VS2013. 它为我工作,但我必须提到此方法适用于VS2012和VS2013。 Don't know about other versions. 不了解其他版本。

server=.\\SQLEXPRESS替换server=localhost可能会完成这项工作。

尝试:

SqlConnection myConnection = new SqlConnection("Database=testDB;Server=Paul-PC\\SQLEXPRESS;Integrated Security=True;connect timeout = 30");

Note to under 注意下

connetionString =@"server=XXX;Trusted_Connection=yes;database=yourDB;";

Note: XXX = . 注意:XXX =。 OR .\\SQLEXPRESS OR .\\MSSQLSERVER OR (local)\\SQLEXPRESS OR (localdb)\\v11.0 &... 或。\\ SQLEXPRESS或。\\ MSSQLSERVER或(本地)\\ SQLEXPRESS或(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;";

使用这种风格

@"server=.\sqlexpress;"

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

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