We help IT Professionals succeed at work.

Different instances of sqlexpress

FLUXWIRE
FLUXWIRE asked
on
468 Views
Last Modified: 2010-04-15
Hi All.
I have installed the sqlexpress in my machine with the instance SQLEXPRESS. when i run my program in c# i have two different instances running in the machine. one logged by network services and other with my own credentials.
Using SMO and Serverconnection(".\SQLEXPRESS") I got one of them. and with sqlconnection... the other. What i need to do to use the same all the time?

thx in advance.
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Did you install two instances with the same name?  Or, did you name one of them differently than the other?

Bob

Author

Commented:
nop. I only have in the services one service SQLEXPRESS.
Ok. the begginning is:
1. i need to create a new database files, database.mdf and database_log.ldf
2. I need to do this using SMO, because i will do that after the clickonce deployment
3. With angellll we agreed that the only way is using a SqlCommand("CREATE DATABASE...
4. the connectionstring for the SMO ServerConnection is (@".\SQLEXPRESS") and for the sqlconnection is @"Data Source=.\SQLEXPRESS"...
5. trying to create the database, i realize that using the smo the database does not exists and the create database gives me an error sayng that the database already exists...
after that, into the TaskManageri could see two sqlservr.exe instances running.

thx in advance.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
What is wrong with something like this with SMO?

// Add a reference to Microsoft.SqlServer.Manager.Smo
// Add a reference to Microsoft.SqlServer.ConnectionInfo

using System;
using Microsoft.SqlServer.Management.Smo;

public class SqlServerDatabase
{

  public void CreateDatabase(string server, string databaseName)
  {
    Server instance = new Server(server);

    Database db = new Database(instance, databaseName);
    db.Create();
  }

}

Bob

Author

Commented:
Hi.
well. thats is the problem. If we are talking about sqlexpress, i need to have the files (primary file and transaction file) into a known directory.  
This smo construction creates a database into a sql server instance but where?
sds
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Okey!
than are you saying that with the create(), if the datafiles didnt exists, they will be created in this folder even i only have the sqlexpress installed?

if it is true, all my problems goes away.
thx in advance.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
I tested it with my SQL Server 2005 Express Advanced Edition SP2, but I believe that it should work with the regular edition also.

Bob
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
If you have C# Express 2005, then I could see where you would think that you needed to create a database file, instead of a normal database.

Bob

Author

Commented:
im just finishing download it. but i will test it asap in my office. give me a minute.
sds

Author

Commented:
Hi Bob.
with this construction
using System;
using Microsoft.SqlServer.Management.Smo;

public class SqlServerDatabase
{

  public void CreateDatabase(string server, string databaseName)
  {
    Server instance = new Server(server);

    Database db = new Database(instance, databaseName);
    db.Create();
  }

}

how is the connection string?
You do not need to reference a ServerConnection?
thx in advance.

Author

Commented:
i mean. what is the value of the string server in the arguments when you call the CreateDatabase
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
You just need the server name and the database name:

SqlServerDatabase.CreateDatabase(@".\SQLEXPRESS", "MyDatabase");

Bob

Author

Commented:
yep. it works.
great job Bob.
thx

Author

Commented:
Hi. another question about the same subject
this is part of my code

ServerConnection sc = new ServerConnection(@".\SQLEXPRESS");
Server server = new Server(sc);
System.Collections.Specialized.StringCollection dbPath = new System.Collections.Specialized.StringCollection();
dbPath.Add(@"d:\thefolder\catalog.mdf");
Database db = new Database(server,"catalog");
db.Create();
Database db = server.Databases["catalog"];

could i use this to create the files into a specific folder?

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.