这是我用来将数据插入数据库的代码。
using (SqlConnection connection = new SqlConnection(@"Data Source=stroibot-pc\sqlexpress;Initial Catalog=OOP6;Integrated Security=True"))
{
SqlCommand cmd = new SqlCommand("INSERT INTO Collection (ID, Color, Width, Height) VALUES (@ID, @Color, @Width, @Height)");
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("@ID", GetID());
cmd.Parameters.AddWithValue("@Color", GetColor());
cmd.Parameters.AddWithValue("@Width", GetWidth());
cmd.Parameters.AddWithValue("@Height", GetHeight());
connection.Open();
try {
cmd.ExecuteNonQuery();
}
catch(SqlException)
{
ClearDataBase();
cmd.ExecuteNonQuery();
}
connection.Close();
}
此后我的datagridview中没有显示任何内容(是的,数据库本身有数据)。如果我关闭应用程序并再次打开它,那么数据就会出现,但是来吧,我不想关闭应用只是为了查看数据。
我尝试使用Update()更新它并使用Refresh()刷新但没有任何帮助。
那么我需要做些什么才能以某种方式显示我刚上传到数据库中的数据? 谢谢!
P.S。的WinForms
编辑#1 :我还尝试将数据源置空,然后重新初始化它。
编辑#2 :以下是与数据库
相关的所有内容 //
// dataGridView1
//
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.AutoGenerateColumns = false;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.iDDataGridViewTextBoxColumn,
this.colorDataGridViewTextBoxColumn,
this.widthDataGridViewTextBoxColumn,
this.heightDataGridViewTextBoxColumn});
this.dataGridView1.DataSource = this.collectionBindingSource;
this.dataGridView1.Location = new System.Drawing.Point(353, 18);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(443, 401);
this.dataGridView1.TabIndex = 2;
//
// iDDataGridViewTextBoxColumn
//
this.iDDataGridViewTextBoxColumn.DataPropertyName = "ID";
this.iDDataGridViewTextBoxColumn.HeaderText = "ID";
this.iDDataGridViewTextBoxColumn.Name = "iDDataGridViewTextBoxColumn";
//
// colorDataGridViewTextBoxColumn
//
this.colorDataGridViewTextBoxColumn.DataPropertyName = "Color";
this.colorDataGridViewTextBoxColumn.HeaderText = "Color";
this.colorDataGridViewTextBoxColumn.Name = "colorDataGridViewTextBoxColumn";
//
// widthDataGridViewTextBoxColumn
//
this.widthDataGridViewTextBoxColumn.DataPropertyName = "Width";
this.widthDataGridViewTextBoxColumn.HeaderText = "Width";
this.widthDataGridViewTextBoxColumn.Name = "widthDataGridViewTextBoxColumn";
//
// heightDataGridViewTextBoxColumn
//
this.heightDataGridViewTextBoxColumn.DataPropertyName = "Height";
this.heightDataGridViewTextBoxColumn.HeaderText = "Height";
this.heightDataGridViewTextBoxColumn.Name = "heightDataGridViewTextBoxColumn";
//
// collectionBindingSource
//
this.collectionBindingSource.DataMember = "Collection";
this.collectionBindingSource.DataSource = this.oOP6DataSet;
//
// oOP6DataSet
//
this.oOP6DataSet.DataSetName = "OOP6DataSet";
this.oOP6DataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
//
// collectionTableAdapter
//
this.collectionTableAdapter.ClearBeforeFill = true;