How to execute sql script in .Net?

  • Thread starter Thread starter Faraz Rasheed
  • Start date Start date
F

Faraz Rasheed

I want to make an application in C# or VB.Net which
executes the sql script written in some file (script.sql).
I am using the SQL Server 2000.
 
Faraz,

Below is a example for a console application that reads a query from a text
file and executes the query on the SQL Server.

For testing purposes I put the following line in the text file:
select count(*) from orders

using System;
using System.Data.SqlClient;
using System.IO;

namespace DataSql
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[STAThread]
static void Main(string[] args)
{
string myConnectionString = "Initial Catalog=Northwind;Data
Source=localhost;Integrated Security=SSPI;";
SqlConnection myConnection = new SqlConnection(myConnectionString);
myConnection.Open();
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null && (line.Length > 0) )
{
int cnt = ExecuteMyQuery(myConnection, line);
Console.WriteLine(line + " returned {0}", cnt);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
myConnection.Close();
Console.WriteLine("Hit Enter to Close");
Console.ReadLine();
}



static int ExecuteMyQuery(SqlConnection myConnection, string
mySqlCommand)
{

SqlCommand myCommand = new SqlCommand(mySqlCommand,myConnection);
int iRet = (int) myCommand.ExecuteScalar();
return iRet;
}
}
}

I hope this helps.

Andrew Steenson [MSFT]
Visual Studio Update Team
--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
use osql.exe , i.e create a new process of osql and pass your script file as
an argument to it..

--
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Gurudev
Software Engineer, NetKraft,
Bangalore, India.
e-me: (e-mail address removed)
____________________________________________
Andrew Steenson said:
Faraz,

Below is a example for a console application that reads a query from a text
file and executes the query on the SQL Server.

For testing purposes I put the following line in the text file:
select count(*) from orders

using System;
using System.Data.SqlClient;
using System.IO;

namespace DataSql
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[STAThread]
static void Main(string[] args)
{
string myConnectionString = "Initial Catalog=Northwind;Data
Source=localhost;Integrated Security=SSPI;";
SqlConnection myConnection = new SqlConnection(myConnectionString);
myConnection.Open();
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null && (line.Length > 0) )
{
int cnt = ExecuteMyQuery(myConnection, line);
Console.WriteLine(line + " returned {0}", cnt);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
myConnection.Close();
Console.WriteLine("Hit Enter to Close");
Console.ReadLine();
}



static int ExecuteMyQuery(SqlConnection myConnection, string
mySqlCommand)
{

SqlCommand myCommand = new SqlCommand(mySqlCommand,myConnection);
int iRet = (int) myCommand.ExecuteScalar();
return iRet;
}
}
}

I hope this helps.

Andrew Steenson [MSFT]
Visual Studio Update Team
--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
I want to make an application in C# or VB.Net which
executes the sql script written in some file (script.sql).
I am using the SQL Server 2000.
 
Back
Top