Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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# SqlCeDataReader Read()返回false_C#_Database_Sql Server Ce - Fatal编程技术网

C# SqlCeDataReader Read()返回false

C# SqlCeDataReader Read()返回false,c#,database,sql-server-ce,C#,Database,Sql Server Ce,我正在做一个纸牌游戏,它使用一个数据库来存储和检索纸牌。不幸的是,我无法从数据库中检索数据 Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CardGame { class Program { static void Main(string[] args) {

我正在做一个纸牌游戏,它使用一个数据库来存储和检索纸牌。不幸的是,我无法从数据库中检索数据

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CardGame
{
    class Program
    {
        static void Main(string[] args)
        {
            Card c = new Card();
            Card d;

            c.Name = "TestCard";
            c.Pack = "Pack";
            c.Value = 5;
            c.Color = Colors.green;
            c.Description = "Describing stuff";

            //Database.CreateCardTable(); (Already created)
            Database.InsertCard(c);
            d = Database.RetrieveCard("TestCard");

            Console.WriteLine(d.Name);

            Console.ReadLine();
        }
    }
}
Database.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
using System.Data;
using System.Data.SqlClient;

namespace CardGame
{
    class Database
    {
        public static void CreateCardTable()
        {
            string sqlStr;
            SqlCeConnection CardConn = new SqlCeConnection(Properties.Settings.Default.CardsConnectionString);

            sqlStr = "CREATE TABLE Data " +
                "(Name NVARCHAR(50), " +
                "Pack NVARCHAR(50), " +
                "Value INT, " +
                "Color INT," +
                "Description NVARCHAR(200))";

            SqlCeCommand CardComm = new SqlCeCommand(sqlStr, CardConn);

            try
            {
                CardConn.Open();
                CardComm.ExecuteNonQuery();
                Console.WriteLine("Tables Created Successfully!");
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.WriteLine("Error creating tables.");
            }
            finally
            {
                if (CardConn.State == ConnectionState.Open)
                {
                    CardConn.Close();
                }
            }
        }

        public static void InsertCard(Card c)
        {
            SqlCeConnection CardConn = new SqlCeConnection(Properties.Settings.Default.CardsConnectionString);

            try
            {
                CardConn.Open();
                SqlCeCommand insertCommand = new SqlCeCommand("INSERT INTO Data VALUES(@Name, @Pack, @Value, @Color, @Description)", CardConn);
                insertCommand.Parameters.Add("Name", c.Name);
                insertCommand.Parameters.Add("Pack", c.Pack);
                insertCommand.Parameters.Add("Value", c.Value);
                insertCommand.Parameters.Add("Color", c.Color);
                insertCommand.Parameters.Add("Description", c.Description);
                Console.WriteLine("Card inserted successfully");
            }
            catch
            {
                Console.WriteLine("Some kind of error.");
            }
            finally
            {
                if (CardConn.State == ConnectionState.Open)
                {
                    CardConn.Close();
                }
            }
        }

        public static Card RetrieveCard(string name)
        {
            Card c = new Card();
            SqlCeConnection CardConn = new SqlCeConnection(Properties.Settings.Default.CardsConnectionString);

            try
            {
                CardConn.Open();
                SqlCeCommand retrieveCommand = new SqlCeCommand("SELECT * FROM Data WHERE Name=@Name", CardConn);
                retrieveCommand.Parameters.Add("Name", name);
                SqlCeDataReader reader = retrieveCommand.ExecuteReader();

                while (reader.Read())
                {
                    Console.WriteLine("Reader: " + reader["Name"].ToString());
                    c.Name = (string)reader["Name"];
                    //read rest
                }


            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }


            return c;
        }

        public void CreateAccountDB(string DBName)
        {

        }
    }
}

当我运行insert card时,我确实会收到一条成功消息(在try语句的末尾)。当我做
Database.RetrieveCard(“测试卡”)它到达while(reader.Read())
行,但我的程序没有到达while语句中的代码。如何修复代码,以便从数据库中读取卡片?

我创建了将卡片插入数据库的命令,但从未实际执行过该命令。所以没有插入任何内容,所以当我去检索卡时,它将不会返回任何内容,因为数据库中没有任何内容

我变了

try
            {
                CardConn.Open();
                SqlCeCommand insertCommand = new SqlCeCommand("INSERT INTO Data VALUES(@Name, @Pack, @Value, @Color, @Description)", CardConn);
                insertCommand.Parameters.Add("Name", c.Name);
                insertCommand.Parameters.Add("Pack", c.Pack);
                insertCommand.Parameters.Add("Value", c.Value);
                insertCommand.Parameters.Add("Color", c.Color);
                insertCommand.Parameters.Add("Description", c.Description);
                Console.WriteLine("Card inserted successfully");
            }


你能确认查询返回数据吗?
try
            {
                CardConn.Open();
                SqlCeCommand insertCommand = new SqlCeCommand("INSERT INTO Data VALUES(@Name, @Pack, @Value, @Color, @Description)", CardConn);
                insertCommand.Parameters.Add("Name", c.Name);
                insertCommand.Parameters.Add("Pack", c.Pack);
                insertCommand.Parameters.Add("Value", (int)c.Value);
                insertCommand.Parameters.Add("Color", (int)c.Color);
                insertCommand.Parameters.Add("Description", c.Description);
                insertCommand.ExecuteNonQuery();
                Console.WriteLine("Card inserted successfully");
            }