在.NET中一直使用Sql Server做数据库。前几天学习通过JDBC使用Java连接MySql数据库,我就想.NET是否同样可以使用MySql数据库呢?答案是肯定的,方法也很简单,在这里我使用C#作为编程语言,将自己整理出来的方法同大家一起分享。 欲在.NET中使用MySql数据库
在.NET中一直使用Sql Server做数据库。前几天学习通过JDBC使用Java连接MySql数据库,我就想.NET是否同样可以使用MySql数据库呢?答案是肯定的,方法也很简单,在这里我使用C#作为编程语言,将自己整理出来的方法同大家一起分享。
欲在.NET中使用MySql数据库,其步骤是:
1. 下载MySql驱动包
2. 引用驱动包中已经编译好的MySql类组件
3. 编写代码
步骤一:下载MySql驱动包
在我写篇文章的时候,最新的驱动包是5.2,支持Visual Studio 2008。官方下载地址是http://dev.mysql.com/downloads/connector/net/5.2.html
步骤二:引入组件
下载的文件是一个压缩文件,将其解压缩到本地磁盘。找到bin文件夹中的MySql.Data.dll文件,这个就是我们要引用的组件。通过Visual Studio将其引用到你的项目中
步骤三:编码,我编写了一个控制台的示例,并将重要的部分都添加了注释,代码如下:
<font color="#008080">1<img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922471.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_0_59_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_0_59_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_0_59_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_0_59_Closed_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" ><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922472.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_0_59_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_0_59_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_0_59_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_0_59_Open_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" ></font><span id="Codehighlighter1_0_59_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</span><span id="Codehighlighter1_0_59_Open_Text"><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">*<br></span><span style="COLOR: #008080"> 2</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> * 程序名称:使用.NET连接MySql数据库<br></span><span style="COLOR: #008080"> 3</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> * 作者:吴磊<br></span><span style="COLOR: #008080"> 4</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> * 日期:2009年01月17日<br></span><span style="COLOR: #008080"> 5</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922476.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #008000">*/</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080"> 6</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922477.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"><br></span><span style="COLOR: #008080"> 7</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922477.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 使用MySql.Data.MySqlClient命名空间</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080"> 8</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922477.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000"> System;<br></span><span style="COLOR: #008080"> 9</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922477.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000"> MySql.Data.MySqlClient;<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922477.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"><br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922477.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"></span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000"> Project_Personnel.db<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922471.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_169_2590_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_169_2590_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_169_2590_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_169_2590_Closed_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" ><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922472.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_169_2590_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_169_2590_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_169_2590_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_169_2590_Open_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" ></span><span id="Codehighlighter1_169_2590_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224715.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt=""></span><span id="Codehighlighter1_169_2590_Open_Text"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> dbManage<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224717.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_194_2588_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_194_2588_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_194_2588_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_194_2588_Closed_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" ><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224718.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_194_2588_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_194_2588_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_194_2588_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_194_2588_Open_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" > </span><span id="Codehighlighter1_194_2588_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224715.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt=""></span><span id="Codehighlighter1_194_2588_Open_Text"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224717.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_204_311_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_204_311_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_204_311_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_204_311_Closed_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" ><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224718.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_204_311_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_204_311_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_204_311_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_204_311_Open_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" > </span><span id="Codehighlighter1_204_311_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</span><span id="Codehighlighter1_204_311_Open_Text"><span style="COLOR: #808080">///</span><span style="COLOR: #008000"> </span><span style="COLOR: #808080"><summary></summary></span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">16</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #808080">///</span><span style="COLOR: #008000"> 连接到MySql数据库并返回连接对象<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #808080">///</span><span style="COLOR: #008000"> </span><span style="COLOR: #808080"></span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">18</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224724.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #808080">///</span><span style="COLOR: #008000"> </span><span style="COLOR: #808080"><returns></returns></span><span style="COLOR: #008000">数据库连接对象</span><span style="COLOR: #808080"></span></span><br><span style="COLOR: #008080">19</span><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000"> MySqlConnection getCon()<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224717.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_367_1026_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_367_1026_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_367_1026_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_367_1026_Closed_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" ><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224718.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_367_1026_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_367_1026_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_367_1026_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_367_1026_Open_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" > </span><span id="Codehighlighter1_367_1026_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224715.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt=""></span><span id="Codehighlighter1_367_1026_Open_Text"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> MySqlConnection myCon </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224717.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_439_843_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_439_843_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_439_843_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_439_843_Closed_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" ><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224718.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_439_843_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_439_843_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_439_843_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_439_843_Open_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" > </span><span id="Codehighlighter1_439_843_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224715.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt=""></span><span id="Codehighlighter1_439_843_Open_Text"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224717.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_457_687_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_457_687_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_457_687_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_457_687_Closed_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" ><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224718.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_457_687_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_457_687_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_457_687_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_457_687_Open_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" > </span><span id="Codehighlighter1_457_687_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</span><span id="Codehighlighter1_457_687_Open_Text"><span style="COLOR: #008000">/*</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">25</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> * Server:数据库服务器。localhost表示本机<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> * Database:数据库名称。school表示mySql中一个名为school的数据库<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> * Uid:用户名<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> * Pwd:密码<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> * CharSet:使用的字符编码。不设置可能产生乱码<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224724.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #008000">*/</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> myCon </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> MySqlConnection(</span><span style="COLOR: #800000">"</span><span style="COLOR: #800000">Server=localhost;Database=school;<br>Uid=root;Pwd=123;CharSet=utf8;</span><span style="COLOR: #800000">"</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> myCon.Open();<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224724.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000"> (MySqlException e)<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224717.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_893_990_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_893_990_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_893_990_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_893_990_Closed_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" ><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224718.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_893_990_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_893_990_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_893_990_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_893_990_Open_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" > </span><span id="Codehighlighter1_893_990_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224715.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt=""></span><span id="Codehighlighter1_893_990_Open_Text"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">抛出连接MySql数据库的异常</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">37</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"></span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">throw</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> Exception(e.Message);<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224724.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> myCon;<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224724.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"><br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> Main()<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224717.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_1070_2582_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_1070_2582_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_1070_2582_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_1070_2582_Closed_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" ><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224718.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_1070_2582_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_1070_2582_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_1070_2582_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_1070_2582_Open_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" > </span><span id="Codehighlighter1_1070_2582_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224715.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt=""></span><span id="Codehighlighter1_1070_2582_Open_Text"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224717.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_1100_2227_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_1100_2227_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_1100_2227_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_1100_2227_Closed_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" ><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224718.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_1100_2227_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_1100_2227_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_1100_2227_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_1100_2227_Open_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" > </span><span id="Codehighlighter1_1100_2227_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224715.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt=""></span><span id="Codehighlighter1_1100_2227_Open_Text"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">从shool表中检索数据的sql语句(别误会,我是在school数据库中建立了一个school表)</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">47</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"></span><span style="COLOR: #000000"> MySqlCommand myCmd </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> MySqlCommand(</span><span style="COLOR: #800000">"</span><span style="COLOR: #800000">select * from school</span><span style="COLOR: #800000">"</span><span style="COLOR: #000000">, getCon());<br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> <br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">执行检索语句并将执行结果赋值给MySqlDataReader对象</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">50</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"></span><span style="COLOR: #000000"> MySqlDataReader myReader </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> myCmd.ExecuteReader();<br></span><span style="COLOR: #008080">51</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"><br></span><span style="COLOR: #008080">52</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">进行读取</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">53</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"></span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000"> (myReader.Read())<br></span><span style="COLOR: #008080">54</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224717.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_1472_2213_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_1472_2213_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_1472_2213_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_1472_2213_Closed_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" ><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224718.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_1472_2213_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_1472_2213_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_1472_2213_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_1472_2213_Open_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" > </span><span id="Codehighlighter1_1472_2213_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224715.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt=""></span><span id="Codehighlighter1_1472_2213_Open_Text"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">55</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">myReader的FieldCount属性表示列数</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">56</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"></span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> (</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> i </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">0</span><span style="COLOR: #000000">; i </span><span style="COLOR: #000000"><span style="COLOR: #000000"> myReader.FieldCount; i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">57</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224717.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_1608_2129_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_1608_2129_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_1608_2129_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_1608_2129_Closed_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" ><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224718.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_1608_2129_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_1608_2129_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_1608_2129_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_1608_2129_Open_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" > </span><span id="Codehighlighter1_1608_2129_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224715.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt=""></span><span id="Codehighlighter1_1608_2129_Open_Text"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">58</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224717.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_1634_1784_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_1634_1784_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_1634_1784_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_1634_1784_Closed_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" ><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224718.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_1634_1784_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_1634_1784_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_1634_1784_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_1634_1784_Open_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" > </span><span id="Codehighlighter1_1634_1784_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</span><span id="Codehighlighter1_1634_1784_Open_Text"><span style="COLOR: #008000">/*</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">59</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> * 提示:如果使用此if语句,会产生异常:数据为空,不能对空值调用此方法或属性。<br></span><span style="COLOR: #008080">60</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> * 判断索引为i的列中的值是否为空,如果为空输出NULL<br></span><span style="COLOR: #008080">61</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224724.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #008000">*/</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">62</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">myReader[i].Equals(DBNull.Value))<br></span><span style="COLOR: #008080">63</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224717.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_1872_1965_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_1872_1965_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_1872_1965_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_1872_1965_Closed_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" ><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224718.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_1872_1965_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_1872_1965_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_1872_1965_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_1872_1965_Open_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" > </span><span id="Codehighlighter1_1872_1965_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224715.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt=""></span><span id="Codehighlighter1_1872_1965_Open_Text"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">64</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> Console.Write(</span><span style="COLOR: #800000">"</span><span style="COLOR: #800000">{0,10}</span><span style="COLOR: #800000">"</span><span style="COLOR: #000000">, myReader[i]);<br></span><span style="COLOR: #008080">65</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224724.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">66</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">67</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224717.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_2019_2107_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_2019_2107_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_2019_2107_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_2019_2107_Closed_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" ><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224718.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_2019_2107_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_2019_2107_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_2019_2107_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_2019_2107_Open_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" > </span><span id="Codehighlighter1_2019_2107_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224715.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt=""></span><span id="Codehighlighter1_2019_2107_Open_Text"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">68</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> Console.Write(</span><span style="COLOR: #800000">"</span><span style="COLOR: #800000">{0,10}</span><span style="COLOR: #800000">"</span><span style="COLOR: #000000">, </span><span style="COLOR: #800000">"</span><span style="COLOR: #800000">Null</span><span style="COLOR: #800000">"</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">69</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224724.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">70</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224724.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">71</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">换行</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">72</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"></span><span style="COLOR: #000000"> Console.WriteLine();<br></span><span style="COLOR: #008080">73</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224724.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">74</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224724.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">75</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">捕获MySql异常</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">76</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"></span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000"> (MySqlException e)<br></span><span style="COLOR: #008080">77</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224717.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_2301_2362_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_2301_2362_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_2301_2362_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_2301_2362_Closed_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" ><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224718.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_2301_2362_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_2301_2362_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_2301_2362_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_2301_2362_Open_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" > </span><span id="Codehighlighter1_2301_2362_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224715.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt=""></span><span id="Codehighlighter1_2301_2362_Open_Text"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">78</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> Console.WriteLine(e.Message);<br></span><span style="COLOR: #008080">79</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224724.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">80</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">捕获其他异常</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">81</span><span style="COLOR: #008000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"></span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000"> (Exception e)<br></span><span style="COLOR: #008080">82</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224717.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_2428_2489_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_2428_2489_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_2428_2489_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_2428_2489_Closed_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" ><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224718.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_2428_2489_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_2428_2489_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_2428_2489_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_2428_2489_Open_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" > </span><span id="Codehighlighter1_2428_2489_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224715.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt=""></span><span id="Codehighlighter1_2428_2489_Open_Text"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">83</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> Console.WriteLine(e.Message);<br></span><span style="COLOR: #008080">84</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224724.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">85</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> </span><span style="COLOR: #0000ff">finally</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">86</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224717.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_2522_2572_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_2522_2572_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_2522_2572_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_2522_2572_Closed_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" ><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224718.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" id="Codehighlighter1_2522_2572_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_2522_2572_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_2522_2572_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_2522_2572_Open_Text').style.display='inline';" align="top" alt="如何在.NET中使用MySQL数据库" > </span><span id="Codehighlighter1_2522_2572_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224715.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt=""></span><span id="Codehighlighter1_2522_2572_Open_Text"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">87</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922473.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> Console.ReadKey();<br></span><span style="COLOR: #008080">88</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224724.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">89</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224724.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">90</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/09224724.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top"> }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">91</span><span style="COLOR: #000000"><img class="fit-image lazy" src="/static/imghwm/default1.png" data-src="/files/uploadimg/20090119/0922476.gif" onload="javascript:if(this.width>498)this. style="max-width:90%" onmousewheel="javascript:return big(this)" alt="" align="top">}</span></span><span style="COLOR: #000000"><br></span></span> |

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use
