第2个回答 2013-05-31
WorldNewsAdd.cs源码:
public static bool updateWorldNews(WorldNewsType t)
{
try
{
SqlConnection con = DB.createCon();
con.Open();
SqlCommand cmd = new SqlCommand("update WorldNews set WorldNewsDate='" + t.WorldNewsDate + "' where WorldNewsID='" + t.WorldNewsID + "'", con);
cmd.ExecuteNonQuery();
return true;
}
catch (Exception e)
{
return false;
}
}
更新页update.aspx.cs后台代码:
protected void btnUpdate_Click(object sender, EventArgs e)
{
string strID = Request.QueryString["WorldNewsID"];
WorldNewsType t = new WorldNewsType();
t.WorldNewsID = strID;
if (this.txtWorldNewsDate.Text == null || this.txtWorldNewsDate.Text == "")
{
t.WorldNewsDate = DBNull.Value.ToString();
}
else
{
t.WorldNewsDate = this.txtWorldNewsDate.Text;
}
if (WorldNewsAdd.updateWorldNews(t))
{
Response.Write("成功");
}
else
{
Response.Write("失败");
}
要实现文本框接收的值为空值(NULL)时,把空值(NULL)更新回数据库.
WorldNewsDate的数据类型是DateTime,用我上面的代码更新回数据库时,如果文本框是空值会往数据库里写进一个1900-1-1 0:00:00的时间值...