Conn--mySql


using System;
using System.Configuration;
using MySql.Data.MySqlClient;
/// <summary>
/// TestDatebase 的摘要说明
/// </summary>
public class TestDatebase
{
    public TestDatebase()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    public static void Main(String[] args)
    {
        MySqlConnection mysql = getMySqlCon();
        //查询sql
        String sqlSearch = "select * from student";
        //插入sql
        String sqlInsert = "insert into student values (12,'张三',25,'大专')";
        //修改sql
        String sqlUpdate = "update student set name='李四' where id= 3";
        //删除sql
        String sqlDel = "delete from student where id = 12";
        //打印SQL语句
        Console.WriteLine(sqlDel);
        //四种语句对象
        //MySqlCommand mySqlCommand = getSqlCommand(sqlSearch, mysql);
        //MySqlCommand mySqlCommand = getSqlCommand(sqlInsert, mysql);
        //MySqlCommand mySqlCommand = getSqlCommand(sqlUpdate, mysql);
        MySqlCommand mySqlCommand = getSqlCommand(sqlDel, mysql);
        mysql.Open();
        //getResultset(mySqlCommand);
        //getInsert(mySqlCommand);
        //getUpdate(mySqlCommand);
        getDel(mySqlCommand);
        //记得关闭
        mysql.Close();
       String readLine = Console.ReadLine();
    }
    /// <summary>
    /// 建立mysql数据库链接
    /// </summary>
    /// <returns></returns>
    public static MySqlConnection getMySqlCon()
    {
        String mysqlStr = "Database=test;Data Source=127.0.0.1;User Id=root;Password=root;pooling=false;CharSet=utf8;port=3306";
        // String mySqlCon = ConfigurationManager.ConnectionStrings["MySqlCon"].ConnectionString;
        MySqlConnection mysql = new MySqlConnection(mysqlStr);
        return mysql;
    }
    /// <summary>
    /// 建立执行命令语句对象
    /// </summary>
    /// <param name="sql"></param>
    /// <param name="mysql"></param>
    /// <returns></returns>
    public static MySqlCommand getSqlCommand(String sql,MySqlConnection mysql)
    {
        MySqlCommand mySqlCommand = new MySqlCommand(sql, mysql);
        //  MySqlCommand mySqlCommand = new MySqlCommand(sql);
        // mySqlCommand.Connection = mysql;
        return mySqlCommand;
    }
    /// <summary>
    /// 查询并获得结果集并遍历
    /// </summary>
    /// <param name="mySqlCommand"></param>
    public static void getResultset(MySqlCommand mySqlCommand)
    {
        MySqlDataReader reader = mySqlCommand.ExecuteReader();
        try
        {
            while (reader.Read())
            {
                if (reader.HasRows)
                {
                    Console.WriteLine("编号:" + reader.GetInt32(0) + "|姓名:" + reader.GetString(1) + "|年龄:" + reader.GetInt32(2) + "|学历:" + reader.GetString(3));
                }
            }
        }
        catch (Exception)
        {

            Console.WriteLine("查询失败了!");
        }
        finally
        {
            reader.Close();
        }
    }
    /// <summary>
    /// 添加数据
    /// </summary>
    /// <param name="mySqlCommand"></param>
    public static void getInsert(MySqlCommand mySqlCommand)
    {
        try
        {
            mySqlCommand.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            String message = ex.Message;
            Console.WriteLine("插入数据失败了!" + message);
        }
      
    }
    /// <summary>
    /// 修改数据
    /// </summary>
    /// <param name="mySqlCommand"></param>
    public static void getUpdate(MySqlCommand mySqlCommand)
    {
        try
        {
            mySqlCommand.ExecuteNonQuery();
        }
        catch (Exception ex)
        {

            String message = ex.Message;
            Console.WriteLine("修改数据失败了!" + message);
        }
    }
    /// <summary>
    /// 删除数据
    /// </summary>
    /// <param name="mySqlCommand"></param>
    public static void getDel(MySqlCommand mySqlCommand)
    {
        try
        {
            mySqlCommand.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            String message = ex.Message;
            Console.WriteLine("删除数据失败了!" + message);
        }
    }
}






朋友下了个程序让我把数据库改为mysql,以前是连接sql server的。查了下资料,花了不少时间终于搞定了。把过程记录如下:

首先在c#里面是没有原生态支持mysql的,谷歌了下,找到了MySQLDriverCS-n-EasyQueryTools-4.0.1-DotNet2.0这个类库,下载下来安装下得到如下几个dll:

 

 

 

 

 

 

 

 

接着在vs里面添加引用

下面就是开始写连接字符串了查了下这个dll的文档

得到MySqlConnection写为:MySqlConnection conn=new MySqlConnection(string cs);其中cs表示连接字符串可以用new MySqlConnectionString("服务器地址","数据库","登录名","密码").AsString;得到,接下来的操作基本就和SqlConnection一致了.

按照上面的配置我开始从数据库检索数据了,写了个测试的方法,结果居然连不上报错了!于是想到是没有为mysql配置远程登录权限,依旧谷歌查到一篇文章参见:

http://dev.firnow.com/course/7_databases/mysql/Mysqljs/2008324/107030.html

在数据库mysql里面的user表执行如下sql语句:

1 grant all privileges on *.* to "创建的用户名 @"%" identified by "密码";
结果搞定了,贴个连接上的图(数据库中的表被朋友删掉故查询不到)


 








 

posted on 2012-03-07 16:16 青蛙學堂 阅读(334) 评论(0)  编辑 收藏 引用 所属分类: 數據庫軟件布袋

只有注册用户登录后才能发表评论。
<2012年5月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

常用链接

留言簿(8)

随笔分类

随笔档案

收藏夹

青蛙学堂

最新评论

阅读排行榜

评论排行榜