using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {        }        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=gk");
            conn.Open();
            string name = username.Text;
            string pwd = password.Text;
            string email1 = email.Text;
            string q=qq.Text;
            SqlCommand cmd = new SqlCommand("insert into register(username,pwd,email,QQ) values('"+name+"','"+pwd+"','"+email1+"','"+q+"')");
            cmd.ExecuteNonQuery();
            conn.Close();
        }
    }
}

解决方案 »

  1.   

    sql语句提取出来直接拿到sql里面执行看看
      

  2.   

    SqlCommand  貌似要指定连接字符串的吧SqlCommand cmd=new SqlCommand("select * from XXX",conn)
      

  3.   

    加上cmd.Connection = conn;            
    SqlCommand cmd = new SqlCommand("insert into register(username,pwd,email,QQ) values('"+name+"','"+pwd+"','"+email1+"','"+q+"')");
      

  4.   

    是在下面加写反了
    SqlCommand cmd = new SqlCommand("insert into register(username,pwd,email,QQ) values('"+name+"','"+pwd+"','"+email1+"','"+q+"')");
    加上cmd.Connection = conn;   
      

  5.   

    你的SqlConnection可以打开?我怎么看着server没赋值啊
      

  6.   

    SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=gk");
    conn.Open();
    string name = username.Text;
    string pwd = password.Text;
    string email1 = email.Text;
    string q=qq.Text;
    SqlCommand cmd = new SqlCommand("insert into register(username,pwd,email,QQ) values('"+name+"','"+pwd+"','"+email1+"','"+q+"')");
    cmd.Connection = conn;//增加这行代码
    int count = cmd.ExecuteNonQuery();//看函数的返回值,SqlCommand.ExecuteNonQuery 方法对连接执行 Transact-SQL 语句并返回受影响的行数。
    conn.Close();
      

  7.   

    SqlCommand comm = new SqlCommand(SQl, Conn);
      

  8.   

     SqlCommand cmd = new SqlCommand("insert into register(username,pwd,email,QQ) values('"+name+"','"+pwd+"','"+email1+"','"+q+"')",conn);
      

  9.   


    SqlCommand cmd = new SqlCommand("insert into register(username,pwd,email,QQ) values('"+name+"','"+pwd+"','"+email1+"','"+q+"')",conn);
      

  10.   

    增加这行代码 cmd.Connection = conn;
    或者改成  SqlCommand cmd = new SqlCommand("insert into register(username,pwd,email,QQ) values('"+name+"','"+pwd+"','"+email1+"','"+q+"')",conn);