简体   繁体   English

C#,Winform,将数据插入本地数据库并在DataGridVIew中查看

[英]C#, Winform, Inserting data into local database and viewing it in DataGridVIew

I am making a C# Winform and I am to the point where I read some data from xml, and from interaction with the user. 我正在制作C#Winform,现在我要从xml和与用户的交互中读取一些数据。 I then need to put that data into a local database and then read it trough a sub-form. 然后,我需要将该数据放入本地数据库,然后通过子窗体读取它。

I am declaring, and reading the data: 我正在声明并读取数据:

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");

( until here all works I just post it for better code comprehence ) (直到这里所有的作品,我只是为了更好的代码理解而发布它)

Now I try to insert some data into the local database table, I have used two approaches sadly both of them did not worked for me: 现在,我尝试将一些数据插入本地数据库表中,但不幸的是,我使用了两种方法对我都无效:

First: 第一:

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();

Second: 第二:

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();
}

I test each and every line and they execute successfully, but when I manually open the database is empty and of course the DataGridView in the other sub-forms are empty too. 我测试了每一行,它们都成功执行,但是当我手动打开数据库时,该数据库为空,当然其他子表单中的DataGridView也为空。

I have tried many modifications but unfortunately, none worked. 我尝试了许多修改,但不幸的是,没有任何修改。 I can not find where my mistake is. 我找不到我的错误在哪里。 If more information is needed I am here. 如果需要更多信息,请在这里。 To finish my question: Data from xml and user interaction is read and then must be put into the mentioned local database, saved for later use and displayed in DataGridView in other sub-forms. 完成我的问题:读取来自xml和用户交互的数据,然后必须将其放入提到的本地数据库中,保存以备后用,并以其他子形式显示在DataGridView中。 Thank you all ! 谢谢你们 !

SqlCeConnection con = new SqlCeConnection(@"Data Source=...users.sdf"); SqlCeConnection con =新的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);
int i=cmd.ExecuteNonQuery();


     if(i>0)
    {
         // Success
    }
    con.Close();

And second Correction 和第二次更正

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));
SsqlDataAdapter aa= new SsqlDataAdapter(Comm);
DataTable dt= new Datatable();
da.Fill(dt);
}

Thank you all for the help and guideline ! 谢谢大家的帮助和指导! And mostly thank you Tanmay Nehete. 非常感谢您Tanmay Nehete。 Your first correction worked like a charm. 您的第一次更正就像是魅力。

I have added @ before the names of the columns: 我在列名称之前添加了@:

cmd.Parameters.AddWithValue("@test_name", (string) Title);

and the line: 和线:

int i=cmd.ExecuteNonQuery();

and the data is stored in the local database and is displayed in all the DataGridViews THANK YOU ! 并且数据存储在本地数据库中,并显示在所有DataGridViews中。谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM