问题描述
我正在尝试将SQL Server紧凑型数据库连接到我的程序,并且我需要一个按钮来删除数据库中的所有条目,当我按下该按钮时,该程序将引发异常并给出以下错误消息网络建立与SQL Server的连接时发生与对象相关的错误或特定于实例的错误.找不到服务器或无法访问该服务器.请验证实例名称正确并且已将SQL Server配置为允许远程连接.(提供者:SQL网络接口),错误:26-查找指定的服务器/实例时出错)"
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)"
请帮助?=]
对不起,代码在下面=]
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");
}
}
}
推荐答案
尝试使用 SqlCeConnection
类而不是 SqlConnection
:
SqlCeConnection sqlConnection1 = new SqlCeConnection();
sqlConnection1.ConnectionString = "Data Source = C:\\Users\\Administrator\\My Documents\\BMS_Data.sdf;Persist Security Info=False";
这篇关于将C#连接到SQL Server Compact数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!