我是个新手,大家帮我看看是哪错了,总也写不到数据库中去
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();
            
        }

解决方案 »

  1.   

     myCommand.CommandText = "insert into newsinfo(lmmc,title,writer,publishtime,isjd,content) values('" + lmmc + "','" + title + "','" + writer + "','" + time + "','" + isjd + "','" + content + "')"; 
      

  2.   

    朋友,即使是datetime类型也要像字符串那样处理,
    也就是('" + lmmc + "','" + title + "','" + writer + "'," + time + ",'" + isjd + "','" + content + "')"; 
    这句中time前必须加引号,应该是如下语句:
    ('" + lmmc + "','" + title + "','" + writer + "','" + time + "','" + isjd + "','" + content + "')"; 
      

  3.   

    myCommand.ExcuteNonQuery();
    这里报错
      

  4.   

    最好用SqlParameter,不要这样拼sql串。string title = this.TextBox3.Text.Trim(); 
    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(); 
    }