.net使用mysql及示例...
时间:2010-08-07 来源:tzjzcy
1.到mysql官网下载mysql-connector-net安装;
2.vs2008创建新项目,添加引用MySql.Data;
3.添加命名空间using MySql.Data.MySqlClient;
4.web.config:
<connectionStrings>
<add name="MySqlDB" connectionString="Database=test;Data Source=localhost;User Id=root;Password=ttt" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
5.存储过程:
create procedure GetAllPerson()
select name from person
6.示例:
//连接
MySqlConnection mySqlCon = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MySqlDB"].ConnectionString);
//命令
MySqlCommand mySqlCmd = new MySqlCommand("GetAllPerson", mySqlCon);
//命令类型为存储过程
mySqlCmd.CommandType = CommandType.StoredProcedure;
//适配器
MySqlDataAdapter mySqlDa = new MySqlDataAdapter(mySqlCmd);
DataSet ds = new DataSet();
mySqlDa.Fill(ds);
mySqlCon.Close();
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();