使用visual studio安装程序安装的sql server数据库以及如何查找数据库是否安装在我的sql server中? [英] Installed sql server database with visual studio setup and how to find the database is installed in my sql server or not?

查看:133
本文介绍了使用visual studio安装程序安装的sql server数据库以及如何查找数据库是否安装在我的sql server中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在visual studio 2012中创建了一个sql脚本设置,并在我的机器上安装,但是在我的Sql server中找不到数据库,如何知道它是否安装在我的SQL Server中。我是新部署的SQL Server数据库安装程序,请告诉我解决方案,并提前致谢。



这里是代码。



使用System;

使用System.Collections;

使用System.Collections.Generic;

使用System。 ComponentModel;

使用System.Configuration.Install;

使用System.Linq;

使用System.Threading.Tasks;

使用Microsoft.SqlServer.Management.Smo;

使用Microsoft.SqlServer.Management.Common;

使用System.Reflection;

使用System.IO;

使用System.Data.SqlClient;



命名空间ExchangeAppDatabase

{

[RunInstaller(true)]

public partial class CustomInstaller:System.Configuration.Install.Installer

{

private string logFilePath =C:\\SetupLog.txt;

public CustomInstaller()

{

//组件设计器需要此调用。

//调用InitializeComponent后添加初始化代码

InitializeComponent();

}



私人字符串GetSql(字符串名称)

{

尝试

{



//获取当前程序集。

程序集Asm = Assembly.GetExecutingAssembly();



//资源使用完全限定名称命名。

Stream strm = Asm.GetManifestResourceStream(Asm.GetName()。Name +。+ Name);



//读取嵌入文件的内容。

StreamReader reader = new StreamReader(strm);



返回reader.ReadToEnd();

}

catch(例外情况)

{

Log(ex.ToString());

throw ex;

}

}

private void ExecuteSql(string serverName,string dbName,string Sql)

{

string connStr =Data Source =+ serverName +; Initial Catalog =+ dbName +; Integrated Security = True;

使用(SqlConnection conn = new SqlConnection(connStr))

{

try

{

服务器服务器=新服务器(新的ServerConnection(conn));

server.ConnectionContext.ExecuteNonQuery(Sql);

}

catch(例外情况)

{

Log(ex.ToString());
$
}

}

protected void AddMasterDatabase(string serverName)

{

try

{

//创建数据库并安装表。

string strScript = GetSql(master.sql);

ExecuteSql(serverName,master,strScript);

}

catch(exception ex)

{

//报告任何错误并中止。

Log(ex.ToString( ));

抛出ex;

}

}



protected void AddAppRuleLogDatabase(string serverName)

{

try

{

//创建数据库并安装表。

string strScript = GetSql(apprulelog.sql);

ExecuteSql(serverName,master,strScript);

}

catch(exception ex)

{

//报告任何错误并中止。

Log(ex.ToString( ));

抛出ex;

}

}



公共覆盖void Install(System.Collections.IDictionary stateSaver)

{

base.Install(stateSaver);

Log(安装程序启动);



if(this.Context.Parameters [InstallType] ==1)

{

AddMasterDatabase(this.Context.Parameters [InstallServerName]);

}



if(this.Context.Parameters [InstallType ] ==2)

{

AddAppRuleLogDatabase(this.Context.Parameters [InstallServerName]);

}



if(this.Context.Parameters [InstallType ] ==3)

{

AddMasterDatabase(this.Context.Parameters [InstallServerName]);

AddAppRuleLogDatabase(this。 Context.Parameters [InstallServerName]);

}







}

public void Log(string str)

{

StreamWriter Tex;

try

{

Tex = File.AppendText(this.logFilePath);

Tex.WriteLine(DateTime.Now.ToString()++ str);

Tex.Close();

}

catch

{}

}

}

}

Hi, i was created an sql script setup in visual studio 2012, and Installed in my machine, but the database is not find in my Sql server, how to know whether it is installed in my SQL Server or not. I am new deployement of SQL Server Database Setup, Tell me the slove, and Thanks Advancely.

And Here is Code for that.

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Reflection;
using System.IO;
using System.Data.SqlClient;

namespace ExchangeAppDatabase
{
[RunInstaller(true)]
public partial class CustomInstaller : System.Configuration.Install.Installer
{
private string logFilePath = "C:\\SetupLog.txt";
public CustomInstaller()
{
//This call is required by the Component Designer.
//Add initialization code after the call to InitializeComponent
InitializeComponent();
}

private string GetSql(string Name)
{
try
{

// Gets the current assembly.
Assembly Asm = Assembly.GetExecutingAssembly();

// Resources are named using a fully qualified name.
Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name);

// Reads the contents of the embedded file.
StreamReader reader = new StreamReader(strm);

return reader.ReadToEnd();
}
catch (Exception ex)
{
Log(ex.ToString());
throw ex;
}
}
private void ExecuteSql(string serverName, string dbName, string Sql)
{
string connStr = "Data Source=" + serverName + ";Initial Catalog=" + dbName + ";Integrated Security=True";
using (SqlConnection conn = new SqlConnection(connStr))
{
try
{
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(Sql);
}
catch (Exception ex)
{
Log(ex.ToString());
}
}
}
protected void AddMasterDatabase(string serverName)
{
try
{
// Creates the database and installs the tables.
string strScript = GetSql("master.sql");
ExecuteSql(serverName, "master", strScript);
}
catch (Exception ex)
{
//Reports any errors and abort.
Log(ex.ToString());
throw ex;
}
}

protected void AddAppRuleLogDatabase(string serverName)
{
try
{
// Creates the database and installs the tables.
string strScript = GetSql("apprulelog.sql");
ExecuteSql(serverName, "master", strScript);
}
catch (Exception ex)
{
//Reports any errors and abort.
Log(ex.ToString());
throw ex;
}
}

public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
Log("Setup started");

if(this.Context.Parameters["InstallType"] == "1")
{
AddMasterDatabase(this.Context.Parameters["InstallServerName"]);
}

if (this.Context.Parameters["InstallType"] == "2")
{
AddAppRuleLogDatabase(this.Context.Parameters["InstallServerName"]);
}

if (this.Context.Parameters["InstallType"] == "3")
{
AddMasterDatabase(this.Context.Parameters["InstallServerName"]);
AddAppRuleLogDatabase(this.Context.Parameters["InstallServerName"]);
}



}
public void Log(string str)
{
StreamWriter Tex;
try
{
Tex = File.AppendText(this.logFilePath);
Tex.WriteLine(DateTime.Now.ToString() + " " + str);
Tex.Close();
}
catch
{ }
}
}
}

推荐答案

取决于你想要检查的方式。



你可以通过打开SSMS(Sql Server Management Studio)手动完成,连接到你的实例(通过第一个登录屏幕)并查看左侧的数据库列表。



或者您可以通过列出所有数据库在代码中执行此操作在您的服务器实例上(如果您有连接字符串):

Depends on how you want to check.

You do it manually by opening SSMS (Sql Server Management Studio), connecting to your instance (via the first log-in screen) and looking in the Databases list on the left hand side.

Or you can do it in code by listing all the databases on your server instance (if you have a connection string):
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("sp_databases", con))
        {
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataReader read = cmd.ExecuteReader();
        while (read.Read())
            {
            Console.WriteLine((string)read["DATABASE_NAME"]);
            }
        }
    }


这篇关于使用visual studio安装程序安装的sql server数据库以及如何查找数据库是否安装在我的sql server中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆