这里我们使用图形化操作的SQL Manager 2005 Lite for MySQL来建立数据,它的操作界面非常类似OFFICE软件,使用方便、很容量上手、下面开始建立数据库及表
单击"Creat New DataBase":新建DB
输入密码:
选择客户端编码为gb2312,防乱码;也可以在新建好的数据库右键选择"DataBase Registration Info"更改编码:
新建表:输入表名:
新建字段
点击加号,手动添加数据,点出对号,提交数据:
选择DDL选项,直接看到刚才操作的脚本;也可以不用上面的方式操作,直接写sql脚本来创建数据:
二、ADO.NET数据操作
推荐使用MySQL Connector Net 5.0.3
web.config添加连接字符串:
<add name="MySqlServer" connectionString="Data Source=127.0.0.1;User ID=root;Password=123;DataBase=BOOK;Charset=gb2312"/>
为了方便,把对数据的访问封装到一个类中:
1// 执行sql
2public int ExecuteSql(string strSql, MySqlParameter [] myPar)
3 {
4 try
5 {
6 myConnection.Open();
7 MySqlCommand cmd = new MySqlCommand(strSql, myConnection);
8 if (myPar != null)
9 {
10 foreach (MySqlParameter spar in myPar)
11 {
12 cmd.Parameters.Add(spar);
13 }
14 }
15 int result = cmd.ExecuteNonQuery();
16 myConnection.Close();
17 return result;
18 }
19 catch
20 {
21 return 0;
22 }
23 }
获取数据:2public int ExecuteSql(string strSql, MySqlParameter [] myPar)
3 {
4 try
5 {
6 myConnection.Open();
7 MySqlCommand cmd = new MySqlCommand(strSql, myConnection);
8 if (myPar != null)
9 {
10 foreach (MySqlParameter spar in myPar)
11 {
12 cmd.Parameters.Add(spar);
13 }
14 }
15 int result = cmd.ExecuteNonQuery();
16 myConnection.Close();
17 return result;
18 }
19 catch
20 {
21 return 0;
22 }
23 }
1 public DataSet GetDataSet(string strSql)
2 {
3 try
4 {
5 MySqlDataAdapter da = new MySqlDataAdapter(strSql, myConnection);
6 DataSet ds = new DataSet();
7 da.Fill(ds);
8 return ds;
9 }
10 catch
11 {
12 return null;
13 }
14 }
2 {
3 try
4 {
5 MySqlDataAdapter da = new MySqlDataAdapter(strSql, myConnection);
6 DataSet ds = new DataSet();
7 da.Fill(ds);
8 return ds;
9 }
10 catch
11 {
12 return null;
13 }
14 }
在页面中我们用一个gridview实现数据的读取、写入、编辑、删除等操作:
数据绑定:
1 //databind
2 protected void BindGrid()
3 {
4 DataSet ds = obj.GetDataSet("SELECT * FROM book ORDER BY bid");
5 GridView1.DataSource = ds;
6 GridView1.DataBind();
7 ds.Dispose();
8 }
添加数据:2 protected void BindGrid()
3 {
4 DataSet ds = obj.GetDataSet("SELECT * FROM book ORDER BY bid");
5 GridView1.DataSource = ds;
6 GridView1.DataBind();
7 ds.Dispose();
8 }
1 string strSQL = "INSERT INTO book (bname,author,publish) VALUES (?bname,?author,?publish)";
2 MySqlParameter[] mysp =
3 {
4 new MySqlParameter ("?bname",MySqlDbType.VarChar ),
5 new MySqlParameter ("?author",MySqlDbType.VarChar),
6 new MySqlParameter ("?publish",MySqlDbType.VarChar)
7 };
8 mysp[0].Value = txtName.Text.Trim();
9 mysp[1].Value = txtAuthor.Text.Trim();
10 mysp[2].Value = txtPublish.Text.Trim();
11 if (obj.ExecuteSql(strSQL, mysp) == 1)
12 {
13 Response.Write("<script>alert('提交成功');</script>");
14 BindGrid();
15 txtName.Text = txtAuthor.Text = txtPublish.Text = "";
16 }
注意参数符号是"?"而不是"@",这一点不同于sql server2 MySqlParameter[] mysp =
3 {
4 new MySqlParameter ("?bname",MySqlDbType.VarChar ),
5 new MySqlParameter ("?author",MySqlDbType.VarChar),
6 new MySqlParameter ("?publish",MySqlDbType.VarChar)
7 };
8 mysp[0].Value = txtName.Text.Trim();
9 mysp[1].Value = txtAuthor.Text.Trim();
10 mysp[2].Value = txtPublish.Text.Trim();
11 if (obj.ExecuteSql(strSQL, mysp) == 1)
12 {
13 Response.Write("<script>alert('提交成功');</script>");
14 BindGrid();
15 txtName.Text = txtAuthor.Text = txtPublish.Text = "";
16 }
其它编辑等操作代码等请下载文件
下载详细完整的代码文件/Files/chy710/MySQL_ADONET.rar
下篇预告:mysql开发中的乱码问题