2015-04-12 44 views
0

我想通過將其轉換爲dateTime將日期插入到Session DateValue的數據庫中。我面臨的問題是它接受四月份的價值,但是當我輸入三月份的價值錯誤時。 請幫幫忙,我已經使用了查詢和代碼如下:SQL查詢插入數據到數據庫

string a = Session["Date_Value"].ToString(); 
DateTime date= DateTime.Parse(a); 

foreach (GridViewRow g1 in grdData.Rows) 
     { 

      //string Status = (g1.FindControl("TextBox1") as TextBox).Text; 
      //int Status = Convert.ToInt32(Sta); 

      SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Real_Attendance"].ConnectionString); 

      SqlCommand cmd = new SqlCommand("Insert into Attendanc(Stu_id,Status,time,Date,Sub_id) values('" + g1.Cells[3].Text + "',@Status,'" + Session["Time_Value"].ToString() + "', @Date ,'" + Session["Sub_id"].ToString() + "')", con); 
      //SqlCommand cmd = new SqlCommand("Insert into Attendanc(Stu_id,Status,time,Date,Sub_id) values('" + g1.Cells[3].Text + "','"+ g1.Cells[1].Text +"','" + Session["Time_Value"].ToString() + "','" + Session["Date_Value"].ToString() + "','" + Session["Sub_id"].ToString() + "')", con); 
      //cmd.Parameters["@Status"].Value = ((Label)(g1.FindControl("Label1"))).Text; 
      cmd.Parameters.AddWithValue("@Status", ((Label)(g1.FindControl("Label1"))).Text); 
      cmd.Parameters.AddWithValue("@Date", date.ToShortDateString()); 
      // cmd.Parameters.AddWithValue("@Date", date.ToShortDateString()); 

      con.Open(); 

      cmd.ExecuteNonQuery(); 
      con.Close(); 

}

這是我用來創建會話的代碼。我在webform3創建會話,並在webform4

protected void Calendar1_SelectionChanged(object sender, EventArgs e) 
    { 
     Calendar1.Visible = false; 
     System.DateTime myDate = new System.DateTime(); 
     myDate = Calendar1.SelectedDate; 
     txtDate.Text = myDate.ToString("dd/MM/yyyy"); 
     Date = txtDate.Text; 
     day = Calendar1.SelectedDate.DayOfWeek.ToString(); 
     Response.Write(day); 
     txtday.Text = day; 
     Session["Date_Value"] = Date; 
     //SelectTime(); 

    } 
+0

你在哪裏以及如何在會話中設置日期值?請提供代碼 –

+0

你得到哪個錯誤? – Legends

+0

從字符串轉換日期和/或時間時轉換失敗。 – user2747666

回答

0

使用它的價值我不知道這將解決你的問題,但它會提高你的代碼:

string a = Session["Date_Value"].ToString(); 
DateTime date= DateTime.Parse(a); 
using(SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Real_Attendance"].ConnectionString)) 
{ 
    con.Open(); 
    foreach (GridViewRow g1 in grdData.Rows) 
    { 

     using(SqlCommand cmd = new SqlCommand("Insert into Attendanc (Stu_id,Status,time,Date,Sub_id) values (@Stu_id, @Status, @Time, Convert(DateTime, @Date, 103), @Sub_id)", con)) 
     { 
      cmd.Parameters.AddWithValue("@Stu_id", g1.Cells[3].Text); 
      cmd.Parameters.AddWithValue("@Status", ((Label)(g1.FindControl("Label1"))).Text); 
      cmd.Parameters.AddWithValue("@Time", Session["Time_Value"].ToString()); 
      cmd.Parameters.AddWithValue("@Date", date.ToString("dd/MM/yyyy")); 
      cmd.Parameters.AddWithValue("@Sub_id", Session["Sub_id"].ToString()); 
      cmd.ExecuteNonQuery(); 
     } 
    } 
    con.Close(); 
} 

首先,我已經改變sql語句只有參數。它更清潔,更安全,更易於閱讀和調試。

其次,我已將@Date參數的值更改爲特定日期格式,並使用Convert函數將預期格式通知數據庫。這應該可以解決您的轉換問題。

其他改進:我已經移動了foreach循環之外的連接的打開和關閉。不需要關閉並重新打開每個命令執行的連接。另外,我已經將SqlConnection和SqlCommand移動到using語句中,正如Microsoft推薦的那樣。