【发布时间】:2016-02-26 11:09:46
【问题描述】:
我正在制作一个 C# Winform,我已经到了从 xml 以及与用户交互中读取一些数据的地步。然后我需要将该数据放入本地数据库,然后通过子表单读取它。
我正在声明并读取数据:
public int score = 0; //not null after user interaction
public string candidate = null; //not null after user interaction
XElement Title = Test.Root.Element("title");
XElement MaxScore = Test.Root.Element("property_maxscore");
XElement PassScore = Test.Root.Element("property_passscore");
(直到这里所有的作品我只是为了更好地理解代码而发布它)
现在我尝试在本地数据库表中插入一些数据,我使用了两种方法,遗憾的是它们都对我不起作用:
第一:
SqlCeConnection con = new SqlCeConnection(@"Data Source=...users.sdf");
con.Open();
SqlCeCommand cmd = new SqlCeCommand(@"INSERT INTO final_tests (test_name, candidate, score, pass_score, max_score, completed) VALUES (@test_name, @candidate, @score, @pass_score, @max_score, @completed)", con);
cmd.Parameters.AddWithValue("test_name", (string) Title);
cmd.Parameters.AddWithValue("candidate", candidate);
cmd.Parameters.AddWithValue("score", score);
cmd.Parameters.AddWithValue("pass_score", (string) PassScore);
cmd.Parameters.AddWithValue("max_score", (string) MaxScore);
cmd.Parameters.AddWithValue("completed", DateTime.Now);
cmd.ExecuteNonQuery();
con.Close();
第二:
SqlCeConnection con = new SqlCeConnection(@"Data Source=...users.sdf");
con.Open();
using (SqlCeCommand comm = new SqlCeCommand("SELECT * FROM final_tests WHERE score LIKE @score", con))
{
comm.Parameters.Add(new SqlCeParameter("score", score));
comm.ExecuteNonQuery();
}
我测试了每一行,它们执行成功,但是当我手动打开数据库时是空的,当然其他子窗体中的 DataGridView 也是空的。
我尝试了很多修改,但不幸的是,没有一个奏效。我找不到我的错误在哪里。如果需要更多信息,我在这里。 完成我的问题: 读取来自xml和用户交互的数据,然后必须将其放入提到的本地数据库中,保存以备后用,并以其他子窗体的形式显示在DataGridView中。谢谢你们 !
【问题讨论】:
-
检查 cmd.ExecuteNonQuery() 返回的内容。它应该返回受影响的行数 (msdn.microsoft.com/en-us/library/…)
-
您希望第二个查询做什么?这是一个
SELECT,但你永远不会对结果做任何事情。
标签: c# xml winforms datagridview local-database