将 C# 连接到 SQL Server Compact 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15157368/
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
Connecting C# to SQL Server Compact database
提问by Captain_Custard
Hi I'm trying to connect an SQL server compact database to my program and I want a button that deletes all entries from the database, when I Press said button the program throws an exception and gives the following error message "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. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"
嗨,我正在尝试将 SQL 服务器紧凑型数据库连接到我的程序,我想要一个按钮来删除数据库中的所有条目,当我按下所述按钮时,程序抛出异常并给出以下错误消息“与网络相关或与 SQL Server 建立连接时发生特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供程序:SQL 网络接口,错误: 26 - 定位服务器/实例时出错)”
Help Please? =]
请帮忙?=]
Sorry, Code is Below =]
对不起,代码在下面=]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
namespace Booking_system_Final
{
public partial class PendingJobs : Form
{
SqlConnection sc = new SqlConnection("Data Source=C:\Users\Administrator\My Documents\BMS_Data.sdf");
public PendingJobs()
{
InitializeComponent();
}
private void PendingJobs_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'bMSDataSet.Bookings' table. You can move, or remove it, as needed.
this.bookingsTableAdapter.Fill(this.bMSDataSet.Bookings);
// TODO: This line of code loads data into the 'bMS_DataDataSet1.Bookings' table. You can move, or remove it, as needed.
}
private void button1_Click(object sender, EventArgs e)
{
sc.Open();
SqlCommand cmd = new SqlCommand("DELETE FROM Bookings");
cmd.Connection = sc;
cmd.ExecuteNonQuery();
sc.Close();
MessageBox.Show("Database Cleared");
}
}
}
采纳答案by www
Try use SqlCeConnection
class rather than SqlConnection
:
尝试使用SqlCeConnection
类而不是SqlConnection
:
SqlCeConnection sqlConnection1 = new SqlCeConnection();
sqlConnection1.ConnectionString = "Data Source = C:\Users\Administrator\My Documents\BMS_Data.sdf;Persist Security Info=False";
回答by Jens H
Have a look at this blog article: SQL SERVER – FIX : ERROR : (provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server)
This goes step-by-step through what you might need to do:
这将逐步完成您可能需要执行的操作:
In short:
简而言之:
- SQL Server should be up and running.
- Enable TCP/IP in SQL Server Configuration
- Open Port in Windows Firewall
- Enable Remote Connection
- Enable SQL Server Browser Service
- Create exception of sqlbrowser.exe in Firewall
- Recreate Alias
- SQL Server 应该已启动并正在运行。
- 在 SQL Server 配置中启用 TCP/IP
- 在 Windows 防火墙中打开端口
- 启用远程连接
- 启用 SQL Server 浏览器服务
- 在防火墙中创建 sqlbrowser.exe 的异常
- 重新创建别名
About the where and what to do in each step, you will find more in-depth information in the article.
关于每个步骤的位置和操作,您将在文章中找到更深入的信息。
You may also want to have a look at the Connection strings for SQL Server Compact. There you can find other variations of the connection string you could try to play with.
您可能还想查看SQL Server Compact的连接字符串。在那里您可以找到可以尝试使用的连接字符串的其他变体。
回答by Retired_User
You seems to be using a bad connection string. (Or the file path is wrong). Check out http://www.connectionstrings.com/sql-server-cefor connection string options.
您似乎使用了错误的连接字符串。(或者文件路径错误)。查看http://www.connectionstrings.com/sql-server-ce了解连接字符串选项。
回答by ErikEJ
If you want to connect to SQL Server Compact, use SqlCeConnection, SqlCeCommand etc. Add a reference to the SQL Server Compact ADO.NET provider, System.Data.SqlServerCe.dll
如果要连接到 SQL Server Compact,请使用 SqlCeConnection、SqlCeCommand 等。添加对 SQL Server Compact ADO.NET 提供程序 System.Data.SqlServerCe.dll 的引用