使⽤VisualStudio写⼀个简单的Windows窗体应⽤登录界⾯需要的知识:C#的基本语法,以及Visual Studio的基本操作⽅法
编辑软件:Sql Server 2017,Visual Studio 2017
前提:Sql Server 中有⼀个名为"MY-LAPTOP"的服务器,⼀个名为"Test"的数据库,库下有⼀个名为"Login_Table"的表
⼀、编写Form1,登录主界⾯
⾸先编写Form1.Designer.cs
namespace WindowsFormsApp3
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使⽤的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器⽣成的代码
/// <summary>
/// 设计器⽀持所需的⽅法 - 不要修改
/// 使⽤代码编辑器修改此⽅法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.linkLabel1 = new System.Windows.Forms.LinkLabel();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
/
/
// label1
//
this.label1.AutoSize = true;
this.label1.Enabled = false;
this.label1.Font = new System.Drawing.Font("宋体", 15F);
this.label1.ForeColor = System.Drawing.SystemColors.ControlText;
this.label1.Location = new System.Drawing.Point(49, 41);
this.label1.Text = "账号:";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
/
/ label2
//
this.label2.AutoSize = true;
this.label2.Enabled = false;
this.label2.Font = new System.Drawing.Font("宋体", 15F);
this.label2.ForeColor = System.Drawing.SystemColors.ControlText;            this.label2.Location = new System.Drawing.Point(49, 98);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(87, 25);
this.label2.TabIndex = 1;
this.label2.Text = "密码:";
this.label2.Click += new System.EventHandler(this.label2_Click);
//
// button1
//
this.button1.Font = new System.Drawing.Font("宋体", 13F);
this.button1.Location = new System.Drawing.Point(54, 168);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(82, 33);
this.button1.TabIndex = 2;
this.button1.Text = "登录";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);            //
/
/ button2
//
this.button2.Font = new System.Drawing.Font("宋体", 13F);
this.button2.Location = new System.Drawing.Point(190, 168);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(90, 33);
this.button2.TabIndex = 3;
this.button2.Text = "注册";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);            //
// button3
/
/
this.button3.Font = new System.Drawing.Font("宋体", 13F);
this.button3.Location = new System.Drawing.Point(329, 168);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(79, 33);
this.button3.TabIndex = 4;
this.button3.Text = "取消";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);            //
// textBox1
//
//
// textBox2
//
/
/
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(54, 217);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(0, 15);
this.label3.TabIndex = 7;
//
// linkLabel1
登录按钮图片素材
//
this.linkLabel1.AutoSize = true;
this.linkLabel1.Location = new System.Drawing.Point(321, 134);
this.linkLabel1.Name = "linkLabel1";
this.linkLabel1.Size = new System.Drawing.Size(83, 15);
this.linkLabel1.TabIndex = 8;
this.linkLabel1.TabStop = true;
this.linkLabel1.Text = "忘记密码>>";
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(415, 110);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(89, 19);
this.checkBox1.TabIndex = 9;
this.checkBox1.Text = "显⽰密码";
this.checkBox1.UseVisualStyleBackColor = true;
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);            //
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(508, 244);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.linkLabel1);
this.Controls.Add(this.label3);
this.Controls.Box2);
this.Controls.Box1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
}
以下代码中编写了SqlHelp类,其作⽤是封装连接数据库语句,这样就可以不⽤每次都编写连接数据库语句,如此便可快速完成编写
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace WindowsFormsApp3
{
class SqlHelp
{
/// <summary>
/// DataSource : MY-LAPTOP
/// Initial Catalog: Test
/// User ID:
/// Password:
/// </summary>
private const string connectionString = @"Data Source=MY-LAPTOP;Initial Catalog=Test;Integrated Security = SSPI";
public SqlHelp() { }
public static SqlConnection conn;
//打开数据库连接
public static void OpenConn()
{
string SqlCon = connectionString;//数据库连接字符串
conn = new SqlConnection(SqlCon);
if (conn.State==ConnectionState.Closed)
{
conn.Open();
}
}
//关闭数据库连接
public static void CloseConn() {
if(conn.State.ToString().ToLower() == "open")
{
//读取数据
public static SqlDataReader GetDataReaderValue(string sql) {
OpenConn();
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataReader dr = cmd.ExecuteReader();
CloseConn();
return dr;
}
/
/返回DataSet
public static DataSet GetDataSetValue(string sql, string tableName) {            OpenConn();
SqlDataAdapter da = new SqlDataAdapter(sql,conn);
DataSet ds = new DataSet();
da.Fill(ds, tableName);
CloseConn();
return ds;
}
//返回DataView
public static DataView GetDataViewValue(string sql) {
OpenConn();
SqlDataAdapter da;
DataSet ds = new DataSet();
da = new SqlDataAdapter(sql, conn);
da.Fill(ds, "temp");
CloseConn();
return ds.Tables[0].DefaultView;
}
//返回DataTable
public static DataTable GetDataTableValue(string sql) {
OpenConn();
SqlDataAdapter da = new SqlDataAdapter(sql,conn);
DataTable dt = new DataTable();
da.Fill(dt);
CloseConn();
return dt;
}
//执⾏⼀个Sql操作:添加,删除,更新操作
public void ExecuteNonQuery(string sql)
{
OpenConn();
SqlCommand cmd;
cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
cmd.Dispose();
CloseConn();
}
//执⾏⼀个Sql操作:添加,删除,更新操作,返回受影响⾏数
public int ExecuteNonQueryCount(string sql)
{
OpenConn();
SqlCommand cmd;
cmd = new SqlCommand(sql, conn);
int value = cmd.ExecuteNonQuery();
return value;
}
//执⾏⼀条返回第⼀条记录第⼀列的SqlCommand命令
public object ExecuteScalar(string sql)
{
OpenConn();
SqlCommand cmd = new SqlCommand(sql, conn);
object value = cmd.ExecuteScalar();

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。