破碎了无痕

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

长期以来一直使用的是微软的SQL Server, 从当年的2000到如今的2008, 一直听说MySQL比较火,免费,MySQL + PHP建站乃黄金组合,最近花了点时间研究了一下MySQL数据库,这里记录一点心得。

首先是安装了,在windows环境下,直接到MySQL官网下载 MySQL Installer 一次性从数据库,到可视化工具,再到asp.net驱动全搞定!

它自带的可视化工具是Oracle出的 MySQL WorkBench,界面比较简单,但跟用习惯了的 SQL ServerManagement Studio相比,还是有很大的不同,这里,数据库不叫Database而叫Schema, 存储过程不叫Store Procedure而叫Routine,不过总体还是比较简单的。

 

下一步就是尝试各种语法了,总体跟SQL Server差别不大,值得注意的是,跟SQLite一样,MySQL的语句也是以分号;结束的,另外执行存储过程用call关键字,且参数必须用括号括起来,比较像程序中方法的调用。

例如: call GetCustomerById (2)

 

 然后是Code操作数据库,这个也比较简单,主要就是引用MySQL.Data程序集,这个在上面安装MySQL Installer时已经安装好了,可以通过程序添加引用从.NET下找到,也可以去安装目录把这个dll拷到程序里面用起来更方便,其位置在MySQL安装目录下的 \MySQL\Connector NET 6.5.4\Assemblies\下。

 

在代码里引入命名空间MySql.Data.MySqlClient,调用比较简单,参考以下code:


View Code
using System;
using System.Data;
using MySql.Data.MySqlClient;

namespace SQLiteConsoleApp
{
    // For MySQL
    class Program_ForMySql
    {
        static void Main(string[] args)
        {
            MySqlConnection conn = new MySqlConnection("server=localhost;user id=root;password=abcd1234**##;database=chytest");
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM Customer", conn);
            MySqlCommand cmd2 = new MySqlCommand(@"insert into Customer (UserName, PassWord, Gender, Address, Telephone) values 
                ('Tom', '111', 'M', 'ShangHai', '4558556')
", conn);
            
            conn.Open();

            // insert new data
            
//Console.WriteLine("------------insert new data---------------------------");
            
//cmd2.ExecuteNonQuery();

            
// use reader to read the data
            Console.WriteLine("");
            Console.WriteLine("------------use reader to read the data---------------------------");
            var reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                if (reader.HasRows)
                {
                    Console.WriteLine(reader["CustomerId"].ToString() + "~" + 
                                            reader["UserName"].ToString() + "~" +
                                            reader["PassWord"].ToString() + "~" +
                                            reader["Gender"].ToString() + "~" +
                                            reader["Address"].ToString() + "~" +
                                            reader["Telephone"].ToString());
                }
            }
            reader.Close();

            // use adapter to read data into datatable
            Console.WriteLine("");
            Console.WriteLine("------------use adapter to read data into datatable----------------------------");

            MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
            DataTable dt = new DataTable("tbl1");
            adapter.Fill(dt);

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    Console.WriteLine(row[0].ToString() + "^" + row[1].ToString());
                }
            }

            // Call store procedure
            Console.WriteLine("");
            Console.WriteLine("----------------Call Store Procedure-------------------------------------");

            MySqlCommand cmd3 = new MySqlCommand("GetCustomerById", conn);
            cmd3.CommandType = CommandType.StoredProcedure;
            MySqlParameter mparam = new MySqlParameter("@custId", MySqlDbType.Int32);   //here ?custId and custId also works
            mparam.Value = 2;
            mparam.Direction = ParameterDirection.Input;
            cmd3.Parameters.Add(mparam);
            var reader2 = cmd3.ExecuteReader();
            while (reader2.Read())
            {
                if (reader2.HasRows)
                {
                    Console.WriteLine(reader2["CustomerId"].ToString() + "~" +
                                            reader2["UserName"].ToString() + "~" +
                                            reader2["PassWord"].ToString() + "~" +
                                            reader2["Gender"].ToString() + "~" +
                                            reader2["Address"].ToString() + "~" +
                                            reader2["Telephone"].ToString());
                }
            }
            reader.Close();


            conn.Close();

            Console.ReadLine();

        }
    }
}
 

 有一点值得注意,在调用存储过程时,对于参数名, SQL Server是以@符号打头, MySQL除了支持@符号外,还支持问号?,不用任何符号打头也可以。

posted on 2012-06-01 17:34  破碎了无痕  阅读(293)  评论(0编辑  收藏  举报