📄 user.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication1
{
public partial class user : Form
{ private DataSet ds = new DataSet();
private SqlDataAdapter da = new SqlDataAdapter();
public user()
{
InitializeComponent();
}
private void button11_Click(object sender, EventArgs e)
{
if (textBox15.Text != "" && textBox14.Text != "" && comboBox1.SelectedIndex != -1)
{
try
{
SqlConnection myconnection = new SqlConnection();
myconnection.ConnectionString = "server=hxl;database=studentpoints;integrated security=true ";
myconnection.Open();
SqlCommand mycommand = new SqlCommand();
mycommand.CommandText = "select * from 用户表";
// mycommand.Connection = myconnection;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = mycommand;
da.SelectCommand.Connection = myconnection;
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, "用户表");
DataTable dt = ds.Tables["用户表"];
DataRow dr = ds.Tables["用户表"].NewRow();
dr["用户名"] = textBox14.Text;
dr["密码"] = textBox15.Text;
if (comboBox1.SelectedIndex == 0) dr["ID"] = 1;
else if (comboBox1.SelectedIndex == 1) dr["ID"] = 2;
else if (comboBox1.SelectedIndex == 2) dr["ID"] = 3;
ds.Tables["用户表"].Rows.Add(dr);
da.Update(ds, "用户表");
myconnection.Close();
MessageBox.Show("添加成功!");
textBox14.Text = ""; textBox15.Text = "";
}
catch (SqlException se)
{
MessageBox.Show("该用户名已经存在!" + se.Message);
}
catch (FormatException fe)
{
MessageBox.Show("输入的ID格式不正确!" + fe.Message);
}
}
else
{
MessageBox.Show("请输入完各项数据!");
}
}
private void button14_Click(object sender, EventArgs e)
{
if (textBox17.Text != "")
{
try
{
SqlConnection myconnection = new SqlConnection();
myconnection.ConnectionString = "server=hxl;database=studentpoints;integrated security=true ";
myconnection.Open();
SqlCommand mycommand = new SqlCommand();
mycommand.CommandText = "select * from 用户表 where 用户名='" + textBox17.Text.Trim() + "'";
mycommand.Connection = myconnection;
da.SelectCommand = mycommand;
da.SelectCommand.Connection = myconnection;
da.Fill(ds, "用户表");
if (ds.Tables["用户表"].Rows.Count < 1)
{
MessageBox.Show("不存在该帐户!");
}
else
{
dataGridView6.DataSource = ds.Tables["用户表"];
button12.Enabled = true;
button14.Enabled = false;
}
textBox17.Text = "";
myconnection.Close();
}
catch (SqlException se)
{
ds.Clear();
MessageBox.Show("出现错误" + se.Message);
}
}
else
{
MessageBox.Show("请输入用户名!");
}
}
private void button12_Click(object sender, EventArgs e)
{
{
try
{
SqlCommandBuilder cb = new SqlCommandBuilder(da);
da.Update(ds, "用户表");
ds.Clear();
button12.Enabled = false;
button14.Enabled = true;
MessageBox.Show("更新成功!");
}
catch (InvalidOperationException ie)
{
MessageBox.Show("请先修改需要更改的数据!" + ie.Message);
ds.Clear();
}
catch (DBConcurrencyException de)
{
MessageBox.Show("不存在该数据" + de.Message);
ds.Clear();
}
}
}
private void button15_Click(object sender, EventArgs e)
{
if (textBox17.Text != "")
{
try
{
SqlConnection myconnection = new SqlConnection();
myconnection.ConnectionString = "server=hxl;database=studentpoints;integrated security=true ";
myconnection.Open();
SqlCommand mycommand = new SqlCommand();
mycommand.CommandText = "delete from 用户表 where 用户名='" + (textBox17.Text.Trim()) + "'";
mycommand.Connection = myconnection;
int result = mycommand.ExecuteNonQuery();
if (result > 0)
MessageBox.Show("删除成功");
else
MessageBox.Show("该用户不存在");
myconnection.Close();
}
catch (FormatException fe)
{
MessageBox.Show("输入错误" + fe.Message);
}
catch (SqlException se)
{
MessageBox.Show("出现错误!" + se.Message);
}
textBox17.Text = "";
}
else
{
MessageBox.Show("请输入用户名!");
}
}
private void user_Load(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 0;
button12.Enabled = false ;
}
private void button10_Click(object sender, EventArgs e)
{
comboBox1.SelectedIndex = -1;
textBox14 .Text ="";
textBox15 .Text ="";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void groupBox8_Enter(object sender, EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -