Winform连接数据库并实现用户登录
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Winform连接数据库并实现用户登录,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4146字,纯文字阅读大概需要6分钟。
内容图文
![Winform连接数据库并实现用户登录](/upload/InfoBanner/zyjiaocheng/529/96a00ca635f84ee7b95394a6d54c61df.jpg)
首先介绍一下SqlConnection类 SqlConnection类表示一个到SQL Server数据库的打开的连接,此类不能被继承 SqlConnection类的构造函数有如下3个 SqlConnection:初始化 SqlConnection 类的新实例。 SqlConnection(String):如果给定包含连接字符串的字符串,则
首先介绍一下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 System.ComponentModel; 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); } } } }
登录界面如下
内容总结
以上是互联网集市为您收集整理的Winform连接数据库并实现用户登录全部内容,希望文章能够帮你解决Winform连接数据库并实现用户登录所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。