Showing posts with label connection. Show all posts
Showing posts with label connection. Show all posts

Monday, March 26, 2012

Running Transact-SQL script file from within Visual Studio.net

My VB.net project needs to run a .sql file containing transact-sql codes. Is there a way to do this using a connection within VB.net?

Thanks,

Hi Dots
you can read the file as a text file and pass the string of the file to a SQLCommand object|||You will have to check for BATCH separator in the file which requires parsing the file. It is probably easier to just spawn a process like SQLCMD/OSQL and use it to execute the script. You can capture output, error code from the command-line utility and use it in the program.|||

Hi,

I am trying to do what you suggested but can;t get it to work

This is my code...


System.Diagnostics.Process proc = new System.Diagnostics.Process();

proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = @."sqlcmd -i ""C:\Program Files\PCIT\CDEvolution\CreateDB.sql"" -o ""C:\Program Files\PCIT\CDEvolution\CreateDBRes.txt""";

Console.WriteLine(proc.StartInfo.FileName.ToString());

try { proc.Start(); }
catch (Exception e)
{
Console.WriteLine(e.Message);
}

but when I run it I get an erro message saying the file can't be found.

I have tried:

Placing .exe after the "sqlcmd"

Using AppDomain.CurrentDomain.BaseDirectory before the string with and without using the full string for the fille names.

The files do exits in the location spicified but I get the same result every time

How do I get this to work?

Cheers.

PS. You may have noticed I am using C#

Running Transact-SQL script file from within Visual Studio.net

My VB.net project needs to run a .sql file containing transact-sql codes. Is there a way to do this using a connection within VB.net?

Thanks,

Hi Dots
you can read the file as a text file and pass the string of the file to a SQLCommand object|||You will have to check for BATCH separator in the file which requires parsing the file. It is probably easier to just spawn a process like SQLCMD/OSQL and use it to execute the script. You can capture output, error code from the command-line utility and use it in the program.|||

Hi,

I am trying to do what you suggested but can;t get it to work

This is my code...


System.Diagnostics.Process proc = new System.Diagnostics.Process();

proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = @."sqlcmd -i ""C:\Program Files\PCIT\CDEvolution\CreateDB.sql"" -o ""C:\Program Files\PCIT\CDEvolution\CreateDBRes.txt""";

Console.WriteLine(proc.StartInfo.FileName.ToString());

try { proc.Start(); }
catch (Exception e)
{
Console.WriteLine(e.Message);
}

but when I run it I get an erro message saying the file can't be found.

I have tried:

Placing .exe after the "sqlcmd"

Using AppDomain.CurrentDomain.BaseDirectory before the string with and without using the full string for the fille names.

The files do exits in the location spicified but I get the same result every time

How do I get this to work?

Cheers.

PS. You may have noticed I am using C#

Tuesday, March 20, 2012

Running SQL Server Enterprise 2005 on MS Virtual Server

I keep wondering if this is safe. I am getting errors here and there along with the inability to connect to my database via connection string in ASP.NET no matter if the user has complete permissions or not amongst other difficulties and I wonder if this is causing a lot of the problems. WE are running SQL Server Enterprise 2005 on Microsoft Virtual Server. Is this approved?

Running SQL Server in a virtual machine should work. I do this for scenario testing all the time.

The usual suspects for connectivity problems in virtual machines are whether the virtual network adapter is attached to the host's network adapter (vs. the "local adapter" that can't be seen outside the virtual server environment) and the host's firewall getting in the way of other computer's talking to the virtual machine.

The typical issues with SQL Server connectivity also apply to SQL Servers running in virtual machines. Make sure SQL Server is configured to listen to TCP/IP connections on the virtual network adapter that is mapped to the host's network adapter. Also, make sure that the firewall in your virtual machine is allowing external connections to SQL Server's port.

|||We are running some test environments on Virtual Server.
We haven't experienced any issues so far.