protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string strDataCon = "Data Source=.;Initial Catalog=db_Mysite;User ID=sa";
       // string strTime = (string)DateTime.Today ;
        
      
        int count=0;
        SqlConnection mycon = new SqlConnection(strDataCon);
        mycon.Open();
     
        SqlCommand cmd = new SqlCommand("select *from tb_news", mycon);
        SqlDataReader myrd = cmd.ExecuteReader();
        while (myrd.Read())
            count++;
 
       string strcmd = "insert into tb_news(newsid,newsheader,newscontent,newtime) values( count.ToString() ,'" + txtTitle.Text + "','" + txtContent.Text + "',DateTime.Now.ToShortTimeString())";
        myrd.Close();
        cmd = new SqlCommand(strcmd, mycon);
       if( cmd .ExecuteNonQuery ()>0)
        Response .Write ("<script>alert('插入成功!')</script>");
      mycon.Close();
      
        
       
    }
请问这个代码错在什么地方了。报错是在红色部分

解决方案 »

  1.   

    string strcmd=string.Format(insert into tb_news(newsid,newsheader,newscontent,newtime) values({0},'{1}',{2},{3})",count.ToString(),txtTitle.Text,txtContent.Text,DataTime.Now.ToShortTimeString());
    估计是不是拼接字符串有错误,有的有单引号,有的没有,用上面那种方式,有单引号的就加上单引号。
    我没有用工具,就是手动写的,可能有错误。
      

  2.   

    string strcmd = "insert into tb_news(newsid,newsheader,newscontent,newtime) values( '"+count.ToString()+"','" + txtTitle.Text + "','" + txtContent.Text + "','"+DateTime.Now.ToShortTimeString()+"')";