简体   繁体   English

无法连接到我的SQL Server数据库?

[英]Can't connect to my SQL Server database?

I'm creating a very basic CRUD desktop winforms application in C#/.NET 4.0. 我正在C#/ .NET 4.0中创建一个非常基本的CRUD桌面winforms应用程序。

Letting Visual Studio auto-generate the fields for the table I'd like to perform my CRUD operations on works just fine, but I'm running into problems when I try and interact with it manually with my own SQL queries. 让Visual Studio自动生成表的字段我想在我的CRUD操作上运行得很好,但是当我尝试使用我自己的SQL查询手动与它交互时,我遇到了问题。

The auto-generated fields are using the connection string: 自动生成的字段使用连接字符串:

Data Source=|DataDirectory|\Data Analysis.sdf

If I try and do: 如果我尝试做:

SqlConnection conn = new SqlConnection(@"Data Source=|DataDirectory|\Data Analysis.sdf");
conn.Open();

It just hangs. 它只是挂起。 What am I missing? 我错过了什么?

That's a connection string for a SQL Server Compact Edition (CE) database (everything stored inside a single .sdf file) - is that what you're using? 这是SQL Server Compact Edition (CE)数据库的连接字符串(存储在单个.sdf文件中的所有内容) - 您正在使用的是什么?

If so : in that case, you'd have to use SqlCeConnection (not a SqlConnection - that's for "grown-up" SQL Server version - not CE) 如果是这样的话:在这种情况下,你必须使用SqlCeConnection (不是SqlConnection - 那是“成熟的”SQL Server版本 - 而不是CE)

Maybe try adding some more options to the connection string: 也许尝试在连接字符串中添加更多选项:

Persist Security Info=False;
File Mode=shared read;

相信您已经指定了.sdf文件的相对路径,您可能需要从System.Environment.CurrentDirectory获取可执行文件的运行时文件夹并将其添加到文件名中。

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

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