我错在那?我错在那?我究竟错在那?
能不能帮我看一下那错了。。为什么不行嗯?+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=
protected void Button1_Click1(object sender, EventArgs e)
{
string tb_UserName = this.tb_UserName.Text.ToString();
string tb_Message = this.tb_Message.Text.ToString();
string posttime = DateTime.Now.ToString();
string strConnection = "user id=sa;password=;"; strConnection += "initial catalog=GuestBook;Server=hqd-pc;"; strConnection += "Connect Timeout=30";
string sql = "insert into tbguestbook (username,posttime,message) values ('" + tb_UserName + "','" + posttime + "','" + tb_Message + "')";
SqlCommand cmd = new SqlCommand(sql);
cmd.ExecuteNonQuery();
Response.Write("<script>alert('留言成功!');location,href='login3.aspx';</script>"); --------------------编程问答-------------------- 页面错误提示什么? --------------------编程问答-------------------- 用单步调试进行测试 --------------------编程问答-------------------- 用单步调试进行测试 --------------------编程问答-------------------- string strConnection = "user id=sa;password=;"; strConnection += "initial catalog=GuestBook;Server=hqd-pc;"; strConnection += "Connect Timeout=30";
string sql = "insert into tbguestbook (username,posttime,message) values ('" + tb_UserName + "','" + posttime + "','" + tb_Message + "')";
这里面拼接有问题 先看报错提示是不是连接不上数据库?
其次看SQL语句对不对 我感觉你这字符串有问题! --------------------编程问答-------------------- 表中的数据类型和你插入的类型是否相同呢?最好把错误提示贴出来啊。。。 --------------------编程问答-------------------- 报什么错?
你这样不怕注入?
--------------------编程问答-------------------- 其实我建议初学的时候还是应该报一本入门书的。这样容易点。 --------------------编程问答-------------------- 标题好吸引人 --------------------编程问答-------------------- 标题好吸引人 --------------------编程问答-------------------- ADO.NET的基本格式如下:
string connString = "Data Source=xxx;Initial Catalog=yyy;Integrated Security=True";
string sql = "insert......";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
你在你的程序里构建了连接字符串,但没建立连接对象(SqlConnection),也没打开连接。
除此之外,日期字段容易出错。
补充:.NET技术 , 非技术区