注册一个新用户,也可以登录,在数据库表里没有注册新用户的信息!但是VS一关,再打开就登不进去了,好像注册的用户只在本地内存中,可是UPDATE也不行啊,请高手指教,主要是是在数据库的表里没有注册新用户的信息using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace 管理系统
{
public partial class 注册 : Form
{
string connectionString;
SqlDataAdapter adapter;
//DataTable table; public 注册()
{
InitializeComponent();
connectionString = Properties.Settings.Default.lianxi1ConnectionString; } private void button2_Click(object sender, EventArgs e)
{
this.Close();
} private void button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
try
{
if (validation())
{
using (SqlConnection con = new SqlConnection(connectionString))
{
conn.Open();
//adapter = new SqlDataAdapter("select * from [user]",conn);
//DataTable table = new DataTable();
//adapter.Fill(table);
//SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
//adapter.UpdateCommand = builder.GetUpdateCommand();
cmd.CommandText = string.Format("insert into [user](username,password) values ('"+textBox1.Text.Trim()+"' ,'"+textBox2.Text.Trim()+"')");
//cmd.CommandText = string.Format("insert into [user] values('{0}','{1}')", textBox1.Text.Trim(), textBox2.Text.Trim());
//SqlCommand cmd = new SqlCommand(strSql, con);
int num = cmd.ExecuteNonQuery(); if (num == 1)
{
MessageBox.Show("注册成功!");
//adapter.Update(table);
}
else
{
MessageBox.Show("注册失败!");
}
}
} }
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
}///
///验证文本框是否为空的方法
///
public bool validation(){
if (textBox1.Text == "" || textBox2.Text=="" || textBox3.Text=="")
{
MessageBox.Show("请输入用户名和密码");
return false;
}
if (textBox2.Text.Trim() != textBox3.Text.Trim())
{
MessageBox.Show("两次输入的密码不一致");
return false;
}
else
{
return true;
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace 管理系统
{
public partial class 注册 : Form
{
string connectionString;
SqlDataAdapter adapter;
//DataTable table; public 注册()
{
InitializeComponent();
connectionString = Properties.Settings.Default.lianxi1ConnectionString; } private void button2_Click(object sender, EventArgs e)
{
this.Close();
} private void button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
try
{
if (validation())
{
using (SqlConnection con = new SqlConnection(connectionString))
{
conn.Open();
//adapter = new SqlDataAdapter("select * from [user]",conn);
//DataTable table = new DataTable();
//adapter.Fill(table);
//SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
//adapter.UpdateCommand = builder.GetUpdateCommand();
cmd.CommandText = string.Format("insert into [user](username,password) values ('"+textBox1.Text.Trim()+"' ,'"+textBox2.Text.Trim()+"')");
//cmd.CommandText = string.Format("insert into [user] values('{0}','{1}')", textBox1.Text.Trim(), textBox2.Text.Trim());
//SqlCommand cmd = new SqlCommand(strSql, con);
int num = cmd.ExecuteNonQuery(); if (num == 1)
{
MessageBox.Show("注册成功!");
//adapter.Update(table);
}
else
{
MessageBox.Show("注册失败!");
}
}
} }
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
}///
///验证文本框是否为空的方法
///
public bool validation(){
if (textBox1.Text == "" || textBox2.Text=="" || textBox3.Text=="")
{
MessageBox.Show("请输入用户名和密码");
return false;
}
if (textBox2.Text.Trim() != textBox3.Text.Trim())
{
MessageBox.Show("两次输入的密码不一致");
return false;
}
else
{
return true;
}
}
}
}
查查数据库中新注册的用户数据还在不在VS 单步调试
cmd.CommandText = string.Format("insert into [user](username,password) values ('"+textBox1.Text.Trim()+"' ,'"+textBox2.Text.Trim()+"')");如果是文本框的话应该是这样的吧
cmd.CommandText = string.Format("insert into [user](username,password) values ('"+textBox1.Text+"' ,'"+textBox2.Text+"')");
{
MessageBox.Show("注册成功!");
//adapter.Update(table);
}--------------------
这个num条件最好为
if(num>0)
其实你的sql语句并没有错误。不知道该语句在查询分析器里面是否能执行,如果可以的话,就说明问题应该存在与程序中,应该从程序中寻找问题。我也帮不了忙加油,希望你能尽快解决难题!!!