缘起 由于一些特别的原因,我再次短暂的回到 windows ,回到了 visualstudio2010 和 c# 。习惯了 ubuntu/linux 的快速高效的开发环境,对 windows 下的开发工具的庞大臃肿反应慢既愤慨又无奈。由于不想使用和 visaulstudio2010 相同的臃肿的开发环境,就使用
缘起由于一些特别的原因,我再次短暂的回到windows,回到了visual studio 2010和c#。习惯了ubuntu/linux的快速高效的开发环境,对windows下的开发工具的庞大臃肿反应慢既愤慨又无奈。由于不想使用和visaul studio 2010相同的臃肿的开发环境,就使用c#+mysql这个组合。以下是一些记录。
正文连接数据库最重要是找到驱动程序,odbc、jdbc是数据库连接的接口的标准,实现这些接口称为驱动程序。无论什么语言何种平台,都需要连接数据库都需要相应的驱动器。比如连接mysql数据库,java就需要java的连接器,.net就需要.net的连接器,像ruby这类的脚本程序也需要相应的数据库连接包。
关于在windows下用c#开发应用程序,如果是asp.net+c#的话,没的选,只有visual studio;如果开发c#的winform程序或控制台程序,ide可以选择sharpdevelop或者monodevelop,sharpdevelop的启动速度很快,几乎秒开,但sharpdevelop只适用windows;monodevelop可以跨平台,可以在windows或linux下使用。
1. c#连接mysql关于c#连接数据库方法是使用mysql官方的提供的connector-net的包,然后将其引用到项目中,这个方式可以适用与任何使用c#开发的程序,包括visual studio和monodevelop创建的项目。具体的操作步骤如下:
1.下载connector
mysql的connector-net下载地址:http://dev.mysql.com/downloads/connector/net/
2014年5月份,最新的连接版本是:6.8.3,该包中含有的文件:
其中,vx.0表示的是.netframework的版本号,根据项目使用的.netframework选择相应的目录下的mysql.**.dll。
2.在项目中引用 mysql-connector-net包中的mysql.data.dll(注意引用和项目使用框架相同的版本dll)
3.设置数据库连接字符串
字符串的样例如下:
server=localhost;user id=root;password=localhost;database=web;port=3306;charset=utf8;
一般而言,这里需要修改的只有password和database,其他的都可以使用默认。
4.简单测试数据库连接的demo程序
using system;using system.collections.generic;using mysql.data.mysqlclient;//引用mysql.data.dll中的类 namespace testdb{ class program { static void main(string[] args) { string query = select * from t_user; mysqlconnection myconnection = new mysqlconnection(server=localhost;user id=root;password=11;database=db_user); mysqlcommand mycommand = new mysqlcommand(query, myconnection); myconnection.open(); mycommand.executenonquery(); mysqldatareader mydatareader = mycommand.executereader(); string bookres = ; while (mydatareader.read() == true) { bookres += mydatareader[id]; bookres += mydatareader[username]; bookres += mydatareader[password]; } mydatareader.close(); myconnection.close(); console.writeline(bookres); } }}
2. mysqlhelper辅助类像上面的测试连接的样例中那样,每次都自己编写相应的连接之类的非常不方便,此时,可以考虑使用一个dbhelper这样的辅助类来减少重复代码。下面是一个简单的dbheper辅助类:
public class mysqlhelper{ private static string connectionstring = configurationmanager.connectionstrings[mysqlconn].connectionstring; /// /// 执行查询语句,返回dataset /// /// 查询语句 /// dataset public static dataset query(string sqlstring) { using (mysqlconnection connection = new mysqlconnection(connectionstring)) { dataset ds = new dataset(); try { connection.open(); mysqldataadapter command = new mysqldataadapter(sqlstring, connection); command.fill(ds); } catch (system.data.sqlclient.sqlexception ex) { throw new exception(ex.message); } finally { connection.close(); } return ds; } } /// /// 执行sql语句,返回影响的记录数 /// /// sql语句 /// 影响的记录数 public static int executesql(string sqlstring) { using (mysqlconnection connection = new mysqlconnection(connectionstring)) { using (mysqlcommand cmd = new mysqlcommand(sqlstring, connection)) { try { connection.open(); int rows = cmd.executenonquery(); return rows; } catch (system.data.sqlclient.sqlexception e) { connection.close(); throw e; } finally { cmd.dispose(); connection.close(); } } } } /// /// 执行sql语句,返回影响的记录数 /// /// sql语句 /// 影响的记录数 public static int executesql(string[] arrsql) { using (mysqlconnection connection = new mysqlconnection(connectionstring)) { try { connection.open(); mysqlcommand cmdencoding = new mysqlcommand(set_encoding, connection); cmdencoding.executenonquery(); int rows = 0; foreach (string strn in arrsql) { using (mysqlcommand cmd = new mysqlcommand(strn, connection)) { rows += cmd.executenonquery(); } } return rows; } catch (system.data.sqlclient.sqlexception e) { connection.close(); throw e; } finally { connection.close(); } } }}
备注:sqlserver代码改写成mysql很容易,由于二者格式几乎一样,而改写的部分也很少。
后记再次回到windows修改c#的winform程序,让我反思了一些问题,比如rails到底能做到什么,桌面程序和web程序优缺点这类的问题,也算不错。不过,再次明白windows确实不是一个好的开发环境。
回想起来,自己当初在选择技术时,选择的是c#,为此花费了一年的时间来学习c#。后来,跟老师搞研究,转战java,最后,在拥抱ubuntu一年后,选择了rails和ruby作为谋生的工具。这次,再次回到短暂的windows上,让我想到当初花了很长的时间学习,也应该积累了很多的经验,可惜的是都没有记下来,然后,就全忘光了。
参考文献1..net mysql-connector-net连接mysql
2.asp.net连接mysql(connector/net 5.0)
