登陸窗口:Form_login.cs
using MedicalRecord.Class;
using System;
using System.Windows.Forms;
namespace MedicalRecord
{
public partial class Form_login : DevComponents.DotNetBar.OfficeForm
{
//數據庫鏈接類 和用戶類
private SQLhelper sqLhelper = new SQLhelper();
private static LoginUser LoginUser = new LoginUser();
private string code = String.Empty;
public Form_login()
{
InitializeComponent();
}
//登陸按鈕
private void Login_Click(object sender, EventArgs e)
{
// 判斷輸入的是否爲空 ,如果爲空需要提醒
//判斷是否輸入了賬號
if (string.IsNullOrEmpty(this.Skin_usercode.Text))
{
MessageBox.Show("用戶名必須輸入!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
Skin_usercode.Focus();
return;
}
//判斷是否輸入密碼
if (string.IsNullOrEmpty(Skin_password.Text))
{
MessageBox.Show("密碼必須輸入!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
Skin_password.Focus();
return;
}
//如果沒有的話把輸入的賦值給loginuser 類
LoginUser.Usercode = Skin_usercode.Text;
LoginUser.Password = Skin_password.Text;
//去訪問數據庫,查詢是否存在該用戶
string selectSql =
string.Format("select count(*) from mris.dbo.sys_user where usercode = '{0}' and password = '{1}' ",
LoginUser.Usercode, LoginUser.Password);
//接受數據庫返回信息
//ExecuteScalar 執行一條返回第一條記錄第一列的SqlCommand命令
int ss = (int) sqLhelper.ExecuteScalar(selectSql);
//如果存在就將 DialogResult.OK,否則提示不存在
if (ss == 1)
{
this.DialogResult = DialogResult.OK;
}
else
{
MessageBox.Show("用戶密碼不存在!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
Skin_password.Clear();
Skin_usercode.Clear();
Skin_usercode.Focus();
return;
}
}
//窗口打開的時候就生成一個驗證碼
private void Form_login_Load(object sender, EventArgs e)
{
change();
}
private void cde_Click(object sender, EventArgs e)
{
change();
}
private void cde_DoubleClick(object sender, EventArgs e)
{
change();
}
public void change()
{
UpdateCode updateCode = new UpdateCode();
code = updateCode.MakeRandomCode(4);
this.cde.Image = updateCode.MakeImage(code, this.cde.Width, this.cde.Height);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MedicalRecord.Class
{
public class LoginUser
{
private string _usercode;
public string Usercode
{
get { return _usercode; }
set { _usercode = value; }
}
private string _password;
public string Password
{
get { return _password; }
set { _password = value; }
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace MedicalRecord.Class
{
public class UpdateCode
{
public string MakeRandomCode(int codelenght)
{
return CreateRandomCode(codelenght);
}
public Bitmap MakeImage(string codestring ,int width ,int height)
{
return CreateImage(codestring, width, height);
}
private string CreateRandomCode(int codelenght)
{
int rand;
char code;
String RandomCode = String.Empty;
Random random = new Random();
for (int i = 0; i < codelenght; i++)
{
rand = random.Next();
if (rand%3 == 0)
{
code = (char) ('A' + (char) (rand%26));
}
if (rand%3 == 1)
{
code = (char) ('a' + (char) (rand%26));
}
else
{
code = (char) ('0' + (char) (rand%10));
}
RandomCode += code;
}
return RandomCode;
}
private Bitmap CreateImage(string RandCode ,int width ,int height )
{
Bitmap map = new Bitmap(width, height);
Graphics graph = Graphics.FromImage(map);
graph.Clear(Color.AliceBlue);
graph.DrawRectangle(new Pen(Color.Black, 0), 0, 0, width - 1, height - 1); //畫一個邊框
graph.SmoothingMode = SmoothingMode.AntiAlias; //
Random rand = new Random();
//背景噪點生成
Pen blackpen = new Pen(Color.Gray, 0);
for (int i = 0; i < 50; i++)
{
int y = rand.Next(0, height);
int x = rand.Next(0, width);
graph.DrawRectangle(blackpen, x, y, 1, 1);
}
//文字距中
char[] chars = RandCode.ToCharArray();
StringFormat format = new StringFormat(StringFormatFlags.NoClip);
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
//定義字體
Color[] c =
{
Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan,
Color.Purple
};
string[] font = {"Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋體"};
for (int i = 0; i < chars.Length; i++)
{
int cindex = rand.Next(7);
int findex = rand.Next(5);
Font f = new Font(font[findex], 13, FontStyle.Bold); //字體樣式(參數2爲字體大小)
Brush b = new SolidBrush(c[cindex]);
Point dot = new Point(16, 16);
float angle = rand.Next(-45, 45); //轉動的度數
graph.TranslateTransform(dot.X, dot.Y); //移動光標到指定位置
graph.RotateTransform(angle);
graph.DrawString(chars[i].ToString(), f, b, 1, 1, format);
graph.RotateTransform(-angle); //轉回去
graph.TranslateTransform(2, -dot.Y); //移動光標到指定位置
}
return map ;
}
}
}
using System.Data;
using System.Data.SqlClient;
namespace MedicalRecord.Class
{
internal class SQLhelper
{
private static readonly string DateSource = "(Local)";
private static readonly string Initial = "";//寫庫名
private static readonly string User = "sa";
private static readonly string Password = " ";//寫密碼
private static readonly string connectionString =
string.Format(
"Data Source={0};Pooling=False;Max Pool Size = 1024;Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}",
DateSource, Initial, User, Password);
public static SqlConnection conn;
//打開數據庫連接
public static void OpenConn()
{
conn = new SqlConnection(connectionString);
if (conn.State.ToString().ToLower() != "open")
{
conn.Open();
}
}
//關閉數據庫連接
public static void CloneConn()
{
if (conn.State.ToString().ToLower() != "open")
{
conn.Close();
conn.Dispose();
}
}
// 讀取數據
public static SqlDataReader GetDataReaderValue(string sql)
{
OpenConn();
SqlCommand command = new SqlCommand(sql, conn);
SqlDataReader dataReader = command.ExecuteReader();
CloneConn();
return dataReader;
}
// 返回DataSet
public DataSet GetDataSetValue(string sql, string tablename)
{
OpenConn();
DataSet dataSet = new DataSet();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, conn);
sqlDataAdapter.Fill(dataSet, tablename);
CloneConn();
return dataSet;
}
// 返回DataView
public DataView GetDataViewVaule(string sql)
{
OpenConn();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, conn);
DataSet dataSet = new DataSet();
sqlDataAdapter.Fill(dataSet, "temp");
CloneConn();
return dataSet.Tables[0].DefaultView;
}
// 返回DataView
public DataTable GetDataTableValue(string sql)
{
OpenConn();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
DataTable dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
CloneConn();
return dataTable;
}
// 執行一個SQL操作:添加、刪除、更新操作
public void ExecuteNonQuery(string sql)
{
OpenConn();
SqlCommand command = new SqlCommand(sql, conn);
command.ExecuteNonQuery();
command.Dispose();
CloneConn();
}
// 執行一個SQL操作:添加、刪除、更新操作,返回受影響的行
public int ExecuteNonQueryCount(string sql)
{
OpenConn();
SqlCommand 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();
return value;
}
// 返回記錄數
public int SqlServerRecordCount(string sql)
{
OpenConn();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = cmd.ExecuteReader();
int RecordCount = 0;
while (dr.Read())
{
RecordCount = RecordCount + 1;
}
CloneConn();
return RecordCount;
}
}
}
Program 裏: internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main()
{
bool ret;
Mutex mutex = new Mutex(true, Application.ProductName, out ret);
if (ret)
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form_login fl = new Form_login();
if (fl.ShowDialog() == DialogResult.OK)
{
Application.Run(new Main());
}
mutex.ReleaseMutex();
}
catch (Exception ex)
{
MessageBox.Show(null, ex.ToString(), Application.ProductName,
MessageBoxButtons.OK, MessageBoxIcon.Warning);
Application.Exit();
}
}
Application.EnableVisualStyles();
}
}