deepa

deepa

  • NA
  • 42
  • 0

excecuting file containing sql script of a database

Feb 15 2006 4:04 AM
how to execute a text file, which contains sql script of a database.
I need to execute this file in my code.. i tried this function:
private void ExecSqlFileProcess(String fileName)
{
if (! File.Exists(fileName))
{
MessageBox.Show ("Specified .SQL File Not Found.","Net Fare Database Upgrade Tool",MessageBoxButtons.OK);
return;
}
using (StreamReader sr = new StreamReader(fileName))
{
String line,line1;
String sqlLine= "" ;

while ((line = sr.ReadLine()) != null)
{
line1 = line.Trim();
int compareResult = String.Compare(line1, "GO", true, CultureInfo.InvariantCulture);

if(compareResult != 0)
sqlLine += line + Environment.NewLine ;
//sqlLine += line + c.ToString() ;
else
{
if(sqlLine != "")
{
try
{
int i = SqlHelper.ExecuteNonQuery(connectionString,CommandType.Text,"USE "+DatabaseName+" "+sqlLine);
}
catch(Exception Ex)
{
MessageBox.Show (Ex.Message.ToString(),"Net Fare Database Upgrade Tool",MessageBoxButtons.OK ,MessageBoxIcon.Error );;
sqlLine = "";
}
}
sqlLine = "";
}
}//While
}//Using
}

but i got the following errors for all stored procedure scripts..but it dropped the stored procedure..it only did not create it..following error comes:
'Create Procedure' must be first statement in the query batch.
Must declare variable @IATACodeName, @IATADescription..etc..

Answers (4)