我使用這個代碼將數據插入到一個Access數據庫文件查詢列的確切日期C#
decimal quantity;
if (!Decimal.TryParse(textBox9.Text, out quantity))
MessageBox.Show("Invalid number");
else
{
using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data source=|DataDirectory|\\crepeDB.accdb;"))
{
conn.Open();
string query = @"insert into Sales (Sdate,SQuantity)
values (@date, @qta)";
System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(query, conn);
cmd.Parameters.Add("@date", OleDbType.Date).Value = dateTimePicker1.Value;
cmd.Parameters.Add("@qta", OleDbType.Decimal).Value = quantity;
cmd.ExecuteNonQuery();
}
}
現在,我想今天的總SQuantity期間的總和,所以我用這個代碼,但它不斷給我的錯誤
public void Calculate()
{
using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data source=|DataDirectory|\\crepeDB.accdb;"))
{
conn.Open();
string query = @"select SQuantity SUM(SQuantity) AS 'Total' From Sales where Sdate = '"+ dateTimePicker1.Value +"'";
System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(query, conn);
cmd.ExecuteNonQuery();
textBox1.Text = cmd.ExecuteScalar().ToString();
}
}
private void frmShiftEnd_Load(object sender, EventArgs e)
{
Calculate();
}
什麼錯誤? http://stackoverflow.com/help/how-to-ask – user5226582
我相信你必須從選擇中的查詢中取出SQuantity。應該選擇SUM(SQuantity)AS'Total'... –
您試圖在'Calculate()'方法中將日期作爲字符串。爲什麼不像你在插入中那樣使用日期參數?你已經知道這是有效的。 –