您的位置:首页 > 数据库 > MySQL

C# 连接Mysql数据库及从数据库中读取某一个值(注册与登录)

2018-03-12 19:01 671 查看
      由于课题需要在C#上搭建一个平台用来显示心电,所以我就在师兄的基础上修改了C#心电显示程序,但是作为一个不想科研的小姑娘,我就决定用一天的时间来给这个显示程序加个登录注册的功能。这个纯属自己瞎折腾,和课题没关系。
     先来说一下注册的功能实现,注册功能就是在面板上拖动几个label键和几个textBox键,label中写上你要的信息,关键是textBox的name名你要知道,因为后面要通过name名来获得值,保存到数据库中,这里选择的数据库是Mysql关系型数据库。另外拖动两个button出来,一个是注册建,一个是退出键(名字随便你),关键是给注册键绑定监听事件,代码如下(注册,上传到数据库)
      private void btn_Register_Click(object sender, EventArgs e)
{
//备注:server =IP; user id = 用户名; password = 密码; database = 库名"; //根据自己的设置
string connectString = "server=IP地址;user=用户名;password=密码;database=数据库名;Charset=utf8"; //根据自己的设置

// MySqlConnection connection = new MySqlConnection(connectString);//初始化到数据库的连接
using (MySqlConnection connection = new MySqlConnection(connectString))
{
try
{
//注册信息的每一个空都不能为空
if (txt_UserName.Text == "" || txt_Password.Text == "" || txt_gestation.Text == "" || txt_UserRegName.Text == "" || txt_birthday.Text == "")
MessageBox.Show(this, "请填写所有选项", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
else
{
connection.Open();//打开此数据库连接

// SqlCommand sqlText = new SqlCommand( connectString,connection);//建立数据库命令对象 (这里数据库)
//像mysql数据库里面添加数据。
string sql = "insert into yyl values('" + txt_UserName.Text.Trim() + "','" + txt_UserRegName.Text + "','" + txt_Password.Text + "','" + txt_birthday.Text + "','" + txt_gestation.Text + "'+'" + "')";

MySqlCommand sqlText = new MySqlCommand(sql, connection);//建立数据库命令对象
//添加数据库命令参数

if (sqlText.ExecuteNonQuery() == 0)
MessageBox.Show(this, "注册失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);

else
MessageBox.Show(this, "恭喜您,注册成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);

}
}
catch (MySql.Data.MySqlClient.MySqlException)
{ //我把用户名当做键值,不允许重名(用户名)
MessageBox.Sh
4000
ow( this, "用户名已存在", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
catch (SqlException exception)
{
MessageBox.Show(this, "注册失败:\n" + exception.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
finally
{
connection.Close();
}
}
}     注册完了我们就来写个登录系统,无非就是判断用户有没有输正确用户名与密码,非空输入是不允许的。为了在检测主界面中可以有用户的信息,我们这里还需要从数据库里面读值并且传给检测界面中。代码如下:
    private void button1_Click_1(object sender, EventArgs e) //登录键
{
try
{ //判断用户是否输入,如果有空,则报错
if (textBox1.Text == "")
{
MessageBox.Show("用户名不能为空");
}
else
{
if (textBox2.Text == "")
{
MessageBox.Show("密码不能为空!");
}
else
{
string admin_id = textBox1.Text;//获取账号
string admin_psw = textBox2.Text;//获取密码
string connectString = "server=IP地址;user=用户名;password=密码;database=数据库名;Charset=utf8"; //根据自己的设置
MySqlConnection connection = new MySqlConnection(connectString);//创建连接
connection.Open();//打开连接
//去数据库里面验证输入的用户名和密码是不是存在
string sql = string.Format("select count(*) from yyl where useName='{0}' and password='{1}'", admin_id, admin_psw);//查询是否有该条记录,根据账户密码
MySqlCommand command = new MySqlCommand(sql, connection);//sqlcommand表示要向数据库执行sql语句或存储过程
int i = Convert.ToInt32(command.ExecuteScalar());//执行后返回记录行数
if (i > 0)//如果大于1,说明记录存在,登录成功

{
MessageBox.Show("登录成功!");

}

else
{
MessageBox.Show("用户名或者密码错误!");
}
connection.Close(); //关闭数据库连接
//通过键值,去寻找你要的那列数据,
string gestation1 = string.Format("select X from yyl where useName='{0}'", admin_id);//X要寻找的列名
MySqlConnection conn1 = new MySqlConnection(connectString);

MySqlCommand command1 = new MySqlCommand(gestation1, conn1);
string gestation="";
using (conn1)

{

conn1.Open();

MySqlDataReader reader = command1.ExecuteReader();

while (reader.Read())

{

gestation = reader[0].ToString();

}

}

new Form1(textBox1.Text, gestation).Show();//将这两个值传给主界面
this.Hide();

}
}
}

catch (Exception ex)
{
MessageBox.Show("异常错误" + ex);
}
}
     仅供大家参考学习
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: