查找SQL服务器名称&数据库名称 [英] Find SQL server name & database name
本文介绍了查找SQL服务器名称&数据库名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hi
i需要备份一个sql文件还原以及如何查找SQL服务器名称和数据库名称我使用sqlexpress。
请帮帮我
hi
i need backup a sql file an restore and how to find SQL server name and database name i use sqlexpress.
please help me
推荐答案
我使用此代码检索我的SQL Server的名称,它在我的电脑上使用Visual Studio 2012和SQLExpress 2012正常工作。
I used this code to retrieve the name of my SQL Server and it worked fine on my PC with Visual Studio 2012 and SQLExpress 2012.
public void Main()
{
// Retrieve the enumerator instance and then the data.
SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
System.Data.DataTable table = instance.GetDataSources();
//
// Display the contents of the table.
DisplayData(table);
}
private void DisplayData(DataTable table)
{
foreach (DataRow row in table.Rows)
{
//
//
Console.WriteLine(@"\\{0}", row[0]); // Output: \\SQLServerName
//
//
// Show all columns in the datarow to see what else is in the row
foreach (DataColumn col in table.Columns)
{
Console.WriteLine("{0} = {1}", col.ColumnName, row[col]);
}
Console.WriteLine("============================");
}
}
我在BAT文件中使用它来执行我的SQLExpress数据库的计划备份:
在下面的文件中:
*将 mycomputer 更改为您的计算机名称。
*将 mydatabase 更改为您的数据库名称。
mydatabase_Backup.bat的内容:
I use this in a BAT file to do a scheduled backup of my SQLExpress database:
In the files below:
* Change mycomputer to your computer name.
* Change mydatabase to your database name.
Contents of mydatabase_Backup.bat:
C:
cd C:\Program Files\Microsoft SQL Server\110\Tools\Binn
sqlcmd -E -S mycomputer\SQLEXPRESS -d mydatabase -i c:\bat\mydatabase_Backup.txt -o C:\Bat\mydatabase_Backup.log -y 132
C:\ batt \ _mydatabase_Backup.txt的内容:
BACKUP DATABASE [mydatabase] TO
DISK = N''E:\SQLBackups\mydatabase.bak''
WITH FORMAT, INIT,
MEDIANAME = N''mydatabase'',
NAME = N''mydatabase-Full Database Backup'',
SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
declare @backupSetId as int
select @backupSetId = position from msdb..backupset where database_name=N''mydatabase'' and backup_set_id=(select max(backup_set_id) from msdb..backupset where database_name=N''mydatabase'' )
if @backupSetId is null
begin
raiserror(N''Verify failed. Backup information for database ''''mydatabase'''' not found.'', 16, 1)
end
RESTORE VERIFYONLY FROM
DISK = N''E:\SQLBackups\mydatabase.bak'' WITH FILE = @backupSetId, NOUNLOAD, NOREWIND
GO
我使用此代码查找但没有结果
i use this code to find but without result
string myServer = Environment.MachineName;
DataTable servers = SqlDataSourceEnumerator.Instance.GetDataSources();
for (int i = 0; i < servers.Rows.Count; i++)
{
if (myServer == servers.Rows[i]["ServerName"].ToString())
{
if ((servers.Rows[i]["InstanceName"] as string) != null)
{
//comboBox2.Visibility = Visibility.Visible;
listBox1.Items.Add(servers.Rows[i]["ServerName"] + "\\" + servers.Rows[i]["InstanceName"]);
}
else
{
//comboBox2.Visible = false;
listBox1.Items.Add(servers.Rows[i]["ServerName"]);
}
}
}
这篇关于查找SQL服务器名称&数据库名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文