写在前面的基础工作
本案例是基于microsoft sql server 2017完成,下载并安装好microsoft sql server 2017以及sql server management studio,然后按下图步骤进行设置sql server management studio的配置
-
首先利用windows身份验证连接数据库引擎
-
-
右键资源管理器的连接项,选择属性
-
-
单击左侧 安全性 选项卡,选中图中框住的内容,点击确定
-
-
选择图中的sa用户,右键选择属性
-
- 跳出来的是登陆属性页,你可以在这里修改你的密码,用来以后通过代码访问数据库,同时取消勾选 强制实施密码策略 复选框;在左侧选择 状态,选中登录的已启用单选按钮。设置完后单击确定
- 跳出来的是登陆属性页,你可以在这里修改你的密码,用来以后通过代码访问数据库,同时取消勾选 强制实施密码策略 复选框;在左侧选择 状态,选中登录的已启用单选按钮。设置完后单击确定
-
下次登陆你就可以通过server用户名和密码登陆啦
-
本例的目的
实现1.添加用户,2.修改已存在的用户密码,3.删除用户信息的操作
- 相关设计界面控件
- 代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;//使用命名空间
namespace WindowsFormsApp19
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection contest;
//连接数据库对象
SqlCommand cmdLoginOP;
//执行SQL命令对象
string cmdString;
/// <summary>
/// SQL语句字符串,判断指定的用户名是否存在,如果存在返回true,否则false
/// </summary>
/// <param name="Name"></param>
/// <returns></returns>
bool IsExist(string Name)
{
string str = string.Format($"select count(*) from Table_2 " +
$"where Name= '{Name}'");
//所以你需要在你的test的数据库下新建一个表2
cmdLoginOP = new SqlCommand(str, contest);
contest.Open();
int count = Convert.ToInt32(cmdLoginOP.ExecuteScalar());
//查询该用户名的个数
contest.Close();
if(count!=0)
{
return true;
}
return false;
}
/// <summary>
/// 执行SQL命令,执行成功返回true,否则false
/// </summary>
/// <param name="CmdStr"></param>
/// <returns></returns>
bool GetSqlCmd(string CmdStr)
{
cmdLoginOP = new SqlCommand(CmdStr, contest);
contest.Open();
int count = cmdLoginOP.ExecuteNonQuery();
//执行SQL命令并返回受影响行数
contest.Close();
if(count!=0)
{
return true;
}
return false;
}
private void Form1_Load(object sender, EventArgs e)
{
string ConString = "server=.;database=test;uid=sa;pwd=sa";
//所以你需要在你的数据库下创一个test的新库
//这里的用户名和密码要跟你自己之前设置的一样
contest = new SqlConnection(ConString);
}
/// <summary>
/// 选项卡选择页面更改事件执行代码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if(tabControl1.SelectedIndex==0)
{
groupBox1.Parent = tabControl1.TabPages[0];
//将组中对象放入第1个页面
}
if(tabControl1.SelectedIndex == 1)
{
groupBox1.Parent = tabControl1.TabPages[1];
//将组中对象放入第2个页面,所以第二个页面不用加控件
}
}
/// <summary>
/// 保存按钮单击事件执行代码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
if(textBox1.Text==string.Empty||textBox2.Text==string.Empty)
{
MessageBox.Show("用户名和密码不能为空!");
return;//中止该方法
}
string StuName = textBox1.Text.Trim();
string StuPass = textBox2.Text.Trim();
if(tabControl1.SelectedIndex==0)
//组合框内控件在添加界面
{
cmdString = string.Format($"insert into Table_2 values('{StuName}','{StuPass}')");
if(IsExist(StuName))
{
MessageBox.Show("对不起,该用户名已经存在!");
}
else
{
if(GetSqlCmd(cmdString))
{
MessageBox.Show("添加用户成功!");
textBox1.Text = "";
textBox2.Text = "";
textBox1.Focus();//获取光标
}
}
}
//组合框内控件在修改界面
if (tabControl1.SelectedIndex==1)
{
cmdString = string.Format($"update Table_2 set Name='{StuName}'," +
$"Pass='{StuPass}' where Name='{StuName}'");
if (!IsExist(StuName))
{
MessageBox.Show("对不起,修改的用户名不存在!");
}
else
{
if (GetSqlCmd(cmdString))
{
MessageBox.Show("修改用户成功!");
textBox1.Text = "";
textBox2.Text = "";
textBox1.Focus();//获取光标
}
}
}
}
/// <summary>
/// 退出按钮单击事件执行代码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
contest.Dispose();
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
string StuName = textBox3.Text.Trim();
cmdString = string.Format($"delete Table_2 where Name=" +
$"'{StuName}'");
if(!IsExist(StuName))
{
MessageBox.Show("对不起,该用户不存在!");
}
else
{
if(GetSqlCmd(cmdString))
{
MessageBox.Show("删除用户成功!");
textBox3.Text = "";
textBox3.Focus();//获取光标
}
}
}
}
}
- 运行结果