MySQL和.Net2.0配合使用

    技术2022-05-11  51

     

    MySql现在的最新版本是5.x.,用php,那时好像还是4.x版本。

    Mysql5增加很多新的功能,开始支持:存储过程、触发器、视图、信息架构视图等...

    MySql在安装时一如既往的比较复杂,往往就是一个失败的提示,没有什么其它提示原因。

    这是一篇文章,比较MySql和SqlServer的,http://htm.winsteps.net/database/331.htm

    MySql中文网站http://www.mysql.cn/上资料很少,大多是些安装帮助。要查资料还是去MySql的网站http://www.mysql.com/。

    MySql现在有提供的各种连接工具(http://dev.mysql.com/downloads/connector/),.net下可以用的有Connector/ODBC和Connector/Net。

    ODBC连接效率可能稍低,最好还是用Net直接的连接这篇文章介绍了各种连接方法http://www.mysql.com/news-and-events/press-release/release_2002_10.html

    1:ODBC连接      现在的版本是3.51,安装之后,可以这样操作:               // string conStr = "DRIVER = {MySQL ODBC 3.51 Driver}; SERVER = localhost; DATABASE =test; UID = root; PASSWORD=;";

                 //string conStr = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=test;USER=root;PASSWORD=;OPTION=3;";            string conStr = "provider = MySQL ODBC 3.51 Driver; SERVER = localhost; DATABASE =test; UID = root; PASSWORD=;";

                try            {                OleDbConnection  connection = new OleDbConnection(conStr);               connection.Open();            }            catch(Exception ex)            {              MessageBox.Show(ex.Message);            }2:Net连接:     MySQL Connector Net 1.0.7:有net1.0;net.1;net2.0;mono1.0四个版本的connector。免费     CoreLab.MySql 3.5:这是个商业的版本,试用期30天。

        下边的代码是使用MySQL Connector Net 的例子。注意:他的Parameter的前缀是“?”而不是“@”。这个问题比较特殊。CoreLab里面的Parameter的前缀就是“@”.      string connStr = String.Format("server={0};user id={1}; password={2}; database={3}; pooling=false;port=3308", "localhost", "root", "", "test");            try            {                MySqlConnection myConn = new MySqlConnection(connStr);                myConn.Open();                MySqlCommand cmd = myConn.CreateCommand();                                cmd.Parameters.Add("?DocName", MySqlDbType.VarChar, 50);                cmd.Parameters[0].Value = "test by code";                cmd.Parameters[0].SourceColumn = "DocName";                cmd.CommandText = "update t_docs set DocName=?DocName where DocId=4";                cmd.ExecuteNonQuery();这是使用一个ORM时设置provider的例子CustomProvider mysqlProvider = new CustomProvider("MySql.Data", "MySql.Data.MySqlClient.MySqlConnection", "MySql.Data.MySqlClient.MySqlDataAdapter");                        mysqlProvider.StartDelimiter = "";//default is "/""                        mysqlProvider.EndDelimiter = "";//default is "/""                        mysqlProvider.ParameterPrefix = "?";//设置参数前缀                        mysqlProvider.SelectPageQuery = "SELECT * LIMIT {0} OFFSET {1}";//设置分页算法                        mysqlProvider.IdentityQuery = "SELECT LAST_INSERT_ID()";//设置获取刚刚插入记录Id的函数

    3:OLE连接:      现在还没有来自官方的支持。

    在vs2005中,直接引用for .net2.0版本的dll即可。至于那个商业版,就得费些功夫了,需要一个许可文件(拖动一个Conection组件到Form上就能自动创建该许可)

    附,连接字符串可以到这里查询http://www.connectionstrings.com/, 够全的了。

     

    最新回复(0)