Click here to Skip to main content
15,896,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
{ }
}
}
}
Posted
Updated 27-Jul-14 23:41pm
v2

1 solution

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):
C#
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"]);
            }
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900