2013-08-02 168 views
0

我用C#和本地数据库(An empty SQL Server Compact Edition database for local data无法连接到本地数据库

打不过,我不能连接到数据库并获取数据。

这是我尝试:

// Properties.Settings.Default.DatabaseConnectionString = Data Source=|DataDirectory|\Database.sdf 
// I guess Visual Studio put it there after I created my database... 

using(SqlConnection sqlConnection = new SqlConnection(Properties.Settings.Default.DatabaseConnectionString)) { 
    using(SqlCommand sqlCommand = new SqlCommand("SELECT * FROM users WHERE id = @id", sqlConnection)) { 
     sqlCommand.CommandType = CommandType.StoredProcedure; 
     sqlCommand.Parameters.AddWithValue("@id", 1); 
     try { 
      sqlConnection.Open(); 
      SqlDataReader reader = sqlCommand.ExecuteReader(CommandBehavior.SingleRow); 
      if(reader.Read()) { 
       System.Console.WriteLine(reader); 
       System.Console.WriteLine(reader["id"]); 
       System.Console.WriteLine(reader["name"]); 
      } 
     } 
     catch(Exception e) { 
      System.Console.WriteLine(e.GetBaseException()); 
     } 
     finally { 
      sqlConnection.Close(); 
     } 
    } 
} 

我的整个PROGRAMM挂了一段时间,暂停后,我得到这个消息:

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)

+0

你确定你的连接字符串是正确的? –

+0

你必须确保'命名的管道'被打开 - “ON” – Maris

+0

@SonerGönül那么,Visual Studio把它放在那里,所以我想它属于那里? –

回答

3

I am playing with C# and a local database (An empty SQL Server Compact Edition database for local data)

您使用的是Sql Server Compact文件,而不是Sql Server Local DB

为了应对的SQL Server Compact,您需要使用System.Data.SqlServerCe命名空间,而不是System.Data.SqlServer

更换SqlConnectionSqlCommand ...

随着SqlceConnectionSqlCeCommand,...


和存储过程中的SQL Server Compact不支持How to use Stored Procedure in SqlCE),所以sqlCeCommand.CommandType能不是CommandType.StoredProcedure

您需要在命令参数中使用commandType.Text。

using(SqlCeConnection sqlCeConnection = new SqlCeConnection(Properties.Settings.Default.DatabaseConnectionString)) { 
    using(SqlCeCommand sqlCeCommand = new SqlCeCommand("SELECT * FROM users WHERE id = @id", sqlCeConnection)) { 
     sqlCeCommand.CommandType = CommandType.Text; 
     sqlCeCommand.Parameters.AddWithValue("@id", 1); 
     try { 
      sqlCeConnection.Open(); 
      SqlCeDataReader reader = sqlCeCommand.ExecuteReader(CommandBehavior.SingleRow); 
      if(reader.Read()) { 
       System.Console.WriteLine(reader); 
       System.Console.WriteLine(reader["id"]); 
       System.Console.WriteLine(reader["name"]); 
      } 
     } 
     catch(Exception e) { 
      System.Console.WriteLine(e.GetBaseException()); 
     } 
     finally { 
      sqlCeConnection.Close(); 
     } 
    } 
} 
+0

谢谢!所以我的数据库类型是错误的。我需要什么类型的本地数据库使用准备好的语句? Express Edition? –

+0

@Bondye我不确定我是否打开你正在寻找的东西。 “使用准备好的陈述”是什么意思? – Chris

+0

嗯..只是[准备语句](http://en.wikipedia.org/wiki/Prepared_statement#C.23_ADO.NET),以防止一些注射和重复。 –

1

您需要使用:

using System.Data.SqlServerCe; 

然后

using (SqlCeConnection sqlConnection = new SqlCeConnection(Properties.Settings.Default.DatabaseConnectionString)) 
{ 
    using (SqlCeCommand sqlCommand = new SqlCeCommand("SELECT * FROM users WHERE id = @id", sqlConnection)) 
    { 
     sqlCommand.CommandType = CommandType.Text; 
     sqlCommand.Parameters.AddWithValue("@id", 1); 
     try 
     { 
      sqlConnection.Open(); 
      SqlCeDataReader reader = sqlCommand.ExecuteReader(CommandBehavior.SingleRow); 
      if (reader.Read()) 
      { 
       System.Console.WriteLine(reader); 
       System.Console.WriteLine(reader["id"]); 
       System.Console.WriteLine(reader["name"]); 
      } 
     } 
     catch (Exception e) 
     { 
      System.Console.WriteLine(e.GetBaseException()); 
     } 
     finally 
     { 
      sqlConnection.Close(); 
     } 
    } 
} 
相关问题