データベースから2つの値を文字列として取ります。今私はこれらの2つの値を減算したい。私を助けて、事前に感謝してください。 C#で2つのDateTime
値を減算するC#で時間を引く方法
TimeSpan result = dt2 - dt1;
、あなたは、単に減算演算子を使用します。
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の有効なデータですか?あなたが得ていることと期待していることを私たちに教えてください。 –