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

掌握添加MySQL用戶的方法對於數據庫管理員和開發者至關重要,因為它確保數據庫的安全性和訪問控制。 1)使用CREATEUSER命令創建新用戶,2)通過GRANT命令分配權限,3)使用FLUSHPRIVILEGES確保權限生效,4)定期審計和清理用戶賬戶以維護性能和安全。

chosecharforfixed-lengthdata,varcharforvariable-lengthdata,andtextforlargetextfield.1)chariseffity forconsistent-lengthdatalikecodes.2)varcharsuitsvariable-lengthdatalikenames,ballancingflexibilitibility andperformance.3)

在MySQL中處理字符串數據類型和索引的最佳實踐包括:1)選擇合適的字符串類型,如CHAR用於固定長度,VARCHAR用於可變長度,TEXT用於大文本;2)謹慎索引,避免過度索引,針對常用查詢創建索引;3)使用前綴索引和全文索引優化長字符串搜索;4)定期監控和優化索引,保持索引小巧高效。通過這些方法,可以在讀取和寫入性能之間取得平衡,提升數據庫效率。

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

tostorestringsefliceflicyInmySql,ChooSetherightDataTypeBasedyOrneOrneEds:1)USEcharforFixed-LengthStstringStringStringSlikeCountryCodes.2)UseVarcharforvariable-lengtthslikenames.3)USETEXTCONTENT.3)

選擇MySQL的BLOB和TEXT數據類型時,BLOB適合存儲二進制數據,TEXT適合存儲文本數據。 1)BLOB適用於圖片、音頻等二進制數據,2)TEXT適用於文章、評論等文本數據,選擇時需考慮數據性質和性能優化。

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

mySqlStringDatatAtatPessHouldBechoseBasedondatActarActeristicsAndusecases:1)USEcharforFixed lengthStstringStringStringSlikeCountryCodes.2)usevarcharforvariable-lengtthslikeLikenames.3)usebarnionororvarinyorvarinyorvarybinarydatalgebenedaTalgeextocrabextrapon.4)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver Mac版
視覺化網頁開發工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版
中文版,非常好用

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!