10.26学习总结

发布时间 2023-10-26 08:52:12作者: 搜一码赛

学习了c#进销存系统开发的知识,完成了对进销存系统登录与加载功能的实现.

加载界面:

实现代码:

private void timer1_Tick(object sender, EventArgs e)
{
startpos+=5;//变量自增
Myprogress.Value = startpos;
PercentageLbl.Text = startpos+"%";
if (Myprogress.Value==100)//进度条一百自动清零
{
Myprogress.Value = 0;
timer1.Stop();
login log=new login();
log.Show();
this.Hide();//隐藏本页面
}
}

private void jiazai_Load(object sender, EventArgs e)
{
timer1.Start();
}

登录功能:

实现代码:

private void button1_Click(object sender, EventArgs e)

{

//获取界面上用户输入信息
string username = textBox1.Text.Trim();
string password = textBox2.Text.Trim();

//2.运用Connection对象建立与数据库的连接
//(1)定义连接数据库的字符串
string connStr = "server=127.0.0.1;database=数据库名;uid=用户名;pwd=你的密码";//采用混合身份验证
//(2)创建Connection对象
SqlConnection conn = new SqlConnection(connStr);
//(3)打开连接
conn.Open();
//3.利用Command对象执行sql语句
//(1)定义要执行的sql语句
string sql = "select count(1) from users where username=@t1 and password=@t2 ";
//(2)通过Connection对象和sql语句,创建Command对象
SqlCommand cmd = new SqlCommand(sql, conn);
//(3)处理Command对象的参数
cmd.Parameters.Add("@t1", SqlDbType.VarChar).Value = username;
cmd.Parameters.Add("@t2", SqlDbType.VarChar).Value = password;

//(4)执行SQL语句
if ((int)cmd.ExecuteScalar() > 0)
{

Main Main = new Main();
username = textBox1.Text;
password = textBox2.Text;
MessageBox.Show("欢迎进入!", "登陆成功!", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Hide(); //登录成功后隐藏界面
Main.Show();//登录成功显示的下一个页面

}
else
{
//没查到数据的处理逻辑代码
MessageBox.Show("用户名或者密码错误", "!提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

}
//4.关闭数据库连接
conn.Close();
}