您好我正在为一所拥有本地SQL数据库的学校制作一个小型winforms应用程序。我正在使用DataGridView来显示数据,但在我添加新学生后,DataGridView不会刷新。
con.Open();
MessageBox.Show("Connection Open");
SqlCommand cm = new SqlCommand("INSERT INTO Students(Grade,Class,Name,sName) VALUES (@Grade,@Class,@Name,@sName)", con);
cm.Parameters.AddWithValue("@Grade", cbGrade.Text);
cm.Parameters.AddWithValue("@Class", comboClass.Text);
cm.Parameters.AddWithValue("@Name", tbFName.Text);
cm.Parameters.AddWithValue("@sName", tbSName.Text);
try
{
int exec = cm.ExecuteNonQuery();
if(exec>0)
{
MessageBox.Show("Added");
}
else
{
MessageBox.Show("Not added");
}
}
catch (Exception ex)
{
MessageBox.Show("Something went wrong");
con.Close();
}
finally
{
con.Close();
}
答案 0 :(得分:0)
一切正常。
string query = "select * from students";
SqlCommand cmd = new SqlCommand(query,con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DGVStudents.DataSource = dt;