http://write.blog.csdn.net/postedit
[代码] c#代码
001 | using System; |
002 | using System.Collections.Generic; |
003 | using System.Text; |
004 | using System.Windows.Forms; |
005 | using System.Data; |
006 | using System.Text.RegularExpressions; |
007 | /// |
008 | using MySql.Data.MySqlClient; |
009 |
010 |
011 | namespace NameSpace |
012 | { |
013 | class MysqlConnection |
014 | { |
015 | MySqlConnection mysqlConnection; |
016 | DataSet dataSet; |
017 | string IP = null ; |
018 | string UserName = "root" ; |
019 | string Password = "root" ; |
020 | string Database = null ; |
021 |
022 | public MysqlConnection() |
023 | { |
024 | try |
025 | { |
026 | mysqlConnection = new MySqlConnection( "datasource=20.0.0.20;username=root;password=root;database=sysinfo;charset=gb2312" ); |
027 | } |
028 | catch (MySqlException ex) |
029 | { |
030 | MessageBox.Show(ex.Message); |
031 | } |
032 | } |
033 |
034 | public MysqlConnection( string IP, string UserName, string Password, string Database) |
035 | { |
036 | try |
037 | { |
038 | string connectionString = "datasource=" + IP + ";username=" +UserName+ ";password=" +Password+ ";database=" + Database+ ";charset=gb2312" ; |
039 | mysqlConnection = new MySqlConnection(connectionString); |
040 | } |
041 | catch (MySqlException ex) |
042 | { |
043 | MessageBox.Show(ex.Message); |
044 | } |
045 | } |
046 |
047 | public string MysqlInfo() |
048 | { |
049 | string mysqlInfo = null ; |
050 | try |
051 | { |
052 | mysqlConnection.Open(); |
053 | mysqlInfo += "Connection Opened." + Environment.NewLine; |
054 | mysqlInfo += "Connection String:" + mysqlConnection.ConnectionString.ToString() + Environment.NewLine; |
055 | mysqlInfo += "Database:" + mysqlConnection.Database.ToString() + Environment.NewLine; |
056 | mysqlInfo += "Connection ServerVersion:" + mysqlConnection.ServerVersion.ToString() + Environment.NewLine; |
057 | mysqlInfo += "Connection State:" + mysqlConnection.State.ToString() + Environment.NewLine; |
058 | } |
059 | catch (MySqlException ex) |
060 | { |
061 | Console.WriteLine( "MySqlException Error:" + ex.ToString()); |
062 | } |
063 | finally |
064 | { |
065 | mysqlConnection.Close(); |
066 | } |
067 | return mysqlInfo; |
068 | } |
069 |
070 | public int MysqlCommand( string MysqlCommand) |
071 | { |
072 | try |
073 | { |
074 | mysqlConnection.Open(); |
075 | Console.WriteLine( "MysqlConnection Opened." ); |
076 | MySqlCommand mysqlCommand = new MySqlCommand(MysqlCommand, mysqlConnection); |
077 | return mysqlCommand.ExecuteNonQuery(); |
078 | } |
079 | catch (MySqlException ex) |
080 | { |
081 | Console.WriteLine( "MySqlException Error:" + ex.ToString()); |
082 | if (Regex.IsMatch(ex.ToString(), "" )) |
083 | { |
084 | MessageBox.Show( "数据库已经存在唯一键值" ); |
085 | } |
086 | } |
087 | finally |
088 | { |
089 | mysqlConnection.Close(); |
090 | } |
091 | return -1; |
092 | } |
093 |
094 | // |
095 | public DataView MysqlDataAdapter( string table) |
096 | { |
097 | DataView dataView = new DataView(); |
098 | try |
099 | { |
100 | mysqlConnection.Open(); |
101 | MySqlDataAdapter mysqlDataAdapter = new MySqlDataAdapter( "Select * from " + table, mysqlConnection); |
102 | dataSet = new DataSet(); |
103 | mysqlDataAdapter.Fill(dataSet, table); |
104 | dataView = dataSet.Tables[table].DefaultView; |
105 | } |
106 | catch (MySqlException ex) |
107 | { |
108 | MessageBox.Show(ex.Message); |
109 | } |
110 | finally |
111 | { |
112 | mysqlConnection.Close(); |
113 | } |
114 | return dataView; |
115 | } |
116 | } //end class |
117 | } |