简体   繁体   中英

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. 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.

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. Thank you all !

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);
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. 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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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