protected void Button1_Click(object sender, EventArgs e)
{
string text1 = TextBox3.Text;//用户名
string text2 = TextBox4.Text;//密码
string text3 = TextBox1.Text;//email
string text4 = TextBox2.Text;//telphone
SqlConnection myconn = new SqlConnection();
string cmdtext = "insert into register values(" + "'" + text1 + "'" + "," + "'" + text2 + "'" + "," + "'" + text3 + "'"+ "," + "'" + text4 + "'"+")";
// string cmdtext = "select * from login where username =" + "'" + text1 + "'";
string cmdtext0 = "select * from register where username =" + "'" + text1 + "'";
SqlCommand mycommand = new SqlCommand(cmdtext0, myconn);
myconn.ConnectionString = myconn_str;
int index=0;
// string output = "<table border=1>";
myconn.Open();
index = mycommand.ExecuteNonQuery(); // ------------------>index = -1
// MessageBox.Show("index =" + index.ToString());
if (index == 0) //名字没有被注册
{
mycommand.CommandText = cmdtext;
index = mycommand.ExecuteNonQuery();
if (index == 1)
{
MessageBox.Show("Register successfully");
}
}
else if(index == 1)
{
MessageBox.Show("Your name is already existed!");
empty();
}
else
{
MessageBox.Show("system exception");
}
myconn.Close();
}
你想问什么问题
index = mycommand.ExecuteNonQuery(); // ------------------>index = -1这里写得不对,
SQL语句 查询 不是ExecuteNonQuery ,你应该把结果存在DataSet,DataTable中。然后查询DataTable中有没有数据,来判断 这个人有没有已经注册
如果用 插入语句index = 1 怎么解释
mycommand.ExecuteNonQuery(); 是返回一个整型数据
@gzjiuying:
string cmdtext0 = "select * from register where username =" + "'" + text1 + "'";
SqlCommand mycommand = new SqlCommand(cmdtext0, myconn);
你没发现么?这里你使用的是查询语句 ,不是插入语句。
你查询的话 ,就不要用ExecuteNonQuery,ExecuteNonQuery 返回值是 "受影响的行数"。
@gzjiuying: 我的5个园豆呢?怎么不给我
@需要格局: 好,给你!