📄 form7.cs
字号:
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 Form7 : Form
{
SqlDataAdapter da;
DataSet ds;
string connectionstring = @"data source=localhost;Initial catalog=tuanweizuzhibu_inf;integrated security=SSPI;";
string commandstring = @"select * from denglu_inf";
public Form7()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{ MessageBox.Show("用户名不能为空!"); }
else
{
SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand(commandstring, conn);
SqlDataReader dr;
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
while (dr.Read())
{
if (dr.GetString(0) == textBox1.Text)
{
MessageBox.Show("用户名已存在,请重新选择用户名!");
}
else MessageBox.Show("本用户名可以使用!");
}
}
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{ MessageBox.Show("用户名不能为空!"); }
else
{
SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand(commandstring, conn);
SqlDataReader dr;
int prime = 0;
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr.GetString(0) == textBox1.Text)
{
MessageBox.Show("用户名已存在,不能添加相同用户名的信息!");
prime = 1;
break;
}
}
if (prime == 0)
try
{
da = new SqlDataAdapter(commandstring, connectionstring);
SqlCommandBuilder scb = new SqlCommandBuilder(da);
ds = new DataSet();
da.Fill(ds, "denglu_inf");
DataTable mytable = ds.Tables["denglu_inf"];
DataRow myrow = ds.Tables["denglu_inf"].NewRow();
myrow[0] = textBox1.Text.ToString();
myrow[1] = textBox2.Text.ToString();
mytable.Rows.Add(myrow);
da.Update(ds, "denglu_inf");
ds.AcceptChanges();
MessageBox.Show("注册成功!");
Form1 f1 = new Form1();
f1.Show();
this.Hide();
}
catch (SqlException ex)
{
MessageBox.Show("注册失败:" + ex.Message, "出现错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
dr.Close();
conn.Close();
}
}
}
private void button3_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.Show();
this.Hide();
}
private void Form7_Load(object sender, EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -