ASP.NET操作MySql数据库的实例代码讲解
更新时间:2016年12月25日 08:25:59 作者:贝壳
这篇文章主要介绍了ASP.NET操作MySql数据库的实例代码讲解,需要的朋友可以参考下
一、把MySql.Data.dll放到BIN目录下。
二、这是aspx.cs的全部源码,修改参数直接运行即可!
using MySql.Data.MySqlClient; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Login : System.Web.UI.Page { public static class MySqlHelper { public static int ExecuteNonQuery(string connectionString, CommandType commandtype, string commandText) { return ExecuteNonQuery(connectionString, commandtype, commandText, null); } public static int ExecuteNonQuery(string connectionString, CommandType commandtype, string commandText, params MySqlParameter[] commandParameters) { if (string.IsNullOrEmpty(connectionString)) { throw new Exception("connectionString exception"); } int result = 0; MySqlConnection con = null; try { using (con = new MySqlConnection(connectionString)) { con.Open(); MySqlCommand command = new MySqlCommand(commandText, con); command.CommandType = commandtype; result = command.ExecuteNonQuery(); } return result; } catch (Exception ex) { throw ex; } finally { if (con.State == ConnectionState.Open) { con.Close(); } } } } protected void Page_Load(object sender, EventArgs e) { string connectionString = "server=localhost;uid=root;pwd=;database=zentaopro;"; LogManage_SqlDate.WriteLog("connectionString=:" + connectionString); string sql = "insert user(account) values('china2')"; MySqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, sql); Console.Read(); } }
以上所述是小编给大家介绍的ASP.NET操作MySql数据库的实例代码讲解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
相关文章
ASP.net(c#)用类的思想实现插入数据到ACCESS例子
ASP.net(c#)用类的思想实现插入数据到ACCESS例子...2007-07-07ASP.NET中GridView、DataList、DataGrid三个数据控件foreach遍历用法示例
这篇文章主要介绍了ASP.NET中GridView、DataList、DataGrid三个数据控件foreach遍历用法,结合实例形式分析了GridView、DataList、DataGrid使用foreach及for语句进行数据遍历的具体使用方法,需要的朋友可以参考下2016-08-08VS2012/VS2013本地发布网站问题集锦(HTTP错误代码)
这篇文章主要为大家详细介绍了VS2012/VS2013本地发布网站遇到问题,HTTP错误代码的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-04-04asp.net使用LINQ to SQL连接数据库及SQL操作语句用法分析
这篇文章主要介绍了asp.net使用LINQ to SQL连接数据库及SQL操作语句用法,较为详细的分析了LINQ操作sql语句的功能、使用方法与相关注意事项,需要的朋友可以参考下2016-05-05
最新评论