我需要使用当前连接端口生成以下格式的结果。有人能帮助我吗?
servername\instance,3000
由于
答案 0 :(得分:0)
以下显示您可以作为c#控制台应用程序运行的代码,以获得您想要的内容。如果您已经在SQL Management Studio中,那么您只需要直接执行命令:
static void Main(string[] args)
{
using (var conn = new SqlConnection("Integrated Security=true; Initial Catalog=dbname;Data Source=servername"))
{
using (var cmd = new SqlCommand("SELECT SERVERPROPERTY('ServerName'), SERVERPROPERTY('InstanceName')", conn))
{
string serverName = "";
string instanceName = "";
cmd.CommandType = System.Data.CommandType.Text;
conn.Open();
var rdr = cmd.ExecuteReader();
if (rdr.Read())
{
serverName = rdr.GetString(0);
if (!rdr.IsDBNull(1))
{
instanceName = rdr.GetString(1);
}
}
rdr.Close();
Console.WriteLine("Server name of this connection is: " + serverName + ". Instance of this connection is: " + instanceName);
}
using (var cmd = new SqlCommand("xp_readerrorlog", conn))
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
var param = cmd.CreateParameter();
param.ParameterName = "@ArchiveID";
param.DbType = System.Data.DbType.Int32;
param.Value = 0;
cmd.Parameters.Add(param);
param = cmd.CreateParameter();
param.ParameterName = "@LogType";
param.DbType = System.Data.DbType.Int32;
param.Value = 1;
cmd.Parameters.Add(param);
param = cmd.CreateParameter();
param.ParameterName = "@Filter1Text";
param.DbType = System.Data.DbType.String;
param.Value = "Server is listening on";
cmd.Parameters.Add(param);
var rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr.GetString(2));
}
rdr.Close();
}
}
Console.ReadKey();
}
请注意,如果您的服务器使用默认实例(如我的那样),则实例名称为NULL。