Exemple #1
0
 private void btnLoad_Click(object sender, EventArgs e)
 {
     using (StreamReader sr = new StreamReader(txtPath.Text, Encoding.UTF8))
     {
         string line = sr.ReadLine();
         while (line != null)
         {
             if (line.IndexOf("CREATE TABLE") > -1)
             {
                 Tbl table = new Tbl();
                 table.name = line.Split('`')[1];
                 line       = sr.ReadLine();
                 while (line != null && line[0] != ')')
                 {
                     if (line.IndexOf("PRIMARY KEY") > -1)
                     {
                         table.pk = line.Split('`')[1];
                     }
                     else if (line.IndexOf("KEY") > -1)
                     {
                         table.keys.Add(line.Split('(')[1].Split(')')[0]);
                     }
                     else
                     {
                         table.fields.Add(line.Trim().Split(' ')[0].Replace("`", ""));
                         if (line.Trim().Split(' ')[1].IndexOf("(") > -1)
                         {
                             table.fields_type.Add(line.Trim().Split(' ')[1].Substring(0, line.Trim().Split(' ')[1].IndexOf("(")));
                             table.fields_size.Add(line.Trim().Split(' ')[1].Substring(line.Trim().Split(' ')[1].IndexOf("(")));
                         }
                         else
                         {
                             table.fields_type.Add(line.Trim().Split(' ')[1]);
                             table.fields_size.Add("");
                         }
                         if (line.IndexOf("NOT NULL") > -1)
                         {
                             table.fields_null.Add("NOT NULL");
                         }
                         else
                         {
                             table.fields_null.Add("NULL");
                         }
                         if (line.IndexOf("unsigned") > -1)
                         {
                             table.fields_sign.Add("unsigned");
                         }
                         else
                         {
                             table.fields_sign.Add("");
                         }
                         if (line.IndexOf("auto_increment") > -1)
                         {
                             table.fields_increase.Add("auto_increment");
                         }
                         else
                         {
                             table.fields_increase.Add("");
                         }
                         if (line.IndexOf("default") > -1)
                         {
                             table.fields_default.Add(line.Substring(line.IndexOf("default") + 8));
                         }
                         else
                         {
                             table.fields_default.Add("");
                         }
                     }
                     line = sr.ReadLine();
                 }
                 if (line[0] == ')')
                 {
                     table.charset = line.Substring(line.IndexOf("CHARSET=") + 8).Split(';')[0];
                     table.enqine  = line.Substring(line.IndexOf("ENGINE=") + 7).Split(' ')[0];
                 }
                 tables.Add(table);
             }
             line = sr.ReadLine();
         }
     }
     setData();
 }
Exemple #2
0
 private void readTbl(string filename)
 {
     using (StreamReader sr = new StreamReader(filename, Encoding.UTF8))
     {
         string line = sr.ReadLine();
         while (line != null)
         {
             if (line.IndexOf("CREATE TABLE") > -1)
             {
                 Tbl table = new Tbl();
                 table.name = line.Split('`')[1];
                 line       = sr.ReadLine();
                 while (line != null && line[0] != ')')
                 {
                     if (line.IndexOf("PRIMARY KEY") > -1)
                     {
                         table.pk = line.Split('`')[1];
                     }
                     else if (line.IndexOf("KEY") > -1)
                     {
                         table.keys.Add(line.Substring(line.IndexOf("KEY") + 4));
                     }
                     else
                     {
                         string[] temp = line.Trim().Split(' ');
                         if (temp.Length > 1)
                         {
                             table.fields.Add(temp[0].Replace("`", ""));
                             if (temp[1].IndexOf("(") > -1)
                             {
                                 table.fields_type.Add(temp[1].Substring(0, temp[1].IndexOf("(")));
                                 table.fields_size.Add(temp[1].Substring(temp[1].IndexOf("(") + 1).Replace(")", ""));
                             }
                             else
                             {
                                 table.fields_type.Add(temp[1]);
                                 table.fields_size.Add("");
                             }
                             if (line.IndexOf("unsigned") > -1)
                             {
                                 table.fields_sign.Add("unsigned");
                             }
                             else
                             {
                                 table.fields_sign.Add("");
                             }
                             if (line.IndexOf("NOT NULL") > -1)
                             {
                                 table.fields_null.Add("NOT NULL");
                             }
                             else
                             {
                                 table.fields_null.Add("NULL");
                             }
                             if (line.IndexOf("auto_increment") > -1)
                             {
                                 table.fields_increase.Add("auto_increment");
                             }
                             else
                             {
                                 table.fields_increase.Add("");
                             }
                             if (line.IndexOf("default") > -1)
                             {
                                 table.fields_default.Add(line.Substring(line.IndexOf("default") + 8).Replace(";", ""));
                             }
                             else
                             {
                                 table.fields_default.Add("");
                             }
                         }
                     }
                     line = sr.ReadLine();
                 }
                 if (line[0] == ')')
                 {
                     if (line.IndexOf("ENGINE=") > -1)
                     {
                         table.enqine = line.Substring(line.IndexOf("ENGINE=") + 7).Split(' ')[0];
                     }
                     if (line.IndexOf("CHARSET=") > -1)
                     {
                         table.charset = line.Substring(line.IndexOf("CHARSET=") + 8).Replace(";", "");
                     }
                 }
                 tables.Add(table);
             }
             line = sr.ReadLine();
         }
     }
 }