Example #1
0
        public static SqlConnection GetConn(string dbname)
        {
            //SqlConnection con = new SqlConnection();
            try
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }

                if (con.State == ConnectionState.Closed)
                {
                    dboperation db           = new dboperation();
                    string      connecstring = db.GetServer_Instance();
                    con.ConnectionString = connecstring; // GetServer_Instance();
                    con.Open();
                    Properties.Settings.Default.Connstring = connecstring;
                    Properties.Settings.Default.Save();
                    // db.TableUpdate();
                }
                return(con);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return(con);
            }
        }
Example #2
0
        public static bool DbRestore(System.Windows.Forms.OpenFileDialog backup, String databaseName)
        {
            try
            {
                dboperation db = new dboperation();
                dboperation.GetConn("master");
                ServerConnection conn = new ServerConnection();
                conn.ServerInstance = ServerInstance();
                Server  srv = new Server(conn);
                Restore res = new Restore();
                res.Devices.AddDevice(backup.FileName, DeviceType.File);
                RelocateFile DataFile = new RelocateFile();
                string       MDF      = res.ReadFileList(srv).Rows[0][1].ToString();
                DataFile.LogicalFileName  = res.ReadFileList(srv).Rows[0][0].ToString();
                DataFile.PhysicalFileName = srv.Databases[databaseName].FileGroups[0].Files[0].FileName;

                RelocateFile LogFile = new RelocateFile();
                string       LDF     = res.ReadFileList(srv).Rows[1][1].ToString();
                LogFile.LogicalFileName  = res.ReadFileList(srv).Rows[1][0].ToString();
                LogFile.PhysicalFileName = srv.Databases[databaseName].LogFiles[0].FileName;

                res.RelocateFiles.Add(DataFile);
                res.RelocateFiles.Add(LogFile);

                res.Database        = databaseName;
                res.NoRecovery      = true;
                res.ReplaceDatabase = true;
                res.SqlRestore(srv);
                conn.Disconnect();
                return(true);
            }
            catch (Exception)
            {
            }
            return(false);
        }
Example #3
0
        public static string  ServerInstance()
        {
            dboperation db         = new dboperation();
            string      serverName = null;
            string      ConnStr    = null;
            RegistryKey rk         = null;

            if (clsproperties.IPAddress != null)
            {
                //rk =RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine,clsproperties.IPAddress).OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server");
                if (clsproperties.IPAddress != null && clsproperties.CompanyName == null)
                {
                    return("Data Source=" + clsproperties.IPAddress + ";Initial Catalog=master;User ID=sa;Password=12345;MultipleActiveResultSets=true");
                }
                else if (clsproperties.IPAddress != null && clsproperties.CompanyName != null)
                {
                    return("Data Source=" + clsproperties.IPAddress + ";Initial Catalog=" + clsproperties.CompanyName + ";User ID=sa;Password=12345;MultipleActiveResultSets=true");
                }
            }
            else
            {
                rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server");
            }

            // RegistryKey rk = localMachine.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server");
            String[] instances = (String[])rk.GetValue("InstalledInstances");
            int      i         = 0;

            if (instances.Length > 0)
            {
                while (ConnStr == null)
                {
                    foreach (String element in instances)
                    {
                        if (i == 0)
                        {
                            if (element == "MSSQLSERVER")
                            {
                                if (clsproperties.CompanyName == null)
                                {
                                    ConnStr = "Data Source=" + Environment.MachineName + ";Integrated Security=True;";
                                    if (db.MastDBCheck(ConnStr))
                                    {
                                        return(Environment.MachineName);
                                    }
                                }
                                else
                                {
                                    if (db.CheckDB(Environment.MachineName))
                                    {
                                        // ConnStr = "Data Source=" + Environment.MachineName + ";Initial Catalog=" + clsproperties.CompanyName + ";Integrated Security=True";
                                        return(Environment.MachineName);
                                    }
                                }
                            }
                            else
                            {
                                serverName = Environment.MachineName + @"\" + element;
                                if (clsproperties.CompanyName == null)
                                {
                                    ConnStr = "Data Source=" + serverName + ";Integrated Security=True";
                                    if (db.MastDBCheck(ConnStr))
                                    {
                                        return(serverName);
                                    }
                                }
                                else
                                {
                                    if (db.CheckDB(serverName))
                                    {
                                        //ConnStr = "Data Source=" + serverName + ";Initial Catalog=" + clsproperties.CompanyName + ";Integrated Security=True";
                                        return(serverName);
                                    }
                                }
                            }
                        }
                        else
                        {
                        }
                    }
                    i++;
                }
            }
            return(serverName);
        }
Example #4
0
 public clssalesinvoice()
 {
     dboperation db = new dboperation();
 }