+ -
当前位置:首页 → 问答吧 → 求高手帮助解决数据库数据插不进的问题

求高手帮助解决数据库数据插不进的问题

时间:2011-12-09

来源:互联网

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");
  }
   
  }
}
为什么插不进数据库的表中,请高手指点,多谢多谢!!!!

作者: jiangxinping   发布时间: 2011-12-09

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

作者: dalmeeme   发布时间: 2011-12-09

另外
if(RadioButtonList1.Items[0].Selected)
  sex=RadioButtonList1.Items[0].Value;
  else
  sex=RadioButtonList1.Items[1].Value;

其实只要
sex=RadioButtonList1.SelectedValue;
一句就行了。

作者: dalmeeme   发布时间: 2011-12-09

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

作者: woshisadshiwo   发布时间: 2011-12-09

相关阅读 更多

热门下载

更多