Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQL连接不工作错误:26-定位指定的服务器/实例时出错_C#_Sql_Error Handling - Fatal编程技术网

C# SQL连接不工作错误:26-定位指定的服务器/实例时出错

C# SQL连接不工作错误:26-定位指定的服务器/实例时出错,c#,sql,error-handling,C#,Sql,Error Handling,我有两个连接数据库的例子,第一个工作,第二个不工作 错误说明: SQL backup.exe中发生类型为“System.Data.SqlClient.SqlException”的未处理异常 其他信息:建立与SQL Server的连接时发生网络相关或特定于实例的错误。找不到服务器或无法访问服务器。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。(提供程序:SQL网络接口,错误:26-定位指定的服务器/实例时出错) .sdf文件在两个类中的位置相同。我使用相同的路径,如何配置第

我有两个连接数据库的例子,第一个工作,第二个不工作

错误说明:

SQL backup.exe中发生类型为“System.Data.SqlClient.SqlException”的未处理异常 其他信息:建立与SQL Server的连接时发生网络相关或特定于实例的错误。找不到服务器或无法访问服务器。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。(提供程序:SQL网络接口,错误:26-定位指定的服务器/实例时出错)

.sdf文件在两个类中的位置相同。我使用相同的路径,如何配置第一个类才能正常工作?

  string conn = @"Data Source = C:\Users\admin\Documents\visual studio 2013\Projects\SQL backup\SQL backup\sdf\locals.sdf";
    // want work
    public Connections()
    {
        try
        {
            SqlConnection con = new SqlConnection(conn);
            con.Open();
            try
            {
                SqlCommand com = new SqlCommand("select * from tblUsers", con);
                SqlDataReader reader = com.ExecuteReader();
                while (reader.Read())
                {
                    MessageBox.Show(reader.GetString(0));
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message); throw;
            }
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message);
            throw;
        }

    }
    // working
    string localsdf = @"Data Source = C:\Users\admin\Documents\visual studio 2013\Projects\SQL backup\SQL backup\sdf\locals.sdf";
    public ConnectionCe()
    {
        try
        {
            SqlCeConnection sqlcon = new SqlCeConnection(localsdf);
            sqlcon.Open();

            SqlCeCommand com = new SqlCeCommand("select * from tblUsers", sqlcon);

            using (SqlCeDataReader reader = com.ExecuteReader())
            {
                infolist = new List<information>();
                while (reader.Read())
                {
                    info = new information();
                    info.username = reader.GetString(0);
                    info.age = reader.GetInt32(1);
                    infolist.Add(info);
                }
            }
            sqlcon.Close();
        }
        catch (SqlCeException ex)
        {
            MessageBox.Show(ex.Message);
            throw;
        }
string conn=@“数据源=C:\Users\admin\Documents\visualstudio 2013\Projects\SQL backup\SQL backup\sdf\locals.sdf”;
//想要工作吗
公共关系()
{
尝试
{
SqlConnection con=新的SqlConnection(conn);
con.Open();
尝试
{
SqlCommand com=新的SqlCommand(“从tblUsers中选择*”,con);
SqlDataReader=com.ExecuteReader();
while(reader.Read())
{
Show(reader.GetString(0));
}
}
catch(SqlException-ex)
{
MessageBox.Show(例如Message);抛出;
}
}
catch(SqlException-ex)
{
MessageBox.Show(例如Message);
投掷;
}
}
//工作
字符串localsdf=@“数据源=C:\Users\admin\Documents\visual studio 2013\Projects\SQL backup\SQL backup\sdf\locals.sdf”;
公共关系
{
尝试
{
SqlCeConnection sqlcon=新的SqlCeConnection(localsdf);
sqlcon.Open();
SqlCeCommand com=新的SqlCeCommand(“从tblUsers中选择*”,sqlcon);
使用(SqlCeDataReader=com.ExecuteReader())
{
infolist=新列表();
while(reader.Read())
{
信息=新信息();
info.username=reader.GetString(0);
info.age=reader.GetInt32(1);
添加(信息);
}
}
sqlcon.Close();
}
catch(SqlCeException-ex)
{
MessageBox.Show(例如Message);
投掷;
}

您的连接字符串缺少信息。请参阅这篇msdn文章“放置您认为有效的示例”尝试将其添加到您的连接字符串中:;Persist Security Info=False;提示:您应该使用分号结束连接字符串的每个属性,但它不起作用。。。