Advertisement
Guest User

Untitled

a guest
May 25th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.SqlClient;
  6. using System.IO;
  7. using MySql.Data.MySqlClient;
  8.  
  9. using System.Data;
  10.  
  11. namespace ConsoleApplication1
  12. {
  13.     class IrcChannel{
  14.         List<StalkWord> StalkWords{get;set;};
  15.         string ChannelName{get;set;};
  16.        
  17.         public override string ToString(){
  18.             string returnString = "";
  19.             returnString+=ChannelName;
  20.             for(int i = 0; i < StalkWords.Count; i++){
  21.                if(i==0)returnString+="{";
  22.                returnString+=StalkWords[i];
  23.                if(i==0)returnString+="}";
  24.             }
  25.         }
  26.     }
  27.    
  28.     class StalkWord{
  29.         IrcChannel ircChannel;
  30.         string Word{get;set;};
  31.     }
  32.    
  33.     class Program
  34.     {
  35.         public static string SQLuser;
  36.         public static string SQLpassword;
  37.         public static List<IrcChannel> IrcChannels = new List<IrcChannel>();
  38.        
  39.         static void Main(string[] args)
  40.         {
  41.             readconfigfile();
  42.             string connectionString = "Server=sql;" +
  43.                 "Database=u_chzz_stalkbot;" +
  44.                 "User ID=" + SQLuser + ";" +
  45.                 "Password=" + SQLpassword + ";" +
  46.                 "Persist Security Info=True;";
  47.  
  48.             MySqlConnection conn = new MySqlConnection(connectionString);
  49.             conn.Open();
  50.  
  51.             string SQLStatement = "SELECT * FROM channel";
  52.             MySqlDataAdapter ChanAdapter = new MySqlDataAdapter(SQLStatement, conn);
  53.             DataTable dtResult = new DataTable();
  54.             DataTable dtstalk = new DataTable();
  55.  
  56.             // Fill the DataTable with the result of the SQL statement
  57.             ChanAdapter.Fill(dtResult);
  58.  
  59.             // Loop through all entries
  60.            
  61.             foreach (DataRow drRow in dtResult.Rows)
  62.             {
  63.                 IrcChannel myChannel = new IrcChannel();
  64.                 myChannel.ChannelName = drRow["Name"].ToString();
  65.                 Console.WriteLine(myChannel.ChannelName);
  66.                 //now, for each channel, populate array of stalks
  67.                 SQLStatement = "SELECT text FROM chanstalk WHERE channel = '"+myChannel.ChannelName+"'";
  68.                 Console.WriteLine(SQLStatement);
  69.                 MySqlDataAdapter stalkAdapter = new MySqlDataAdapter(SQLStatement, conn);
  70.                 DataTable stResult = new DataTable();
  71.                 stalkAdapter.Fill(stResult);
  72.                 dtstalk.Rows.Add(stResult);
  73.                 foreach (DataRow stRow in stResult.Rows)
  74.                 {
  75.                     StalkWord myStalkWord = new StalkWord();
  76.                     myStalkWord.Word = stRow["text"].ToString();
  77.                     Console.WriteLine("****"+myStalkWord.Word );
  78.                     myChannel.StalkWords.Add(myStalkWord);
  79.                 }
  80.                 stalkAdapter.Dispose();
  81.                 IrcChannels.Add(myIrcChannel);
  82.             }
  83.             // We don't need the data adapter any more
  84.             ChanAdapter.Dispose();
  85.             conn.Close();
  86.             conn.Dispose();
  87.             Console.WriteLine("XXXXXXX");
  88.          foreach (IrcChannel myChan in IrcChannels)
  89.          {
  90.              Console.WriteLine(myChan);
  91.          }
  92.  
  93.  
  94.         }
  95.                 static void readconfigfile()
  96.         {
  97.             Console.WriteLine("reading .my.cnf");
  98.             StreamReader settingsreader = new StreamReader("/home/chzz/.my.cnf");
  99.             try {
  100.                settingsreader.ReadLine();
  101.                SQLuser = settingsreader.ReadLine();
  102.                SQLuser = SQLuser.Remove(0,SQLuser.IndexOf("=")+2);
  103.                SQLpassword = settingsreader.ReadLine();
  104.                SQLpassword = SQLpassword.Remove(0, SQLpassword.IndexOf("=") + 2);
  105.               // SQLpassword = SQLpassword.Replace("\"","");
  106.             }
  107.             catch (Exception ex) {
  108.                 Console.WriteLine("Could not read .my.cnf for SQL user/password");
  109.                 Console.WriteLine(ex.Message);
  110.             }
  111.        
  112.         }
  113.  
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement