S
serge
I managed to put together C# code and have it
do the following:
1- Get all the table names that start with the letter "Z"
from sysobjects of my SQL 2000 database and put
these table names inside an array variable.
2- Loop through each table name inside the array
1 to 1000 tables
3- Loop through each SQL server script file
inside a folder, 1 to 250 files
4- Replace all occurences of " TABLENAME"
with " dbo.TABLENAME"
(TABLENAME being the values inside the array)
I am searching for the space character + the table name
and replacing it with the space character + dbo. + the
table name.
The problem is this is very slow as the C# code I am
currently using ends up opening and saving each file 1000
times. And multiply 1000 x 250, this is slowing it a lot.
If I try to change the foreach locations so that it initially
starts to loop the files and within each file loop through
the array tablenames and replace any occurences, I don't
know how I can run the REPLACE code which is
writing the contents it is reading to a new temp file,
to re-read the newly created temp file and continue
this "recursive" type of operation until it has searched
any of the 1000 table name occurences?
Can someone please help me with this?
Another thing I was trying for the last few hours
is to use the same code I have below except use
a DOS executable I had that does "replacestring.exe"
but since I am using PROCESS Shelling, this is even slower.
By the way how do I pass a double-quote parameter
to the Process...Arguments? I don't know what I am doing
wrong here. I tried writing "\"", """, """"", and many other
variations but I am unable to figure out the right syntax.
Thank you
This is my current code:
private void button3_Click(object sender, System.EventArgs e)
{
string strSQL;
strSQL = "SELECT name FROM sysobjects WHERE name LIKE 'A%";
strSQL = strSQL + "' AND xtype='U' ORDER BY name";
if (!ClassMyDbAccess.Open("(local)", "MYDB", "true", "sa", "mypass"))
return;
// Extract list of tables
ArrayList tableNames = new ArrayList();
using (SqlDataReader rdr = ClassMyDbAccess.ExecuteReader(strSQL))
{
while (rdr.Read())
tableNames.Add(rdr.GetString(0));
}
// For each table in the list
foreach (string tableCrt in tableNames)
{
string tempTarget = System.IO.Path.GetTempFileName();
string path = @"F:\Temp\SQLServer\";
DirectoryInfo dirInfo = new DirectoryInfo(Path.GetDirectoryName(path));
string[] files = Directory.GetFiles(path);
foreach (string fileCrt in files)
{
using (StreamReader reader = new StreamReader(fileCrt))
{
try
{
using (StreamWriter writer = new StreamWriter (tempTarget))
{
string line;
while ( (line=reader.ReadLine()) != null)
{
writer.WriteLine (line.Replace(" " + tableCrt, " dbo." + tableCrt));
}
}
}
catch (Exception f)
{
Console.WriteLine(System.DateTime.Now + " Error while processing file
{0} : {1}", fileCrt, f.Message);
}
}
File.Delete(fileCrt);
File.Move(tempTarget,fileCrt);
}
}
}
do the following:
1- Get all the table names that start with the letter "Z"
from sysobjects of my SQL 2000 database and put
these table names inside an array variable.
2- Loop through each table name inside the array
1 to 1000 tables
3- Loop through each SQL server script file
inside a folder, 1 to 250 files
4- Replace all occurences of " TABLENAME"
with " dbo.TABLENAME"
(TABLENAME being the values inside the array)
I am searching for the space character + the table name
and replacing it with the space character + dbo. + the
table name.
The problem is this is very slow as the C# code I am
currently using ends up opening and saving each file 1000
times. And multiply 1000 x 250, this is slowing it a lot.
If I try to change the foreach locations so that it initially
starts to loop the files and within each file loop through
the array tablenames and replace any occurences, I don't
know how I can run the REPLACE code which is
writing the contents it is reading to a new temp file,
to re-read the newly created temp file and continue
this "recursive" type of operation until it has searched
any of the 1000 table name occurences?
Can someone please help me with this?
Another thing I was trying for the last few hours
is to use the same code I have below except use
a DOS executable I had that does "replacestring.exe"
but since I am using PROCESS Shelling, this is even slower.
By the way how do I pass a double-quote parameter
to the Process...Arguments? I don't know what I am doing
wrong here. I tried writing "\"", """, """"", and many other
variations but I am unable to figure out the right syntax.
Thank you
This is my current code:
private void button3_Click(object sender, System.EventArgs e)
{
string strSQL;
strSQL = "SELECT name FROM sysobjects WHERE name LIKE 'A%";
strSQL = strSQL + "' AND xtype='U' ORDER BY name";
if (!ClassMyDbAccess.Open("(local)", "MYDB", "true", "sa", "mypass"))
return;
// Extract list of tables
ArrayList tableNames = new ArrayList();
using (SqlDataReader rdr = ClassMyDbAccess.ExecuteReader(strSQL))
{
while (rdr.Read())
tableNames.Add(rdr.GetString(0));
}
// For each table in the list
foreach (string tableCrt in tableNames)
{
string tempTarget = System.IO.Path.GetTempFileName();
string path = @"F:\Temp\SQLServer\";
DirectoryInfo dirInfo = new DirectoryInfo(Path.GetDirectoryName(path));
string[] files = Directory.GetFiles(path);
foreach (string fileCrt in files)
{
using (StreamReader reader = new StreamReader(fileCrt))
{
try
{
using (StreamWriter writer = new StreamWriter (tempTarget))
{
string line;
while ( (line=reader.ReadLine()) != null)
{
writer.WriteLine (line.Replace(" " + tableCrt, " dbo." + tableCrt));
}
}
}
catch (Exception f)
{
Console.WriteLine(System.DateTime.Now + " Error while processing file
{0} : {1}", fileCrt, f.Message);
}
}
File.Delete(fileCrt);
File.Move(tempTarget,fileCrt);
}
}
}