简体   繁体   English

将C#连接到SQL Server Compact数据库

[英]Connecting C# to SQL Server Compact database

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");


        }
    }
}

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";

如果要连接到SQL Server Compact,请使用SqlCeConnection,SqlCeCommand等。添加对SQL Server Compact ADO.NET提供程序的引用,System.Data.SqlServerCe.dll

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) 看看这篇博客文章: SQL SERVER - FIX:错误:(提供程序:命名管道提供程序,错误:40 - 无法打开与SQL Server的连接)

This goes step-by-step through what you might need to do: 这可以逐步完成您可能需要做的事情:

In short: 简而言之:

  1. SQL Server should be up and running. SQL Server应该正常运行。
  2. Enable TCP/IP in SQL Server Configuration 在SQL Server配置中启用TCP / IP
  3. Open Port in Windows Firewall 在Windows防火墙中打开端口
  4. Enable Remote Connection 启用远程连接
  5. Enable SQL Server Browser Service 启用S​​QL Server Browser服务
  6. Create exception of sqlbrowser.exe in Firewall 在防火墙中创建sqlbrowser.exe的例外
  7. Recreate Alias 重新创建别名

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 . 您可能还想查看SQL Server Compact连接字符串 There you can find other variations of the connection string you could try to play with. 在那里,您可以找到您可以尝试使用的连接字符串的其他变体。

You seems to be using a bad connection string. 您似乎使用了错误的连接字符串。 (Or the file path is wrong). (或者文件路径错误)。 Check out http://www.connectionstrings.com/sql-server-ce for connection string options. 查看http://www.connectionstrings.com/sql-server-ce了解连接字符串选项。

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

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