{
if (Check(TextBox1.Text) || Check(TextBox2.Text) || Check(TextBox3.Text) ||
Check(TextBox4.Text) ) //判断
{
Label8.Text = "用户信息中不能够包含特殊字符如,',//,\\等,请审核"//输出信息
}
else
{
else
{
try
{
SqlConnection con =
new SqlConnection("server='(local)'database='Register'uid='sa'pwd='sa'")//建立连接
con.Open()//打开连接
string strsql =
"insert into register (username,password,QQ,Email) values
('" + TextBox1.Text + "','" + TextBox2.Text + "','" +
TextBox3.Text + "','" + TextBox4.Text + "',0)"
SqlCommand cmd = new SqlCommand(strsql,con)//创建执行
cmd.ExecuteNonQuery()//执行SQL
Label8.Text = "注册成功,请牢记您的信息"//提示成功
}
catch
{
Label8.Text = "出现错误信息,请返回给管理员"//抛出异常
}
}
}
protected bool Check(string text) //判断实现
{
if (text.Contains("") || text.Contains("'") ||
text.Contains("//") || text.Contains("\\")) //检查字串
{
return true//返回真
}
else
{
return false//返回假
}
}
检测注册名重复的代码:
string check = "select * from register where username='" + TextBox1.Text + "'"
SqlDataAdapter da = new SqlDataAdapter(check,con)//创建适配器
DataSet ds = new DataSet()//创建数据集
da.Fill(ds, "table")//填充数据集
if (da.Fill(ds, "table") >0) //判断同名
{
Label8.Text = "注册失败,有相同用户名"//输出信息
}
else
{
SqlCommand cmd = new SqlCommand(strsql, con)//创建执行对象
cmd.ExecuteNonQuery()//执行SQL
Label8.Text = "注册成功,请牢记您的信息"//输出成功
}
string constr = "server=.database=Graduationuid=123pwd=123"//连接字符串里包含了数据库信息SqlConnection conn = new SqlConnection(constr)//创建数据库连接
conn.Open()//打开连接
//SqlDataAdapter sda = new SqlDataAdapter()//SqlDataAdapter是数据适配器,是数据库和调用者之间的桥梁
SqlCommand cmd = new SqlCommand() //SqlCommand表示对数据库要执行的 *** 作命令。
cmd.CommandText = "select*from Login"//cmd要执行的sql *** 作语句
cmd.Connection = conn//cmd对应的连接
SqlDataReader reader = cmd.ExecuteReader()
while(reader.Read())
{
string username=reader["Name"].ToString()
string password=reader["password"].ToString()
//Trim()表示把字符串前后的空格都去除。不然有空格会干扰判断。
if (this.textBox1.Text .Trim()==username.Trim() && password.Trim () ==this.textBox2.Text.Trim() )
{
Form1 f = new Form1()
f.Show() //d出Form1这个窗体
}
else
{
MessageBox.Show("用户名或密码错误,请重新输入")
this.textBox2.Text = ""
this.textBox1.Text = ""
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)