Wednesday, April 15, 2009

Automatic Typed DataSet Generator

   1: public void CreateTypedSet(string database)
   2: {
   3:     DataSet ds = new DataSet();
   4:     ds.DataSetName = "DS" + database;
   5:     this.FillShema(ds, database);
   6:     ds.WriteXmlSchema("DS" + database + ".xsd");
   7:     System.Reflection.Assembly asm = System.Reflection.Assembly.GetEntryAssembly();
   8:     DirectoryInfo d = new DirectoryInfo(System.Environment.CurrentDirectory);
   9:     try
  10:     {
  11:         File.Copy(d + "\\" + "DS" + database + ".xsd", d.Parent.Parent.FullName + "\\" + "DS" + database + ".xsd",true);
  12:     }
  13:     catch
  14:     {
  15:     }
  16:  
  17:     string sf = asm.FullName;
  18:     int ind = sf.IndexOf(',');
  19:     string mh = sf.Remove(ind);
  20:     StreamReader sr = new StreamReader(d.Parent.Parent.FullName + "\\" + mh + ".csproj");
  21:     string s = sr.ReadToEnd();
  22:     int index = s.IndexOf("</None>");
  23:     index += 7;
  24:     sr.Close();
  25:     string m = s.Insert(index, "\n \n<None Include=\"" + "DS" + database + ".xsd\"" + ">\n<Generator>MSDataSetGenerator</Generator>\n<LastGenOutput>" + "DS" + database + ".Designer.cs</LastGenOutput>\n <SubType>Designer</SubType>\n</None>\n");
  26:     StreamWriter SW = new StreamWriter(d.Parent.Parent.FullName + "\\" + mh + ".csproj");
  27:     SW.Write(m);
  28:     SW.Close();
  29: }
   1: public void FillShema(DataSet ds, string database)
   2: {
   3:     NpgsqlDataAdapter na = new NpgsqlDataAdapter("Select * from \"" + database + "\";", oConn);
   4:     na.FillSchema(ds, SchemaType.Source,database);
   5: }

 

I can hear you say what the..? Let me tell you what the.. This code  creates  your typed dataset and adds it to the solution.Why would someone do something like that? I could not use server explorer for postgredb  and had lots of dbs that change continously so i wrote something like this.I know not the best way but i will not spend days just to see databases with server explorer(spent nearly one day for that if anyone knows how please send me an email :)

No comments: