1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Winform连接数据库并实现用户登录

Winform连接数据库并实现用户登录

时间:2021-10-24 16:29:55

相关推荐

Winform连接数据库并实现用户登录

数据库|mysql教程

Winform,连接,数据库,实现,用户,登录,首先,介绍,

数据库-mysql教程

php影楼预约 源码,ubuntu的删除命令,tomcat下载好怎么用,selenium爬虫被禁,php调用函数的方法,seo工程师seo招聘lzw

首先介绍一下SqlConnection类 SqlConnection类表示一个到SQL Server数据库的打开的连接,此类不能被继承 SqlConnection类的构造函数有如下3个 SqlConnection:初始化 SqlConnection 类的新实例。 SqlConnection(String):如果给定包含连接字符串的字符串,则

网络课堂网站源码,vscode向左缩进,宏碁双显卡笔记本ubuntu,tomcat 不能跳转,sqlite工具导出功能,微信余额自定义插件,前端框架的技术选型外包,长得像蚯蚓的爬虫,php转mp3,随州seo培训,林口分类信息网站 v3.0,手机上如何刷新网页代码,分销王模板安装lzw

原生app生成源码,ubuntu终端连接主机,Api接口防爬虫,php探针,seo表格设置lzw

首先介绍一下SqlConnection类

SqlConnection类表示一个到SQL Server数据库的打开的连接,此类不能被继承

SqlConnection类的构造函数有如下3个

SqlConnection:初始化 SqlConnection 类的新实例。

SqlConnection(String):如果给定包含连接字符串的字符串,则初始化 SqlConnection 类的新实例。

SqlConnection(String, SqlCredential):初始化给定连接字符串的 SqlConnection类的新实例,而不使用包含用户识别号和密码的 Integrated Security = true 和 SqlCredential 对象。

常用的方法

Open 使用 ConnectionString 所指定的属性设置打开数据库连接

Close 关闭与数据库的连接,此方法是关闭任何已打开连接的首选方法

CreateCommand 创建并返回一个与SqlConnection关联的SqlCommand对象

Dispose 释放由Component使用的所有资源

SqlConnection对象若使用了带一个string类型参数的构造函数,这个参数叫做连接字符串

以下是连接字符串的参数名和描述

Data Source 指明服务器,可以是本地机器,机器域名或者IP地址

Initial Catalog 数据库名字

Integrated Security 设置为SSPI,使连接使用用户的Windows登录

User ID 配置在SQL Server中的用户名

Password 与SQL Server的用户名匹配的密码

下面写一个类,用于创建SqlConnection对象,便于以后使用

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data.SqlClient;namespace Cam{ class db { public static SqlConnection Camcon() { return new SqlConnection("Data Source=(local);Initial Catalog=User;Integrated Security=True"); } }}

再说下与数据库执行相关的SqlCommand 类

SqlCommand 类表示要对SQL Server数据库执行的一个Transact-SQL语句或存储过程,此类不能被继承

SqlCommand 类的构造函数有如下4个

SqlCommand 初始化 SqlCommand 类的新实例。

SqlCommand(String) 用查询文本初始化 SqlCommand 类的新实例。

SqlCommand(String, SqlConnection) 初始化具有查询文本和SqlConnection 的 SqlCommand 类的新实例。

SqlCommand(String, SqlConnection,SqlTransaction) 使用查询文本、 SqlConnection 以及 SqlTransaction 初始化 SqlCommand 类的新实例

常用的方法

Dispose 释放由 Component 使用的所有资源

EndExecuteNonQuery 完成Transact-SQL语句的异步执行

EndExecuteReader 完成Transact-SQL语句的异步执行,返回请求的SqlDataReader

ExecuteNonQuery 对连接执行Transact-SQL语句并返回受影响的行数

ExecuteScalar 执行查询,并返回查询所返回的结果集中第一行的第一列

下面建一个WinForm,实现用户的登录,用户名和密码存在User数据库中

using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace Cam{ public partial class Login : Form { public Login() { InitializeComponent(); } private void Login_Load(object sender, EventArgs e) //光标停在第一行 { textBox1.TabIndex = 0; //直接textBox1.Focus();不管用 textBox1.Focus(); } private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == "") {MessageBox.Show("请输入用户名", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else {if (textBox2.Text == ""){ MessageBox.Show("请输入密码", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);}else{ SqlConnection conn = db.Camcon(); conn.Open(); SqlCommand cmd = new SqlCommand("select count(*) from login where name=\" + textBox1.Text + "and password=\" + textBox2.Text + "\", conn); int num = Convert.ToInt32(cmd.ExecuteScalar()); if (num > 0) { conn.Close(); MessageBox.Show("登录成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("用户名或密码错误", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information); }} } } private void button2_Click(object sender, EventArgs e) { if (MessageBox.Show("确定退出登录吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK) {Application.Exit(); } } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) //回车跳到第二行 { if (e.KeyChar == 13) {textBox2.Focus(); } } private void textBox2_KeyPress(object sender, KeyPressEventArgs e) //回车登录 { if (e.KeyChar == 13) {button1_Click(sender, e); } } }}

登录界面如下

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。