protected virtual void btnOK_Click(object sender, EventArgs e) { try { srv = new SQLDMO.SQLServer(); //tempo de conexao com o servidor srv.LoginTimeout = 20; if (chbSSIP.Checked) { srv.LoginSecure = true; srv.Connect(".",null,null); } else { srv.Connect(".",tbLogin.Text,tbSenha.Text); } this.ParentForm.Text = srv.TrueLogin + "@" + srv.TrueName; this.ParentForm.Tag = "true"; this.Close(); } catch (Exception) { this.ParentForm.Tag = "false"; } }
protected virtual void btnOK_Click(object sender, EventArgs e) { try { srv = new SQLDMO.SQLServer(); //tempo de conexao com o servidor srv.LoginTimeout = 20; if (chbSSIP.Checked) { srv.LoginSecure = true; srv.Connect(".", null, null); } else { srv.Connect(".", tbLogin.Text, tbSenha.Text); } this.ParentForm.Text = srv.TrueLogin + "@" + srv.TrueName; this.ParentForm.Tag = "true"; this.Close(); } catch (Exception) { this.ParentForm.Tag = "false"; } }
/// <summary> /// 备份数据库 /// </summary> public bool BackupDataBase(string strDbName, string strFileName, ProgressBar proBar) { SQLDMO.SQLServer sqlServer = new SQLDMO.SQLServer(); try { string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["PSAP.Properties.Settings.PSAPConnectionString"].ConnectionString; string tmpStr = "Data Source="; string DataSource = connectionString.Substring(connectionString.IndexOf(tmpStr) + tmpStr.Length); DataSource = DataSource.Substring(0, DataSource.IndexOf(";")); tmpStr = "User ID="; string UserID = connectionString.Substring(connectionString.IndexOf(tmpStr) + tmpStr.Length); if (UserID.IndexOf(";") == -1) { UserID = UserID.Substring(0); } else { UserID = UserID.Substring(0, UserID.IndexOf(";")); } tmpStr = "Password="******";") == -1) { Password = Password.Substring(0); } else { Password = Password.Substring(0, Password.IndexOf(";")); } sqlServer.Connect(DataSource, UserID, Password); SQLDMO.Backup backup = new SQLDMO.Backup(); proBar.Value = 0; backup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database; backup.Initialize = true; backup.PercentComplete += new SQLDMO.BackupSink_PercentCompleteEventHandler(Backup_PercentComplete); backup.Files = strFileName; backup.Database = strDbName; backup.PercentCompleteNotification = 1; backup.SQLBackup(sqlServer); proBar.Value = 100; backup.PercentComplete -= new SQLDMO.BackupSink_PercentCompleteEventHandler(Backup_PercentComplete); backup = null; return(true); } catch (Exception err) { throw (new Exception("备份数据库失败" + err.Message)); } finally { sqlServer.DisConnect(); } }
//获取指定SQL服务器的全部数据库列表 public ArrayList GetDataBaseList(string ServerName, string UserName, string Pwd) { ArrayList list = new ArrayList(); SQLDMO.SQLServer oServer = new SQLDMO.SQLServer(); oServer.Connect(ServerName, UserName, Pwd); foreach (SQLDMO.Database db in oServer.Databases) { if ((db.Name != null) && (db.SystemObject == false)) { list.Add(db.Name); } } return(list); }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { string strSqlSer = Session["SqlServer"].ToString(); string strUid = Session["user"].ToString(); string strPwd = Session["password"].ToString(); txtUser.Text = strUid; txtname.Text = strSqlSer; SqlEXPRESS.Connect(strSqlSer, strUid, strPwd); foreach (SQLDMO.Database dataExample in SqlEXPRESS.Databases) { if (dataExample.Name != null) { databaseList.Items.Add(dataExample.Name.ToString()); } } }
/// <summary> /// Connects this.Connection /// </summary> public void Connect() { Connection.Connect(ServerName, UserName, Password); }
private string GetTablesScript(string dtName) { try { SQLDMO.SQLServer oserver = new SQLDMO.SQLServer(); string connStr = KingTop.Common.SQLHelper.ConnectionStringLocalTransaction; string[] arrConn = connStr.Split(';'); string server = string.Empty; string database = string.Empty; string login = string.Empty; string password = string.Empty; for (int i = 0; i < arrConn.Length; i++) { string[] itemArr = arrConn[i].Split('='); if (itemArr[0].ToLower() == "server") { server = itemArr[1]; } else if (itemArr[0].ToLower() == "database") { database = itemArr[1]; } else if (itemArr[0].ToLower() == "uid") { login = itemArr[1]; } else if (itemArr[0].ToLower() == "pwd") { password = itemArr[1]; } } oserver.Connect(server, login, password); SQLDMO._Database mydb = oserver.Databases.Item(database, "owner"); SQLDMO._Table mytable = mydb.Tables.Item(dtName, "dbo"); string tableScript = mytable.Script(SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScript_Default, null, null, SQLDMO.SQLDMO_SCRIPT2_TYPE.SQLDMOScript2_Default); tableScript = tableScript.Replace("[nvarchar] (0)", "[nvarchar] (max)"); tableScript = tableScript.Replace("[varchar] (-1)", "[varchar] (max)"); //去掉GO int lastPosNum = tableScript.LastIndexOf("GO"); if (lastPosNum > 0) { tableScript = tableScript.Substring(0, lastPosNum); } oserver.DisConnect(); //注释掉不需复制的字段 string[] arrFields = noCopyFields.Split(','); for (int i = 0; i < arrFields.Length; i++) { if (!string.IsNullOrEmpty(arrFields[i]) && tableScript.ToLower().IndexOf("[" + arrFields[i].ToLower() + "]") != -1) { tableScript = Regex.Replace(tableScript, "\\[" + arrFields[i] + "\\]", "--[" + arrFields[i] + "]", RegexOptions.IgnoreCase); } } return(tableScript.Replace("'", "''")); } catch { Response.Write("<div align=center style='padding:20px'>复制失败,原因是sqldmo.dll未注册,注册方法如下:<br><br> 打开开始,在运行中输入 regsvr32 \"C:\\Program Files\\Microsoft SQL Server\\80\\Tools\\Binn\\sqldmo.dll\" 注册sqldmo.dll。<br><br>在注册前请确认sqldmo.dll是否存在,不存在请从网上下载sqldmo.dll到相应目录,再进行注册"); Response.Write("<br><br><a href=# onclick='history.back();'>[返回]</a></div>"); Response.End(); return(""); } }
/// <summary> /// 使用SQL DMO恢复数据库 /// </summary> /// <param name="serverName"></param> /// <param name="userName"></param> /// <param name="pwd"></param> /// <param name="dbName"></param> /// <param name="dbBakFileName"></param> public void RestoreSqlDb(string serverName, string userName, string pwd, string dbName, string dbBakFileName) { string dbFileDir = string.Empty; SQLDMO.SQLServer sqlServerObj = new SQLDMO.SQLServer(); SQLDMO.Database dbObj = new SQLDMO.Database(); SQLDMO.DBFile dbFileObj = new SQLDMO.DBFile(); SQLDMO.LogFile logFileObj = new SQLDMO.LogFile(); try { System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor; sqlServerObj.Connect(serverName, userName, pwd); dbFileDir = sqlServerObj.Registry.SQLDataRoot + @"\DATA\"; if (!Directory.Exists(dbFileDir)) { Directory.CreateDirectory(dbFileDir); } } catch (Exception exp) { throw new Exception("无法连接到Sql Server," + exp.Message, exp); } #region 属性设置 try { dbObj.Name = dbName; dbFileObj.Name = dbBakFileName; dbFileObj.PhysicalName = dbFileDir + dbName + ".mdf"; dbFileObj.PrimaryFile = true; dbFileObj.Size = 3; dbFileObj.FileGrowthType = SQLDMO.SQLDMO_GROWTH_TYPE.SQLDMOGrowth_MB; dbFileObj.FileGrowth = 1; dbObj.FileGroups.Item("PRIMARY").DBFiles.Add(dbFileObj); logFileObj.Name = dbName + "Log"; logFileObj.PhysicalName = dbFileDir + dbName + "_log.ldf"; logFileObj.Size = 3; dbObj.TransactionLog.LogFiles.Add(logFileObj); sqlServerObj.Databases.Add(dbObj); } catch (Exception exp) { throw new Exception("数据库属性设置失败," + exp.Message, exp); } #endregion #region 数据库恢复操作 try { SQLDMO.Restore restoreObj = new SQLDMO.RestoreClass(); restoreObj.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database; restoreObj.Database = dbName; //是否覆盖原数据库 restoreObj.ReplaceDatabase = true; restoreObj.Files = dbBakFileName;// @"\data\mdernode.bak"; //restoreObj.FileNumber = 1; restoreObj.UnloadTapeAfter = true; restoreObj.SQLRestore(sqlServerObj); } catch (Exception exp) { throw new Exception("数据库恢复失败," + exp.Message, exp); } finally { sqlServerObj.DisConnect(); } #endregion }
/// <summary> /// 选择数据库 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DbName_Click(object sender, EventArgs e) { sr = new SQLDMO.SQLServerClass(); if (DbName.Items.Count < 1) { //DbName.Items.Clear(); if (UserName.Text == "" || Password.Text == "") { if (Password.Text == "" && UserName.Text == "") { try { sr.Connect(DbSource.Text.ToString(), null, null); foreach (SQLDMO.Database db in sr.Databases) { if (db.Name != null) DbName.Items.Add(db.Name); } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } else if (Password.Text == "") { try { sr.Connect(DbSource.Text.ToString(), UserName.Text, null); foreach (SQLDMO.Database db in sr.Databases) { if (db.Name != null) DbName.Items.Add(db.Name); } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } } //else if (UserName.Text == "") //{ // MessageBox.Show("请填写用户名,并重新登陆!"); //} else { try { try { sr.Connect(DbSource.Text.ToString(), UserName.Text, Password.Text); } catch { } foreach (SQLDMO.Database db in sr.Databases) { if (db.Name != null) DbName.Items.Add(db.Name); } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } } }