Home Database Mysql Tutorial 用ODBC连接MySQL和ASP.NET_MySQL

用ODBC连接MySQL和ASP.NET_MySQL

Jun 01, 2016 pm 02:11 PM
mysql deal with Install database server structure connect

ODBC


  被认为是“世界上最流行的开放源代码数据库”的MySQL从最初一个毫不起眼的低成本数据库服务器成长为驱动庞大Web网站和重要商业系统的服务器经历了一个漫长的历程。但是,如果你是一位ASP.NET的开发人员,那么你总会碰到一个不如意的地方:MySQL原先是为UNIX平台上的应用程序设计的,所以对Windows的支持处于次要地位。
  
  但是现在,Windows版本的MySQL已经具备了与UNIX版本同样的特性和稳定性,而且它被认为是用于Windows开发的可行的数据库服务器。现在让我们来看看你可以如何使用ODBC在.NET框架内连接MySQL数据库。
  
  安装
  下载和安装Windows版的MySQL。安装很简单——只用按照提示你就能够立即安装好并开始运行了。如果碰到了问题,可以去MySQL的论坛看看,寻求帮助和解决方法。
  
  要把ASP.NET和MySQL连接起来,你需要使用ODBC.NET。一般来说,ODBC.NET的DataProvider是标准的.NET框架(1.1及以上的版本)的一部分,所以会和后者一起自动安装好。
  
  一旦确认ODBC.NET安装完毕,你就需要下载用于MySQL的ODBC驱动程序。再强调一遍,MySQL的开发人员都很乐意提供帮助——他们都在自己的Web网站上提供了这些驱动程序。在下载文件的时候你可以看看FAQ文档,它会列出在往系统里安装MySQL ODBC驱动程序期间可能碰到的所有的问题。
  
  都做好了?现在就让我们从一些代码开始吧。
  
  用ASP.NET连接MySQL
  我最喜欢做的一件事情是阅读,而且当我没有编写像本文一样的教学文章时,我会找一个安静的角落补全参考书目表。不幸的是,我不是一个组织性很强的人,所以这常常搞得一团糟。
  
  那这又与我们今天要谈的话题有什么关系呢?嗯,这是我第一个例子的开场白,这个例子就是创建一个像列表A里的书籍的数据库。
  
  要建立这个表格,就要使用下面的SQL查询:
  
  CREATE TABLE `books` (
  `id` int(5) NOT NULL auto_increment,
  `title` varchar(255) NOT NULL default '',
  `authors` varchar(255) NOT NULL default '',
  `year_of_publication` year(4) NOT NULL default '0000',
  `date_added` date NOT NULL default '0000-00-00',
  PRIMARY KEY (`id`)
  ) TYPE=MyISAM;
  而要执行这个查询,就要使用MySQL安装目录下的“bin”文件夹的命令行客户端软件“mysql.exe”。下面就是具体命令:
  
  c:\mysql\bin>mysql -u guest -p test
  Enter password: ******
  Welcome to the MySQL monitor. Commands end with ; or \g.
  Your MySQL connection id is 13 to server version: 4.0.12-nt
  
  Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  
  mysql> CREATE TABLE `books` (
  ->  `id` int(5) NOT NULL auto_increment,
  ->  `title` varchar(255) NOT NULL default '',
  ->  `authors` varchar(255) NOT NULL default '',
  ->  `year_of_publication` year(4) NOT NULL default '0000',
  ->  `date_added` date NOT NULL default '0000-00-00',
  ->  PRIMARY KEY (`id`)
  -> ) TYPE=MyISAM;
  Query OK, 0 rows affected (0.02 sec)
  
  一旦“书籍(books)”表格创建好了,就可以开始插入数据了。列表B里列举了一些项目。现在,用ASP.NET做同样的事情——执行SELECT查询(列表C)并在浏览器里显示结果。如果一切都没有问题——MySQL服务器在运行,MySQL ODBC驱动程序安装正确,“书籍”表格含有数据——你就应该会看到像图A里的页面。
   用ODBC连接MySQL和ASP.NET_MySQL
  现在让我们来更加仔细地看看代码列表。所有东西一开始都要调入所需要的.NET库。由于我正在使用ODBC.NET连接MySQL服务器,所以需要调用System.Data.Odbc和System.Data程序集。一旦程序集调用完毕,就要定义连接字符串,这包括修改服务器(Server)、数据库(Database)、uid和pwd变量以体现本地服务器的设置。
  
  在创建了OdbcConnection()和OdbcCommand()对象之后,初始化本地的OdbcDataReader()对象,从“书籍”表格取回数据。这个对象提供了一条ExecuteReader()命令来执行SQL查询。剩下的就是例行公事了:将OdbcDataReader()指定为dbgooks DataGrid的数据源,并调用DataGrid()对象的DataBind()方法将两者绑定起来。
  
  当然,你可以对数据库做更多的事情——向表格里插入(INSERT)数据、使用更新(UPDATE)修改它们、用删除(DELETE)命令获得记录的网格,或者就根据WHERE语句里面的条件选择子集。
  
  异常的处理
  现在让我们来看一些异常的处理(列表D)方法,以对付程序员经常碰到的一些无法预见的状况。正如你会注意到的,列表D使用了嵌套try-catch结构来提供多级别的错误处理。下面列出来的一段代码应该有助于你更好地理解它:
  
    }%>
  现在首先让我们看看里层的“try-catch”结构。这个结构用来处理创建OdbcCommand()或者OdbcDataReader()对象的实例时可能发生的错误。如果数据库里不存在“书籍”表格,就有可能发生这样的错误。如果发生了这样的错误,“catch”部分就会发出一个新的Exception()。里层的“try-catch”结构的“finally”块然后就会在脚本进一步执行之前关闭OdbcConnection()对象。
  
  动作然后就转到外层的“try-catch”结构。外层的结构能够处理两种类型的异常——OdbcException()或者是一般的Exception()。前者在由于某种原因无法打开到数据库服务器的连接或者脚本无法将OdbcConnection()对象实例化的时候产生,而后者用来处理内层“try-catch”结构所产生的异常。无论发生两种情况中的哪一种,都会有一个ASP.NET标签服务器控件向用户显示相应的错误消息。
  
  图B向你显示,当脚本尝试连接到一个不存在的数据库服务器时所发生的事情(这个由外层的“try-catch”结构来处理)。
   用ODBC连接MySQL和ASP.NET_MySQL
  图C向你显示的错误信息会在脚本尝试访问一个不存在的数据库表格时出现(要注意,内层“try-catch”结构产生的异常会由外层结构来处理):
  用ODBC连接MySQL和ASP.NET_MySQL
  以上就是关于如何使用MySQL和ASP.NET的DataGrid服务器控件的介绍。关于你可以如何使用这两种技术还有更多的内容可谈;所以我希望本文和上面提供的其他参考资源能够帮助你对此有个初步的了解。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

See all articles