我從數據庫中取兩個值作爲字符串。現在我想減去這兩個值。請幫助我,並提前致謝。如何在C#中減去時間
private void button3_Click_1(object sender, EventArgs e)
{
label4.Visible = true;
textBox3.Visible = true;
string condur = Properties.Settings.Default.DBConnectionString;
SqlConnection connection = new SqlConnection(condur);
string q1 = "select in_time from in_time where car_reg='" + comboBox1.Text + "' ";
string q2 = "select out_time from out_time where car_reg='" + comboBox1.Text + "' ";
SqlCommand command1 = new SqlCommand(q1, connection);
SqlCommand command2 = new SqlCommand(q2, connection);
try
{
connection.Open();
string q3=command1.ExecuteNonQuery().ToString();
string q4=command2.ExecuteNonQuery().ToString();
DateTime dt1 = DateTime.Parse(q3);
DateTime dt2 = DateTime.Parse(q4);
TimeSpan result = dt2 - dt1; ;
string result1 = result.ToString();
textBox3.Text = result1;
//MessageBox.Show("Insertion successful!");
//textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; comboBox1.Text = ""; comboBox3.Text = ""; textBox11.Text = ""; textBox6.Text = ""; textBox8.Text = ""; textBox9.Text = ""; richTextBox1.Text = ""; textBox4.Text="";
}
catch (Exception exp)
{
throw exp;
}
finally
{
connection.Close();
}
}
什麼問題?您顯示的代碼將成功減去這些值。 – JaredPar
好吧,那什麼都行不通,結果如何,你是否會得到例外,總的來說 - 問題是什麼? – neeKo
是q3和q4有效數據嗎?告訴我們你得到了什麼以及你的期望。 –