Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.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# *.sdf数据库不能使用密码_C#_.net_Sql_Sql Server Ce - Fatal编程技术网

C# *.sdf数据库不能使用密码

C# *.sdf数据库不能使用密码,c#,.net,sql,sql-server-ce,C#,.net,Sql,Sql Server Ce,我使用Visual Studio创建了一个带有密码保护的*.sdf文件,工作正常。我插入了一些数据,并尝试使用VisualStudioSQL编辑器进行读取,但当我转到C代码时,该代码不起作用 我尝试了以下方法: using System; using System.Data.SqlServerCe; class Program { static void Main(string[] args) { var conStr

我使用Visual Studio创建了一个带有密码保护的*.sdf文件,工作正常。我插入了一些数据,并尝试使用VisualStudioSQL编辑器进行读取,但当我转到C代码时,该代码不起作用

我尝试了以下方法:

using System;
using System.Data.SqlServerCe;


    class Program
    {
        static void Main(string[] args)
        {
            var conStr = @"data source=C:\path\db.sdf; password=<...>";
            SqlCeConnection con = new SqlCeConnection(conStr);
            con.Open();

            SqlCeCommand cmd = con.CreateCommand();
            cmd.CommandText = "select * from Cookies";
            cmd.CommandType = System.Data.CommandType.TableDirect;
            cmd.Connection = con;

            SqlCeDataReader result = cmd.ExecuteReader();
            //...
            con.Close();
        }
    }
我发现以下错误: 指定的表不存在。[从Cookies中选择*

表结构

这返回false。有人能指出我的错误吗


提前感谢。

您的问题是您正在指定System.Data.CommandType.TableDirect

当您更改代码以使用以下内容时,您将避免看到的错误

cmd.CommandType = System.Data.CommandType.Text;
您甚至可以排除CommandType属性的设置,因为文本是默认值


TableDirect是OLEDB传统命令类型。除非您的命令只是表名或视图名,否则不要使用它。在内部,命令对象将构造一个SELECT*FROM语句,从指定的表或视图中选择所有字段和所有记录。

删除了sqlite标记-该.sdf文件是SQL Server Compact Edition数据库-与sqlite无关当您说不起作用时-是否有错误?你没有得到你期望的数据吗?发生了什么事?@sq33G:data.Read返回False当您将CommandType设置为TableDirect并将CommandText设置为表名时会发生什么情况?检查您的bin文件夹..那里必须有一个sqlce数据库,这就是带有Cookies表的数据库。