using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;public partial class zc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string bh = TextBox1.Text;
        string mm = TextBox2.Text;
        string hh = TextBox3.Text;
        string yx = youxiang.Text;
        string sex;
        string constr = "server=localhost;uid=sa;pwd=111111;database=bh";
        SqlConnection con = new SqlConnection(constr);        if(RadioButtonList1.Items[0].Selected)
        sex=RadioButtonList1.Items[0].Value;
        else
        sex=RadioButtonList1.Items[1].Value;        string cmdstr = "insert  bh values('" + bh + "','" + mm + "','" + hh + "','" + yx + "','"+ sex +"')";
        SqlCommand cmd = new SqlCommand(cmdstr, con);
        con.Open();
        try
        {
            cmd.ExecuteNonQuery();
        }        catch (Exception err) { Label1.Text = err.Message; }
        finally
        {
            con.Close();Response.Redirect("logion1.aspx");
        }
        
    }
}
为什么插不进数据库的表中,请高手指点,多谢多谢!!!!

解决方案 »

  1.   

     string cmdstr = "insert into bh values('" + bh + "','" + mm + "','" + hh + "','" + yx + "','"+ sex +"')";
      

  2.   

    另外
    if(RadioButtonList1.Items[0].Selected)
      sex=RadioButtonList1.Items[0].Value;
      else
      sex=RadioButtonList1.Items[1].Value;其实只要
    sex=RadioButtonList1.SelectedValue;
    一句就行了。
      

  3.   

    掉了 into ,规范为:insert into tablename(各列明)values(各列对应的值)
    设断点,将SQL语句放到数据库中执行,很快就能找到错误所在。
      

  4.   

    1楼和3楼的回答没有效果,into是可有可无的,不是必需的,不过谢谢你们的热心。二楼的回答还是不行,仍然插不进数据。同样,还是谢谢你!!
      

  5.   

    你先调试。监视sql语句。把这个放到查询分析器里面执行。看行不。另外,建议规范下sql语句。 insert into 表名(字段一,字段二) values(值一,值二)
      

  6.   

    把你拼出来的 SQL 语句放到查询分析器中执行一下,就知道是什么问题了。另外,如果这是你正式的代码的话,有一些问题是需要注意的:
    第一、插入数据时,最好带上表的字段名,如 Insert 表名(字段1, 字段2) Values(值1, 值2),否则表中一旦增加新的字段,你这段程序就运行不了了。
    第二、即使不强求使用参数化的插入方式,至少要对拼接的字符串值做一下单引号的转义吧,很简单的,只要 str.Replace("'", "''") 就可以了。否则,很容易就可以做 SQL 注入了。
      

  7.   

    string cmdstr = "insert bh values('" + bh + "','" + mm + "','" + hh + "','" + yx + "','"+ sex +"')";要指明 bh的字段。insert bh(字段一,字段二) values('" + bh + "','" + mm "')";