2013-03-01 87 views
3

嗨我试图连接一个SQL服务器压缩数据库到我的程序,我想要一个按钮,从数据库中删除所有条目,当我按下按钮时,程序抛出异常并给出以下错误消息“建立与SQL Server的连接时发生网络相关或实例特定的错误未找到服务器或无法访问服务器验证实例名称是否正确以及SQL Server是否配置为允许远程连接。(提供商:SQL网络接口,错误:26 - 错误定位服务器/实例指定)“将C#连接到SQL Server Compact数据库

帮助请吗? =]

对不起,代码如下=]

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


     } 
    } 
} 
+3

那么让我们来看看一些代码。另外,你可以在SQL Server Management Studio中打开它吗? – Arran 2013-03-01 12:09:11

+0

您是否传递正确的凭据,指向正确的SQL Server实例和数据库? – 2013-03-01 12:15:21

+0

我指向本地存储的SQL服务器压缩数据库和目录名称正确 – 2013-03-01 12:17:54

回答

8

尝试使用SqlCeConnection类,而不是SqlConnection

SqlCeConnection sqlConnection1 = new SqlCeConnection(); 
sqlConnection1.ConnectionString = "Data Source = C:\\Users\\Administrator\\My Documents\\BMS_Data.sdf;Persist Security Info=False"; 
+0

如何修改'LAN'的数据源?比方说,'.sdf'在共享网络上位于'\\ xyz-PC \ db \ dbfile.sdf' – Zeeshan 2016-08-06 10:54:46

0

看一看这个博客文章:SQL SERVER – FIX : ERROR : (provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server)

这又一步一步地,你可能需要做什么:

总之:

  1. SQL Server should be up and running.
  2. Enable TCP/IP in SQL Server Configuration
  3. Open Port in Windows Firewall
  4. Enable Remote Connection
  5. Enable SQL Server Browser Service
  6. Create exception of sqlbrowser.exe in Firewall
  7. Recreate Alias

关于在哪里和什么在每个步骤做,你将在文章中找到更深入的信息。您可能也想看看Connection strings for SQL Server Compact。在那里你可以找到你可以尝试使用的连接字符串的其他变体。

+0

感谢您的详细答案,我发现问题的来源是SQL服务器出于某种原因无法连接,我现在得到一个错误,说“远程过程调用失败” – 2013-03-01 13:16:31

+0

@ReeceCottam:我不完全熟悉这一点,是一个“远程过程”,与“存储过程”相同吗?如果是这样,那么CE版本可能不支持以下功能:查看此功能对比表的底部:(比较SQL Server Compact,SQL Server Express 2012和LocalDB)[http:// erikej。 blogspot.de/2011/01/comparison-of-sql-server-compact-4-and.html]它说“存储过程,视图,触发器:No_” – 2013-03-01 14:42:27

+0

请参阅@ ErikEJ的答案... – blins 2013-03-01 14:49:00

4

如果要连接到SQL Server Compact,使用的SqlCeConnection,SqlCeCommand等添加到SQL Server精简ADO.NET提供参考,System.Data.SqlServerCe.dll