protected void BT_SubTz_Click(object sender, EventArgs e)
{
string title = TB_Title.Text.Trim();
string words = TB_Words.Text.Trim();
string companyname;
string SQLstr; SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
int type = Convert.ToInt32(DDL_Type.SelectedValue.ToString());
if (type != 1)
{
TB_CompanyName.Visible = false;
companyname = "指导员";
}
else
{
companyname = TB_CompanyName.Text.Trim();
} SQLstr = "insert into tb_Content(subject,words,UserName,CreateTime,LastAnswerTime,HitCount,Type,CompanyName)"
+ "values('" + title + "','"
+ words + "',"
+ "'" + Session["UserName"].ToString() + "',"
+ DateTime.Now +","
+ DateTime.Now +","
+ "0," + type + ","
+ "'" + companyname + "')";
SqlCommand myCmd = new SqlCommand(SQLstr, conn);
try
{
conn.Open();
myCmd.ExecuteNonQuery();
}
catch
{
throw new Exception("连接服务器出错");
}
finally
{
myCmd.Dispose();
conn.Close();
}
}这是一个向数据库插入数据的函数,但是提示错误,不解,是因为DateTime插入格式的问题?数据库DateTime
{
string title = TB_Title.Text.Trim();
string words = TB_Words.Text.Trim();
string companyname;
string SQLstr; SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
int type = Convert.ToInt32(DDL_Type.SelectedValue.ToString());
if (type != 1)
{
TB_CompanyName.Visible = false;
companyname = "指导员";
}
else
{
companyname = TB_CompanyName.Text.Trim();
} SQLstr = "insert into tb_Content(subject,words,UserName,CreateTime,LastAnswerTime,HitCount,Type,CompanyName)"
+ "values('" + title + "','"
+ words + "',"
+ "'" + Session["UserName"].ToString() + "',"
+ DateTime.Now +","
+ DateTime.Now +","
+ "0," + type + ","
+ "'" + companyname + "')";
SqlCommand myCmd = new SqlCommand(SQLstr, conn);
try
{
conn.Open();
myCmd.ExecuteNonQuery();
}
catch
{
throw new Exception("连接服务器出错");
}
finally
{
myCmd.Dispose();
conn.Close();
}
}这是一个向数据库插入数据的函数,但是提示错误,不解,是因为DateTime插入格式的问题?数据库DateTime
http://bbs.csdn.net/topics/350119289
不要拼字符串了~另外学学3层吧~~学了3层后再学学EF吧~
建议使用默认的日期格式,yyyy-mm-dd Thh:mm:ss.mmm ,这样无论sql server被设置成什么格式,什么语言,都可以识别.一下来自msdn:Examples:
2004-05-23T14:25:10
2004-05-23T14:25:10.487
To use the ISO 8601 format, you must specify each element in the format. This also includes the T, the colons (:), and the period (.) that are shown in the format.
The brackets indicate that the fraction of second component is optional. The time component is specified in the 24-hour format.
The T indicates the start of the time part of the datetime value.
The advantage in using the ISO 8601 format is that it is an international standard with unambiguous specification. Also, this format is not affected by the SET DATEFORMAT or SET LANGUAGE setting.http://msdn.microsoft.com/en-us/library/ms187819.aspx
+ "values('" + title + "','"
+ words + "',"
+ "'" + Session["UserName"].ToString() + "','"
+ DateTime.Now +"','"
+ DateTime.Now +"',"
+ "0," + type + ","
+ "'" + companyname + "')";
+ DateTime.Now +"','"
+ DateTime.Now +"','"
SQLstr = "insert into tb_Content(subject,words,UserName,CreateTime,[LastAnswerTime],HitCount,[Type],CompanyName)"
+ "values('" + title + "','"
+ words + "',"
+ "'" + Session["UserName"].ToString() + "','"
+ DateTime.Now.ToString() +"','"
+ DateTime.Now.ToString() +"',"
+ "0," + type + ","
+ "'" + companyname + "')";
那你以后怎么做report和统计啊。看我8,9楼的回答,以字符串格式输入,格式为DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss")
那你以后怎么做report和统计啊。看我8,9楼的回答,以字符串格式输入,格式为DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss")看帖子以后正往回改呢有得必有失嘛,投机取巧也是不得已而为之