日期:2014-05-18  浏览次数:20369 次

帮我看看这个 insert 为什么插入不进去
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Zhuce : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {

  }
  protected void botton1_Click(object sender, EventArgs e)
  {
  try
  {
  string insert = "insert into [Q_user](userid,password,email,gongsi,province,city,phone,mphone,zcdata)";
  insert += " values (@userid,@password,@email,@gongsi,@province,@city,@phone,@mphone,@zcdata)";
  SqlConnection conn = new SqlConnection("server=.;database=a123;user id=sa;password=sa");
  conn.Open();
  SqlCommand cmd = new SqlCommand(insert, conn);
  cmd.Parameters.Add("@userid", SqlDbType.VarChar).Value = this.username.Text.Trim();
  cmd.Parameters.Add("@userid", SqlDbType.VarChar).Value = this.pwdbox.Text.Trim();
  cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = this.emailbox.Text.Trim();
  cmd.Parameters.Add("@gongsi", SqlDbType.VarChar).Value = this.gsname.Text.Trim();
  cmd.Parameters.Add("@province", SqlDbType.VarChar).Value = this.province.Text.Trim();
  cmd.Parameters.Add("@city", SqlDbType.VarChar).Value = this.city.Text.Trim();
  cmd.Parameters.Add("@phone", SqlDbType.VarChar).Value = this.phone.Text.Trim();
  cmd.Parameters.Add("@mphone", SqlDbType.VarChar).Value = this.mphoneBox.Text.Trim();
  cmd.Parameters.Add("@zcdata", SqlDbType.DateTime).Value = DateTime.Now;
  cmd.ExecuteNonQuery();//这个是插入语句吗?
  cmd.Clone();
  conn.Close();
  }
  catch
  { }
  }
}

------解决方案--------------------
程序有没有出错呢
你的cmd.parameters.Add("@userid")重复了次 但是密码没有赋值
------解决方案--------------------
cmd.Parameters.Add("@userid", SqlDbType.VarChar).Value = this.username.Text.Trim(); 
cmd.Parameters.Add("@userid", SqlDbType.VarChar).Value = this.pwdbox.Text.Trim(); 
重复了,还有就是不是varchar类型
改成SqlDbType.Int
------解决方案--------------------
cmd.Parameters.Add("@userid", SqlDbType.VarChar).Value = this.pwdbox.Text.Trim(); 
改为
cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = this.pwdbox.Text.Trim(); 


------解决方案--------------------
除了上面的问题,还有如下:
如果你连的是SQL Server数据库,那么SqlConnection conn = new SqlConnection("server=.;database=a123;user id=sa;password=sa"); 
应该为SqlConnection conn = new SqlConnection("server=.\sqlexpress;database=a123;user id=sa;password=sa");