作业帮 > ASP.NET > 教育资讯

asp.net技巧:asp.net管理mysql数据库的小工具

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/27 08:37:18 ASP.NET
asp.net技巧:asp.net管理mysql数据库的小工具
asp.net技巧:asp.net管理mysql数据库的小工具ASP.NET
【无忧考网-asp技巧:asp管理mysql数据库的小工具】:

一、打开mysql数据库:    MySqlConnection DBConn = new MySqlConnection();    string connString ="Host=127.0.0.1;UserName=root;PassWord=root;Database=test;Port=3306;CharSet=utf8;Allow Zero Datetime=true";    DBConn.ConnectionString = connString;    DBConn.Open();

二、执行sql命令    string sqlstr="select * from test";    MySqlCommand command = new MySqlCommand(sqlstr, DBConn);    command.ExecuteNonQuery();

从上面可以看出,用法跟操作SQL数据库的SqlConnection、SqlCommand非常相似,下面我这里有一个完整的例子来实现对mysql数据库进行管理,包括浏览表结构和数据、查询-修改-插入-删除数据,导出数据和表结构等,供大家参考和互相学习。代码写得不规范,还请大家指正。 <%@ Page Language="C#" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Text" %> <%@ Import Namespace="MySql.Data.MySqlClient" %>      shouji138 MYSQL Manager (DoNet)    


    

        
                          

                     MYSQL Manager (DoNet) »

                 密码:                                                 
                                           

                     MYSQL Manager (DoNet) »

                                 

                     主机IP:                                          :                                          用户名:                                          密码:                                                               数据库名:                                                               数据库编码:                                                          

                

                 选择数据库:                                                                    

            
            
                 <%if (Session["dbname"] != null && Session["dbname"].ToString() != string.Empty)               { %>                 当前数据库:                      <%=dbname.Value %>                                  <%                     } %>                 <%if (Request.QueryString["tblname"] != null)                   { %>                 | 当前表: ">                     <%=Request.QueryString["tblname"] %>                 [ ">添加                 | ">结构                 | " onclick="return confirm('确定删除表“<%=Request.QueryString["tblname"] %>”?')">                     删除表 ]                 <%                     } %>            
                                      
             数据库已经导出为                                      
                                                                                                                                                            
                         运行SQL语句 :
                                                 
                           <%                             DataTable dColumn = RunTable(sql_query.Value);                              %>                                                               <%                                                         for (int i = 0; i < dColumn.Columns.Count; i++)                             {                         %>                                                  <%                             }                         %>                                          <%                          DataTable dData = dColumn;

                        for (int i = 0; i < dData.Rows.Count; i++)                         {                     %>                    

" onmouSEOver="this.className='focus';" onmouseout="this.className='alt<%=i%2==0?"1":"2" %>';">                         <%                               for (int j = 0; j < dData.Columns.Count; j++)                             {                         %>                                                  <%                             }                         %>                                          <%                         }                     %>                
                             <%= dColumn.Columns[i].Caption%>                        
                             <%= dData.Rows[i][j].ToString()%>                              
                
                 运行的SQL :                 <%=sql_query.Value%>            
                                                                                                                                                                                                                                               <%                         for (int i = 0; i < tblsDt.Rows.Count; i++)                         {                             tblRowsCount += int.Parse(tblsDt.Rows[i]["Rows"].ToString());                             tblsCount++;                             tblDbSize += float.Parse(tblsDt.Rows[i]["Data_length"].ToString());                         %>                     ">                                                                                                                                                                                            <%                         }                      %>                                                                                                                                                                                                                                                  
                             Name                             Rows                             Data_length                             Create_time                             Update_time
                     " />                                                  ">                                 <%= tblsDt.Rows[i]["Name"]%>                             [ ">添加 | ">                                 结构 | " onclick="return confirm('确定删除表“<%= tblsDt.Rows[i]["Name"]%>”?')">                                     删除表 ]                                                      <%= tblsDt.Rows[i]["Rows"]%>                                                      <%= showSize(float.Parse(tblsDt.Rows[i]["Data_length"].ToString()))%>                                                      <%= tblsDt.Rows[i]["Create_time"]%>                                                      <%= tblsDt.Rows[i]["Update_time"]%>                        
                              合计:<%= tblsCount%>                                                      <%= tblRowsCount%>                                                      <%= showSize(tblDbSize)%>                                                       
                         保存为文件                                                                       
            
                                                                                                     <%                             DataTable dColumn = TableColumn(Request.QueryString["tblname"].ToString().Trim());                             for (int i = 0; i < dColumn.Rows.Count; i++)                             {                         %>                                                  <%                             }                         %>                                          <%                          DataTable dData = RunTable(sql_query.Value);                         string tblPkName = FindPK(Request.QueryString["tblname"].ToString().Trim());                         for (int i = 0; i < dData.Rows.Count; i++)                         {                     %>                     " onmouseover="this.className='focus';" onmouseout="this.className='alt<%=i%2==0?"1":"2" %>';">                                                  <%                               for (int j = 0; j < dData.Columns.Count; j++)                             {                         %>                                                  <%                             }                         %>                                          <%                         }                     %>                
                             Action                             <%= dColumn.Rows[i][0]%>                            
                                                              <%= dColumn.Rows[i][1]%>                                                     
                             <%if (tblPkName != string.Empty)                               {%>                             &pk=<%=tblPkName %>&v=<%=dData.Rows[i][tblPkName] %>">                                 编辑 | &pk=<%=tblPkName %>&v=<%=dData.Rows[i][tblPkName] %>"                                     onclick="return confirm('确定删除该记录?')">删除                             <%                                 } %>                                                      <%= dData.Rows[i][j]%>                              
            
                             

                     在表<%=Request.QueryString["tblname"].Trim() %>中编辑记录 »

                                      <%                         DataTable dColumn = TableColumn(Request.QueryString["tblname"].ToString().Trim());                         DataTable editData = RunTable("select * from " + Request.QueryString["tblname"].ToString() + " where " + Request.QueryString["pk"].ToString() + " = " + Request.QueryString["v"].ToString() + "");

                        if (editData.Rows.Count > 0)                         {                             for (int i = 0; i < dColumn.Rows.Count; i++)                             {                     %>                    

" onmouseover="this.className='focus';" onmouseout="this.className='alt<%=i%2==0?"1":"2" %>';">                                                                                            <%                         }                                                                      %>                                                                                        <% } %>                
                                                              <%= dColumn.Rows[i][0]%>                                                         
                             <%= dColumn.Rows[i][1]%>                        
                            
                                                     
            
                             

                     在表<%=Request.QueryString["tblname"].Trim() %>中添加记录 »

                                      <%                         DataTable dColumn = TableColumn(Request.QueryString["tblname"].ToString().Trim());


                        for (int i = 0; i < dColumn.Rows.Count; i++)                         {                     %>                    

" onmouseover="this.className='focus';" onmouseout="this.className='alt<%=i%2==0?"1":"2" %>';">                                                                                            <%                         }                                                                      %>                                                                                   
                                                              <%= dColumn.Rows[i][0]%>                                                         
                             <%= dColumn.Rows[i][1]%>                        
                            
                                                     
            
                             

                     表<%=Request.QueryString["tblname"].Trim() %>的结构 »

                                                                                                                                                                                                                                      <%                         DataTable dColumn = TableStructure(Request.QueryString["tblname"].ToString().Trim());


                        for (int i = 0; i < dColumn.Rows.Count; i++)                         {                     %>                    

" onmouseover="this.className='focus';" onmouseout="this.className='alt<%=i%2==0?"1":"2" %>';">                                                                                                                                                                                                <%                                              } %>                
                             Field                             Type                             Null                             Key                             Default                             Extra
                             <%= dColumn.Rows[i][0]%>                                                      <%= dColumn.Rows[i][1]%>                                                                                    <%= dColumn.Rows[i][2]%>                                                           <%= dColumn.Rows[i][3]%>                                                           <%= dColumn.Rows[i][4]%>                              
            
            
        
       Powered by shouji138

ASP.NET