我有一個windows應用程序,我已經使用日期時間選擇器來進行轉換開始和轉換結束。我必須將這些時間表插入表中。我的代碼是:在c#中只插入sql表中的時間#
if (comboBox1.SelectedIndex == 0 || comboBox1.SelectedIndex == 1 || comboBox1.SelectedIndex == 2 || comboBox1.SelectedIndex == 3 || comboBox1.SelectedIndex == 4 || comboBox1.SelectedIndex == 5 || comboBox1.SelectedIndex == 6 || comboBox1.SelectedIndex == 7 || comboBox1.SelectedIndex == 8 || comboBox1.SelectedIndex == 9 || comboBox1.SelectedIndex == 10 || comboBox1.SelectedIndex == 11 || comboBox1.SelectedIndex == 12 || comboBox1.SelectedIndex == 13 || comboBox1.SelectedIndex == 14 || comboBox1.SelectedIndex == 15 || comboBox1.SelectedIndex == 16)
{
string Query = " Insert into [ICPS].[dbo].[Cau reports]([Name],[Date],[Shift Start],[Shift END ],[Overtime Start],[Overtime End],[Tasks Carried Out],[Normal ],[Overtime],[Normal 1],[Overtime1],[Comments],[Targets_(per hour)]) values ('" + this.textBox1.Text + "','" + this.Date.Text + "', CONVERT(VARCHAR(5),'" + this.SS.Text + "',108), '" + this.SE.Text + "','" + this.OS.Text + "','" + this.OE.Text + "','" + this.comboBox1.SelectedText + "','" + this.NT.Text + "','" + this.OT.Text + "','" + this.textBox2.Text + "','" + this.textBox3.Text + "','" + this.textBox4.Text + "','" + this.textBox5.Text + "') ;";
// string Query = " update [ICPS].[dbo].[Cau reports] set [Normal]='" + this.textBox7.Text + "',[Overtime]='" + this.textBox8.Text + "',[Normal 1]='" + this.textBox2.Text + "',[Overtime1]='" + this.textBox3.Text + "',[comments]='" + this.textBox4.Text + "',[Targets_(per hour)]='" + this.textBox5.Text + "'where [Tasks Carried Out] ='" + this.comboBox1.SelectedText + "'; ";
SqlConnection conDatabase = new SqlConnection(constring);
SqlCommand cmdDataBase = new SqlCommand(Query, conDatabase);
SqlDataReader myReader;
try
{
conDatabase.Open();
myReader = cmdDataBase.ExecuteReader();
MessageBox.Show("Saved");
while (myReader.Read())
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
但我的輸出來了:01/01/1900 16:36(我不需要在換擋開始日期和結束轉變僅僅只有一次)。任何幫助將非常感激。請...
您使用什麼數據類型將時間存儲在底層的Cau_reports表中? – SteveChapman
此外,有點題外話,但我建議你考慮使用SqlCommand&參數 – SteveChapman
http://xkcd.com/327/ – idstam