public static DataContext CreateDatabaseModel(String databaseName) { string namedConnectionString = connectionString.Replace("(*)", databaseName); string winDirDrive = ""; dbDataContext = null; try { Assembly assembly = Assembly.GetExecutingAssembly(); using (Stream sqlMetalResourceStream = assembly.GetManifestResourceStream("DatabaseLoader.SqlMetal.exe")) { if (sqlMetalResourceStream != null) { byte[] byteArray = new byte[sqlMetalResourceStream.Length]; sqlMetalResourceStream.Read(byteArray, 0, byteArray.Length); winDirDrive = Path.GetPathRoot(Environment.SystemDirectory); extractedLocationOfExe = Path.GetTempPath() + "SqlMetal.exe"; if (!File.Exists(extractedLocationOfExe)) { using (FileStream sqlMetalExeStream = new FileStream(extractedLocationOfExe, FileMode.Append)) { sqlMetalExeStream.Write(byteArray, 0, byteArray.Length); } } locationOfDbml = Path.GetTempPath() + "Dynamic" + databaseName + ".dbml"; string parameters = " /server:localhost" + " /database:" + databaseName + " /dbml:\"" + locationOfDbml + "\"" + " /pluralize"; using (Process sqlMetalProcess = Process.Start(extractedLocationOfExe, parameters)) { sqlMetalProcess.WaitForExit(); } if (File.Exists(locationOfDbml)) { locationOfMap = Path.GetTempPath() + databaseName + "MappingSource.map"; locationOfDataContext = Path.GetTempPath() + "Dynamic" + databaseName + "DataContext.cs"; string parameters2 = " /map:\"" + locationOfMap + "\"" + " \"" + locationOfDbml + "\"" + " /code:\"" + locationOfDataContext + "\""; using (Process sqlMetalProcess = Process.Start(extractedLocationOfExe, parameters2)) { sqlMetalProcess.WaitForExit(); } if (locationOfMap != "") { MappingSource mappingSource = XmlMappingSource.FromReader(XmlReader.Create(locationOfMap)); if (mappingSource != null) { dbDataContext = new DataContext(namedConnectionString, mappingSource); } else { dbDataContext = new DataContext(namedConnectionString); } } else { dbDataContext = new DataContext(namedConnectionString); } } } } } catch { //MessageBox.Show("An Error Occurred"); } return(dbDataContext); }