例题7-1
1、连接路径:Connection——>Command——>DataReader
2、源代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using MySql.Data.MySqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//第一步:得到Connection:MySqlConnection
MySqlConnection conn = new MySqlConnection("Database=bookstore;Data Source=localhost;User Id=root;Password=root");
conn.Open();
string sqlcommandstring = "select * from account";
//第二步:Command : MySqlCommand
MySqlCommand mycommand = new MySqlCommand(sqlcommandstring,conn);
//第三步:DataReader: MySqlDataReader
MySqlDataReader myreader = mycommand.ExecuteReader();
while (myreader.Read())
{
showlabel.Text += myreader["accountid"] + " " + myreader["balance"];
}
}
}
3、http://dev.mysql.com/downloads/connector/net/
4、在网上上右击 ->添加->引用
在搜索栏中搜索Mysql.Data 添加上。
using MySql.Data.MySqlClient;只要这个不报错就行。
例题7-2
protected void Page_Load(object sender, EventArgs e)
{
//第一步:得到Connection:MySqlConnection
string stringconn = ConfigurationSettings.AppSettings["dsn"];
MySqlConnection conn = new MySqlConnection(stringconn);
conn.Open();
//创建DataSet数据集对象
DataSet MyDataSet = new DataSet();
string sqlcommandstring = "select * from mybooks";
MySqlDataAdapter mydataadapter = new MySqlDataAdapter(sqlcommandstring, conn);
mydataadapter.Fill(MyDataSet, "Book_Table");
//实现数据绑定与显示
MyGirdView.DataSource = MyDataSet.Tables["Book_Table"];
MyGirdView.DataBind();
}
数据库的连接:
1、在类中设置连接
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
/// <summary>
/// Conn 的摘要说明
/// </summary>
public class Conn
{
public static MySqlConnection createCon()
{
return new MySqlConnection("Database=aspnet;Data Source=localhost;User Id=root;Password=root");
}
}
2、在配置文件中设置连接
<?xml version="1.0"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<appSettings>
<add key="dsn" value="Database=bookstore;Data Source=localhost;User Id=root;Password=root"/>
</appSettings>
<connectionStrings>
<add name="ConnectionString1" connectionString="Data Source=oracle;User ID=scott;Password=tiger;Unicode=True" providerName="System.Data.OracleClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5">
<assemblies>
<add assembly="MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>
</assemblies>
</compilation>
<httpRuntime targetFramework="4.5"/>
</system.web>
</configuration>
例题7-3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//获取mysql数据库连接并打开数据库连接
MySqlConnection mysqlconn = Conn.createCon();
mysqlconn.Open();
//填写查询语句 创建执行sql语句的command
string mysqlcommandstring = "select * from user";
MySqlCommand mysqlcommand = new MySqlCommand(mysqlcommandstring, mysqlconn);
//穿件datareader数据阅读对象
MySqlDataReader mysqldatareader = mysqlcommand.ExecuteReader();
//兑取数据并显示
string name = myname.Text;
string password = mypassword.Text;
int tag = 1;
while (mysqldatareader.Read())
{
if (name.Equals(mysqldatareader["user_name"]) && password.Equals(mysqldatareader["user_password"]))
{
tag = 2;
Response.Redirect("success.aspx");
}
}
if (tag == 1)
{
Response.Write("登陆失败");
}
}
}
7.6.2实现GridView空间和ADO.NET实现数据操作