如何在.NET中使用MySQL数据库
在.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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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> Copy after login |

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.
