无法更新 Access 数据库 DataGridView
Cannot update Access Database DataGridView
我正在尝试以某种方式更新或重新检索来自Access数据库的数据(Random piece of data with column names)
我正在使用 Microsoft.ACE.OLEDB.12.0
。
我创建了一个查询,该查询应该 return 记录列值介于两个日期之间的位置:
SQL 示例:
SELECT Arkusz1.ID, Arkusz1.Adres, Arkusz1.Umówione, Arkusz1.Data_umówienia, Arkusz1.Zakończone, Arkusz1.[Spisano?], Arkusz1.Notatki
FROM Arkusz1
WHERE(
((Arkusz1.Umówione) = Yes) // or 1 or <> 0
AND ((Arkusz1.Zakończone) = No) // or 0 doesen't provide right answer
AND ((Arkusz1.Data_umówienia) BETWEEN 24/07/2021 AND 26/07/2021) )
ORDER BY Arkusz1.Data_umówienia;
我的代码:
void fillGrid()
{
string teraz = DateTime.Today.AddDays(-1).ToShortDateString().Replace('.', '/');
string jutro = DateTime.Today.AddDays(1).ToShortDateString().Replace('.', '/');
// AddDays +-1 because of my data issue, that's not important
string dzisiejsze = "SELECT Arkusz1.ID, Arkusz1.Adres, Arkusz1.Umówione, Arkusz1.Data_umówienia,"
+ " Arkusz1.Zakończone, Arkusz1.[Spisano?], Arkusz1.Notatki FROM Arkusz1"
+ " WHERE( ((Arkusz1.Umówione) = Yes) AND ((Arkusz1.Zakończone) = No) AND "
+ "((Arkusz1.Data_umówienia) BETWEEN "
+ teraz + " AND " + jutro + ") )"
+ " ORDER BY Arkusz1.Data_umówienia;";
//selecting almost every column from my only sheet (Arkusz1),
//if Umówione (appointment is arranged) has True value AND issue is not ended (Zakończone)
//AND appointment date is between yesterday and tomorrow
//(as I said, that's all because my stupid ideas in creating DB)
connection.Open();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dzisiejsze, connection);
DataTable dt = new DataTable();
dataAdapter.Fill(dt);
TodaysPlan_dataGridView.DataSource = dt;
TodaysPlan_dataGridView.Refresh();
connection.Close();
}
我什至添加了一个特殊按钮来更新整个网格(首先它应该在第二个表单关闭后更新):
private void refreshGrid_Click(object sender, EventArgs e)
{
Application.DoEvents(); // because I saw something on stack, didn't work
fillGrid(); //start filling again, didn't work too
TodaysPlan_dataGridView.Refresh(); // same, didn't work
}
我没有收到任何错误,执行(即使在调试模式下)只是通过 refreshGrid_Click
中的代码。
Edit: Tried to execute that SQL code for random data, didn't work too
Ofc 这不是我问题中最重要的部分,但我可以另外问 :)
我还将提供 从第二种形式保存按钮的代码,但我认为它不会真正有用:
private void save_button_Click(object sender, EventArgs e)
{
if ((Tick_label.Visible == true) && (IDInfo_label.Text == "All good!") && (street_textBox.TextLength > 0))
{
connection.Open();
string cmd_string = "INSERT INTO Arkusz1 (ID, Adres, Przydzielenie) VALUES ('" + ID_maskedTextBox.Text + "', '" + street_textBox.Text + "','" + startDate_dateTimePicker.Value + "')";
OleDbCommand command = new OleDbCommand(cmd_string, connection);
command.ExecuteNonQuery();
connection.Close();
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
MessageBox.Show("error");
}
}
这是我在Stack上的第一个问题,欢迎任何建议和建设性的批评!
顺便说一句,我正在使用 this YT 教程,希望它对某人有所帮助:)
好的,@Steve 和@Jimi 提供的信息非常有用,首先我使用 String.Format()
更改了字符串格式并解决了 SQL 问题 - 这是 (#date #)格式:
DateTime tmp_teraz = DateTime.Today.AddDays(-1);
string teraz = String.Format("{0}/{1}/{2}", tmp_teraz.Month.ToString(),
tmp_teraz.Day.ToString(), tmp_teraz.Year.ToString());
DateTime tmp_jutro = DateTime.Today.AddDays(1);
string jutro = String.Format("{0}/{1}/{2}", tmp_jutro.Month.ToString(), tmp_jutro.Day.ToString(), tmp_jutro.Year.ToString());
string dzisiejsze = String.Format("SELECT Arkusz1.ID, Arkusz1.Adres, Arkusz1.Umówione, Arkusz1.Data_umówienia, Arkusz1.Zakończone, Arkusz1.[Spisano?], Arkusz1.Notatki FROM Arkusz1 WHERE( ((Arkusz1.Umówione) = true) AND ((Arkusz1.Zakończone) = false) AND ((Arkusz1.Data_umówienia) " +
"BETWEEN (#{0}#) AND (#{1}#)) ) ORDER BY Arkusz1.Data_umówienia;", teraz, jutro);
最后,网格看起来像 this。
使用 dataGrid 刷新或插入数据不起作用,但这是我会随着时间的推移开发的东西。
感谢您的帮助,祝大家愉快!
我正在尝试以某种方式更新或重新检索来自Access数据库的数据(Random piece of data with column names)
我正在使用 Microsoft.ACE.OLEDB.12.0
。
我创建了一个查询,该查询应该 return 记录列值介于两个日期之间的位置:
SQL 示例:
SELECT Arkusz1.ID, Arkusz1.Adres, Arkusz1.Umówione, Arkusz1.Data_umówienia, Arkusz1.Zakończone, Arkusz1.[Spisano?], Arkusz1.Notatki
FROM Arkusz1
WHERE(
((Arkusz1.Umówione) = Yes) // or 1 or <> 0
AND ((Arkusz1.Zakończone) = No) // or 0 doesen't provide right answer
AND ((Arkusz1.Data_umówienia) BETWEEN 24/07/2021 AND 26/07/2021) )
ORDER BY Arkusz1.Data_umówienia;
我的代码:
void fillGrid()
{
string teraz = DateTime.Today.AddDays(-1).ToShortDateString().Replace('.', '/');
string jutro = DateTime.Today.AddDays(1).ToShortDateString().Replace('.', '/');
// AddDays +-1 because of my data issue, that's not important
string dzisiejsze = "SELECT Arkusz1.ID, Arkusz1.Adres, Arkusz1.Umówione, Arkusz1.Data_umówienia,"
+ " Arkusz1.Zakończone, Arkusz1.[Spisano?], Arkusz1.Notatki FROM Arkusz1"
+ " WHERE( ((Arkusz1.Umówione) = Yes) AND ((Arkusz1.Zakończone) = No) AND "
+ "((Arkusz1.Data_umówienia) BETWEEN "
+ teraz + " AND " + jutro + ") )"
+ " ORDER BY Arkusz1.Data_umówienia;";
//selecting almost every column from my only sheet (Arkusz1),
//if Umówione (appointment is arranged) has True value AND issue is not ended (Zakończone)
//AND appointment date is between yesterday and tomorrow
//(as I said, that's all because my stupid ideas in creating DB)
connection.Open();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dzisiejsze, connection);
DataTable dt = new DataTable();
dataAdapter.Fill(dt);
TodaysPlan_dataGridView.DataSource = dt;
TodaysPlan_dataGridView.Refresh();
connection.Close();
}
我什至添加了一个特殊按钮来更新整个网格(首先它应该在第二个表单关闭后更新):
private void refreshGrid_Click(object sender, EventArgs e)
{
Application.DoEvents(); // because I saw something on stack, didn't work
fillGrid(); //start filling again, didn't work too
TodaysPlan_dataGridView.Refresh(); // same, didn't work
}
我没有收到任何错误,执行(即使在调试模式下)只是通过 refreshGrid_Click
中的代码。
Edit: Tried to execute that SQL code for random data, didn't work too Ofc 这不是我问题中最重要的部分,但我可以另外问 :)
我还将提供 从第二种形式保存按钮的代码,但我认为它不会真正有用:
private void save_button_Click(object sender, EventArgs e)
{
if ((Tick_label.Visible == true) && (IDInfo_label.Text == "All good!") && (street_textBox.TextLength > 0))
{
connection.Open();
string cmd_string = "INSERT INTO Arkusz1 (ID, Adres, Przydzielenie) VALUES ('" + ID_maskedTextBox.Text + "', '" + street_textBox.Text + "','" + startDate_dateTimePicker.Value + "')";
OleDbCommand command = new OleDbCommand(cmd_string, connection);
command.ExecuteNonQuery();
connection.Close();
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
MessageBox.Show("error");
}
}
这是我在Stack上的第一个问题,欢迎任何建议和建设性的批评!
顺便说一句,我正在使用 this YT 教程,希望它对某人有所帮助:)
好的,@Steve 和@Jimi 提供的信息非常有用,首先我使用 String.Format()
更改了字符串格式并解决了 SQL 问题 - 这是 (#date #)格式:
DateTime tmp_teraz = DateTime.Today.AddDays(-1);
string teraz = String.Format("{0}/{1}/{2}", tmp_teraz.Month.ToString(),
tmp_teraz.Day.ToString(), tmp_teraz.Year.ToString());
DateTime tmp_jutro = DateTime.Today.AddDays(1);
string jutro = String.Format("{0}/{1}/{2}", tmp_jutro.Month.ToString(), tmp_jutro.Day.ToString(), tmp_jutro.Year.ToString());
string dzisiejsze = String.Format("SELECT Arkusz1.ID, Arkusz1.Adres, Arkusz1.Umówione, Arkusz1.Data_umówienia, Arkusz1.Zakończone, Arkusz1.[Spisano?], Arkusz1.Notatki FROM Arkusz1 WHERE( ((Arkusz1.Umówione) = true) AND ((Arkusz1.Zakończone) = false) AND ((Arkusz1.Data_umówienia) " +
"BETWEEN (#{0}#) AND (#{1}#)) ) ORDER BY Arkusz1.Data_umówienia;", teraz, jutro);
最后,网格看起来像 this。
使用 dataGrid 刷新或插入数据不起作用,但这是我会随着时间的推移开发的东西。
感谢您的帮助,祝大家愉快!