mysql.net连接器_C# 连接 MySQL

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingMySql.Data.MySqlClient;namespaceMySQLTest

{public partial classForm1 : Form

{

Form2 formMain= newForm2();

MySqlConnection m_Conn= newMySqlConnection();publicForm1()

{

InitializeComponent();

}private void Form1_Load(objectsender, EventArgs e)

{

m_Conn.ConnectionString= "Server=localhost;Port=3306;Database=test; User=root;Password=root;";

}private void button1_Click(objectsender, EventArgs e)

{try{string sql = "";using (MySqlCommand cmd = newMySqlCommand())

{if (m_Conn.State !=ConnectionState.Open)

{

m_Conn.Open();

}

sql= "select name,pass from userinfo where name=@NAME and pass=@PASSWD";

cmd.Parameters.AddWithValue("@NAME", textBox1.Text.Trim());

cmd.Parameters.AddWithValue("@PASSWD", textBox2.Text.Trim());

cmd.Connection=m_Conn;

cmd.CommandText=sql;

MySqlDataReader reader=cmd.ExecuteReader();if(reader.Read())

{if (reader["name"].ToString().CompareTo(textBox1.Text.Trim()) == 0 && reader["pass"].ToString().CompareTo(textBox2.Text.Trim()) == 0)

{this.Hide();

formMain.ShowDialog();this.Show();

}

}else{

MessageBox.Show("帐号或密码错误,请检查!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}

reader.Close();

}

}catch(Exception)

{throw;

}

}private void button2_Click(objectsender, EventArgs e)

{this.Close();

}private void Form1_FormClosed(objectsender, FormClosedEventArgs e)

{try{if (m_Conn.State !=ConnectionState.Closed)

{

m_Conn.Close();

}

}catch(Exception)

{throw;

}

}private void textBox2_KeyPress(objectsender, KeyPressEventArgs e)

{if (e.KeyChar == 13)

{

button1_Click(sender, e);

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值