比较无奈的一个选择,上课老师要求必须使用Mysql,而我又比较喜欢用VS,所以,就又过了这样一个蹩脚的组合……
编程语言:C#;
使用软件:VS2008,Mysql 5.0,mysql-connector-net-5.0.9,MySQL.VisualStudio(要先装mysql-connector-net,后装MySQL.VisualStudio);
在这里实际上使用了一种偷懒的做法,使用Mysql提供的类库,可以在代码中直接使用Mysql.Data.*。
针对几个模块的不同方法:
一,登陆的验证:
没有见识过真正的用户名密码验证时怎么做的,我使用了比较笨的办法:
![](http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif)
![ExpandedBlockStart.gif](http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif)
2 conn.Open();
3 // 读取所输入的用户名和密码
4 MySqlCommand comm = conn.CreateCommand();
5 if (radioButton1.Checked)
6 {
7 string sql = " SELECT aname,pass FROM xuanke.admin WHERE(aname=' " + textBox1.Text + " ') " ;
8 comm.CommandText = sql;
9 try
10 {
11 MySqlDataReader datar = comm.ExecuteReader();
12
13
14 // 判断是否存在输入的用户
15
16 if ( ! datar.HasRows)
17 {
18 MessageBox.Show( " 用户名不存在,请重新输入! " );
19 textBox1.Focus();
20 return ;
21 }
22
23
24 // 读取数据库的内容,并与输入的进行比较;
25 while (datar.Read())
26 {
27 // 判断用户输入是否正确
28 if (datar[ " pass " ].ToString().Trim() != textBox2.Text.Trim())
29 {
30 MessageBox.Show( " 用户密码不正确,请重新输入! " );
31 textBox2.Focus();
32 return ;
33 }
34
35 else
36 {
37 AdminLogin f1 = new AdminLogin();
38 f1.info = textBox1.Text.Trim();
39 f1.ShowDialog();
40 Close();
41
42 }
43
44 }
45 }
46 catch (Exception ex)
47 {
48 MessageBox.Show( " 出现错误,错误原因为 " + ex.Message,
49 " 系统提示: " , MessageBoxButtons.OK, MessageBoxIcon.Error);
50 }
51
52
53 }
先把用户名和密码从表中选出来,然后,持有用户名,把键入的密码在查询结果查询,验证。
这里关于获取数据的方法,就是利用command吸收了sqlstr,然后使用command.ExecuteReader();返回的DataReader,利用这个里面提供的索引来获取。
我觉得这里返回给DataReader的应该也是一个表,可以根据不同的column来读取不同的值。
二,根据学号查询学生信息
![](http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif)
![ExpandedBlockStart.gif](http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif)
2 {
3 try
4 {
5 // Specify a connection string. Replace the given value with a
6 // valid connection string for a Northwind SQL Server sample
7 // database accessible to your system.
8 String connectionString =
9 " Database=xuanke;Data Source=localhost;User Id=root;Password=tjucfsc4 " ;
10
11 // Create a new data adapter based on the specified query.
12 MySqlDataAdapter dataAdapter = new MySqlDataAdapter(selectCommand, connectionString);
13
14 // Create a command builder to generate SQL update, insert, and
15 // delete commands based on selectCommand. These are used to
16 // update the database.
17 MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder(dataAdapter);
18
19 // Populate a new data table and bind it to the BindingSource.
20 DataTable table = new DataTable();
21 table.Locale = System.Globalization.CultureInfo.InvariantCulture;
22 dataAdapter.Fill(table);
23 bd.DataSource = table;
24
25
26
27 }
28 catch (Exception ex)
29 {
30 MessageBox.Show( " 出现错误,错误原因为 " + ex.Message,
31 " 系统提示: " , MessageBoxButtons.OK, MessageBoxIcon.Error);
32 }
33 }
34 private void button3_Click( object sender, EventArgs e)
35 {
36 dataGridView4.DataSource = bindingSource1;
37 if (radioButton2.Checked)
38 {
39 string str = " select * from xuanke.students where sname=\ "" + textBox1.Text.Trim() + " \ " ; " ;
40 GetData(str, bindingSource1);
41 }
42
43 else if (radioButton1.Checked)
44 {
45 string str = " select * from xuanke.students where sid=\ "" + textBox1.Text.Trim() + " \ " ; " ;
46 GetData(str, bindingSource1);
47 }
48 else { ;}
49 }
此处使用了Datagridview和BindingSource,使用比较简单:构造一条SQL语句,然后将Datagridview的数据源指向BindingSource,然后利用GetData方法,将SQL语句的执行结果赋给BindingSource就可以了。我觉得这个方法是见过的方法中最简单有效的。GetData可以COPY到任何你想要的地方而无需更改,只要把表格和源定义好,直接加进SQL语句就可以啦~
三,页面Load
这个方法需要VS能够收录Mysql的连接器,第一次成功了,后来就不行,窃以为扩展性不如上两个好。
这种方法实际上就是程序自主建立自己的DataSet,然后,一些数据访问直接在DataSet 上完成,而不用再去访问数据库。
先建立一个DataTableAdapter对象,然后就根据提示,可以输入SQL语句,一步步,最重结果是建立了一个DataTable。没什么代码,鼠标流。
![](http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif)
![ExpandedBlockStart.gif](http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif)
2 this .dataTable3TableAdapter.Fill( this .kunDataSet.DataTable3);
3 // TODO: 这行代码将数据加载到表“kunDataSet.DataTable1”中。您可以根据需要移动或移除它。
4 this .dataTable1TableAdapter.Fill( this .kunDataSet.DataTable1);
就是把DataSet中的数据表Fill到DataGridView中。
大概用到的也就这几种方法了,权作是个笔记了。