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();
}
请问这个代码错在什么地方了。报错是在红色部分
{
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();
}
请问这个代码错在什么地方了。报错是在红色部分
估计是不是拼接字符串有错误,有的有单引号,有的没有,用上面那种方式,有单引号的就加上单引号。
我没有用工具,就是手动写的,可能有错误。