写在前面的话
哈喽,大家好!新人首发多多指教。最近在老师安排下做了一个小程序,是基于DEV工具开发,使用的是vs2008版本,数据库是使用的sql Server。在开发过程中,首先是对vs2008版本不太熟,因为之前一直用的是vs2012的,其次就是dev控件之前完全没有接触过,虽说跟原始控件差不多,但是使用起来,还是有些力不从心。所以在开发过程中遇到很多困难和麻烦,一有问题当然就是上百度、CSDN查啊查,但是各种功能零零碎碎的很麻烦,然后现在整个做下来,还算有点心得体验,就把整个的学习过程上传上来。希望有兴趣的小伙伴可以来看看,交流一下,也希望能够帮助到一些需要的小伙伴。
这个项目主要是练习DEV相关控件的使用,最主要是 gridControl 的使用,这个后面我会详细再来更,今天想先把登录的部分展示出来。话不多说,请看下文~
界面设计和功能代码
界面设计
如图片所示,我们做的这个小程序是商品管理有关的,就是对于商品信息的增删改查之类的这样的操作,下面先对登录页面的功能做一些简单的介绍吧:
- 页面布局:整个页面使用的全部是dev的控件,包括pictureEdit、panelControl、textEdit和labelControl;
- 登录按钮:点击登录按钮后,触发click事件,并在后台判断其用户名和密码是否正确;
- 取消按钮:点击取消按钮后,询问是否确认退出,确定后直接关闭并退出程序,结束运行;
- 重置按钮:清空文本框内的输入;
- 点击登录按钮后,
判断用户名是否存在
,如果不存在则清空当前文本框并将光标定位到用户名文本框,同时提示“用户名不存在”;- 如果用户名存在,则
判断密码输入是否正确
,如果错误,则清空密码文本框,并将光标定位到密码文本框,同时提示“密码错误”;
登录功能代码
/// <summary>
/// 登录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLogin_Click(object sender, EventArgs e)
{
//获取用户名长度
int uNameLength = this.txtUserName.Text.Trim().Length;
//获取密码长度
int uPwdLength = this.txtPassword.Text.Trim().Length;
//如果用户名密码不为空
if (uNameLength != 0 && uPwdLength != 0)
{
//验证登录信息
//定义变量
string sql;
string sql1;
//给sql赋值,查询用户表中是否有匹配用户名和密码
sql = "select * from Users where userCode='" + txtUserName.Text.ToString().Trim() + "'";
sql1 = "select * from Users where userCode='" + txtUserName.Text.ToString().Trim()
+ "'and password='" + txtPassword.Text.ToString().Trim() + "'";
//查询是否存在用户名
try
{
//验证是否存在用户名
DataSet result = new DataSet();
result = new dbHelper().ExecuteAdapter(sql);
//验证用户名及密码是否正确
DataSet result1 = new DataSet();
result1 = new dbHelper().ExecuteAdapter(sql1);
if (result != null)
{
//如果用户名存在
if (result.Tables[0].Rows.Count > 0)
{
if (result1 != null)
{
//如果用户名密码正确
if (result1.Tables[0].Rows.Count > 0)
{
//给全局变量user赋值
dbHelper.UserInfo.user = result1.Tables[0].Rows[0]["userName"].ToString().Trim();
//隐藏当前页面并跳转到首页
this.Hide();
frmGoodsManage manageForm = new frmGoodsManage();
manageForm.Show();
}
else
{
//如果查询结果为空
MessageBox.Show("密码错误,请重新输入!");
this.txtPassword.Text = "";
this.txtPassword.Focus();
}
}
}
else
{
//用户名不存在
MessageBox.Show("用户名不存在!");
this.txtUserName.Text = "";
this.txtPassword.Text = "";
this.txtUserName.Focus();
}
}
}
//捕获异常
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//如果用户名输入框为空
else if (uNameLength == 0 && uPwdLength != 0)
{
MessageBox.Show("用户名不能为空!");
txtUserName.Focus();
}
//如果密码输入框为空
else if (uNameLength != 0 && uPwdLength == 0)
{
MessageBox.Show("密码不能为空!");
txtPassword.Focus();
}
else
{
MessageBox.Show("您还没有输入!");
txtUserName.Focus();
}
}
取消功能代码
/// <summary>
/// 取消
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCancel_Click(object sender, EventArgs e)
{
try
{
//对消息框进行判断
if (MessageBox.Show("你确定要退出吗?", "退出", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
Application.Exit();
}
}
//捕获异常
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
重置功能代码
/// <summary>
/// 重置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnReset_Click(object sender, EventArgs e)
{
try
{
//如果文本框为空则提示
if (this.txtUserName.Text == "" && this.txtPassword.Text == "")
{
MessageBox.Show("您还没有输入内容!");
this.txtUserName.Focus();
}
//文本框有内容
else
{
//清空文本框内容
this.txtUserName.Text = "";
this.txtPassword.Text = "";
this.txtUserName.Focus();
}
}
//捕获异常
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
dbHelper的代码
因为如果有小伙伴执行的话会发现我上面有一个自己定义的dbHelper的类,其实也就相当于sqlHelper,主要是用来返回sql语句的执行结果
,在代码中判断使用更加方便。
class dbHelper
{
//定义全局静态变量connstring,用于连接数据库
public static string connstring = "data source=.;initial catalog=GoodsManage;user id=sa;password=123456";
/// <summary>
/// 执行多条sql语句
/// </summary>
/// <param name="SqlList"></param>
/// <returns></returns>
public bool ExcuteSqlTran(List<string> SqlList)
{
//连接数据库
SqlConnection conn = new SqlConnection();
conn.ConnectionString = connstring;
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = conn;
//打开数据库连接
conn.Open();
//事务
using (SqlTransaction tran = conn.BeginTransaction())
{
try
{
foreach (string sql in SqlList)
{
myCommand.Transaction = tran;
//执行语句
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
}
//提交
tran.Commit();
//如果正确执行,则返回值1
return true;
}
catch
{
//遇到错误,回滚
tran.Rollback();
//如果出现异常,返回值0
return false;
}
finally
{
//关闭连接
conn.Close();
}
}
}
/// <summary>
/// 执行单条sql语句,并返回整数型值
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public int ExecuteNonQuery(string sql) //自定义函数:执行sql语句
{
//连接数据库
SqlConnection conn = new SqlConnection();
conn.ConnectionString = connstring;
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = conn;
//打开数据库连接
conn.Open();
//事务
using (SqlTransaction tran = conn.BeginTransaction())
{
try
{
myCommand.Transaction = tran;
//执行语句
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
//提交
tran.Commit();
//如果正确执行,则返回值1
return 1;
}
catch
{
//遇到错误,回滚
tran.Rollback();
//如果出现异常,返回值0
return 0;
}
finally
{
//关闭连接
conn.Close();
}
}
}
/// <summary>
/// 自定义函数,用于查询数据库操作
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public DataSet ExecuteAdapter(string sql) //自定义函数,用于查询数据库操作
{
//同上,连接数据库
SqlConnection conn = new SqlConnection();
conn.ConnectionString = connstring;
SqlCommand myCommand = new SqlCommand(sql, conn);
try
{
//执行查询操作,将值以数据集形式返回
SqlDataAdapter da = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
//提交
da.Fill(ds);
return ds;
}
catch
{
//如果异常,返回null
return null;
}
finally
{
//关闭连接
conn.Close();
}
}
/// <summary>
/// 定义用户信息全局变量
/// </summary>
public class UserInfo
{
public static string user;
}
}