在.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> |

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

toadduserInmysqleffectection andsecrely,theTheSepsps:1)USEtheCreateuserStattoDaneWuser,指定thehostandastrongpassword.2)GrantNectalRevileSaryPrivilegesSustate,usiveleanttatement,AdheringTotheTeprinciplelastPrevilegege.3)

toaddanewuserwithcomplexpermissionsinmysql,loldtheSesteps:1)创建eTheEserWithCreateuser'newuser'newuser'@''localhost'Indedify'pa ssword';。2)GrantreadAccesstoalltablesin'mydatabase'withGrantSelectOnMyDatabase.to'newuser'@'localhost';。3)GrantWriteAccessto'

MySQL中的字符串数据类型包括CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT,排序规则(Collations)决定了字符串的比较和排序方式。1.CHAR适合固定长度字符串,VARCHAR适合可变长度字符串。2.BINARY和VARBINARY用于二进制数据,BLOB和TEXT用于大对象数据。3.排序规则如utf8mb4_unicode_ci忽略大小写,适合用户名;utf8mb4_bin区分大小写,适合需要精确比较的字段。

最佳的MySQLVARCHAR列长度选择应基于数据分析、考虑未来增长、评估性能影响及字符集需求。1)分析数据以确定典型长度;2)预留未来扩展空间;3)注意大长度对性能的影响;4)考虑字符集对存储的影响。通过这些步骤,可以优化数据库的效率和扩展性。

mysqlblobshavelimits:tinyblob(255bytes),blob(65,535 bytes),中间布洛布(16,777,215个比例),andlongblob(4,294,967,967,295 bytes).tousebl观察性:1)考虑performance impactsandSandStorelargeblobsextern; 2)管理backbackupsandreplication carecration; 3)usepathsinst

自动化在MySQL中创建用户的最佳工具和技术包括:1.MySQLWorkbench,适用于小型到中型环境,易于使用但资源消耗大;2.Ansible,适用于多服务器环境,简单但学习曲线陡峭;3.自定义Python脚本,灵活但需确保脚本安全性;4.Puppet和Chef,适用于大规模环境,复杂但可扩展。选择时需考虑规模、学习曲线和集成需求。

是的,YouCansearchInIdeAblobInMysqlusingsPecificteChniques.1)转换theblobtoautf-8StringWithConvertFunctionWithConvertFunctionandSearchusiseLike.2)forCompresseBlyblobs,useuncompresseblobs,useuncompressbeforeconversion.3)acpperformance impperformance imperformance imptactsanddataEccoding.4)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

禅工作室 13.0.1
功能强大的PHP集成开发环境