datetime
을 SQL 데이터베이스에 저장하려고합니다. 그 목적을 위해 datetime2(0)
변수를 사용합니다.문자열에서 날짜 및/또는 시간을 변환 할 때 변환하지 못했습니다.
protected void InsertDB(string title, string desc, string cat, string path)
{
string now = DateTime.Now.ToString("dd-MM-yyyy h:mm:ss tt");
title = title.Length == 0 ? "Untitled" : title;
cat = cat.Length == 0 ? "Uncategorized" : cat;
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
try
{
SqlCommand cmd = new SqlCommand(@"INSERT INTO gallery (img_title, img_desc, img_cat, img_date, img_path)
VALUES (@title, @desc, @cat, @date, @path)", con);
con.Open();
cmd.Parameters.AddWithValue("@title", title.Trim());
cmd.Parameters.AddWithValue("@desc", desc.Trim());
cmd.Parameters.AddWithValue("@cat", cat.Trim());
cmd.Parameters.AddWithValue("@date", now.Trim());
cmd.Parameters.AddWithValue("@path", path.Trim());
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
msg_lbl.Visible = true;
msg_lbl.Text = "New Image is uploaded.";
title_txt.Text = "";
desc_txt.Text = "";
cat_txt.Text = "";
}
else
{
msg_lbl.Visible = true;
msg_lbl.Text = "Error occured.";
}
}
catch (SqlException ex)
{
msg_lbl.Visible = true;
msg_lbl.Text = ex.Message; //I get this exception here
}
catch (Exception ex)
{
msg_lbl.Visible = true;
msg_lbl.Text = ex.Message;
}
}
, 그것은 작동합니다. ** 우리는 ** 날짜 형식의 개념을 가지고 있습니다. 내부적으로 .net 및 SQL Server는 원하는 방식으로 저장하고 필요에 따라 프레젠테이션 문제를 걱정합니다. – billinkc