我是个新手,大家帮我看看是哪错了,总也写不到数据库中去
string lmmc = this.DropDownList2.SelectedValue;
string title = this.TextBox3.Text.Trim();
string writer = this.TextBox4.Text.Trim();
DateTime time = DateTime.Now;
string isjd = this.DropDownList1.SelectedValue;
string content = this.TextBox6.Text.Trim(); using (SqlConnection conn = new SqlConnection(SQLHelper.ConnectionString))
{
SqlCommand myCommand = new SqlCommand();
myCommand.CommandText = "insert into newsinfo(lmmc,title,writer,publishtime,isjd,content) values('" + lmmc + "','" + title + "','" + writer + "'," + time + ",'" + isjd + "','" + content + "')";
//string strsql = "insert into newsinfo(lmmc,title,writer,publishtime,isjd,content) values ('" + lmmc + "','" + title + "','" + writer + "'," + time + ",'" + isjd + "','" + content + "')"; myCommand.Connection = conn;
conn.Open();
myCommand.ExecuteNonQuery();
conn.Close();
}
string lmmc = this.DropDownList2.SelectedValue;
string title = this.TextBox3.Text.Trim();
string writer = this.TextBox4.Text.Trim();
DateTime time = DateTime.Now;
string isjd = this.DropDownList1.SelectedValue;
string content = this.TextBox6.Text.Trim(); using (SqlConnection conn = new SqlConnection(SQLHelper.ConnectionString))
{
SqlCommand myCommand = new SqlCommand();
myCommand.CommandText = "insert into newsinfo(lmmc,title,writer,publishtime,isjd,content) values('" + lmmc + "','" + title + "','" + writer + "'," + time + ",'" + isjd + "','" + content + "')";
//string strsql = "insert into newsinfo(lmmc,title,writer,publishtime,isjd,content) values ('" + lmmc + "','" + title + "','" + writer + "'," + time + ",'" + isjd + "','" + content + "')"; myCommand.Connection = conn;
conn.Open();
myCommand.ExecuteNonQuery();
conn.Close();
}
也就是('" + lmmc + "','" + title + "','" + writer + "'," + time + ",'" + isjd + "','" + content + "')";
这句中time前必须加引号,应该是如下语句:
('" + lmmc + "','" + title + "','" + writer + "','" + time + "','" + isjd + "','" + content + "')";
这里报错
using (SqlConnection conn = new SqlConnection(SQLHelper.ConnectionString))
{
SqlCommand myCommand = new SqlCommand();
myCommand.CommandText = "insert into newsinfo(title) values(@title)";
myCommand.Parameters.Add("@title", SqlDbType.VarChar, 50);
myCommand.Parameters["@title"].Value = title ;
myCommand.Connection = conn;
conn.Open();
myCommand.ExecuteNonQuery();
conn.Close();
}