I've written a program that loads in a text SQL file of commands Processes it and displays the result.
To test it I wrote a simple top 10 query which worked perfectly. Then I thought I'd like to test other scripts so in my text document I created:
select ItemDescription from PurchaseOrderIt ems
where ItemDescription like 'z%'
At which point my code balked at the like and whined about a syntax error in my SQL command code. I'm guessing that it doesnt like the ' symbol. How do I process this without altering the text file?
LOAD TEXT FILE
OPEN SQL CONNECTION
SEND TEXT SQL TO SERVER
DISPLAY RETURNED DATA
CLOSE SQL CONNECTION
END
DBSCAN.TXT contains
select ItemDescription from PurchaseOrderIt ems
where ItemDescription like 'z%'
To test it I wrote a simple top 10 query which worked perfectly. Then I thought I'd like to test other scripts so in my text document I created:
select ItemDescription from PurchaseOrderIt ems
where ItemDescription like 'z%'
At which point my code balked at the like and whined about a syntax error in my SQL command code. I'm guessing that it doesnt like the ' symbol. How do I process this without altering the text file?
LOAD TEXT FILE
OPEN SQL CONNECTION
SEND TEXT SQL TO SERVER
DISPLAY RETURNED DATA
CLOSE SQL CONNECTION
END
DBSCAN.TXT contains
select ItemDescription from PurchaseOrderIt ems
where ItemDescription like 'z%'
Code:
public void OpenDataBase(bool OK) { OK = true; string SQLinput = null; try { using (StreamReader sr = new StreamReader("c:\\dbscan.txt")) { String line; // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { SQLinput = SQLinput + line; } } } catch (Exception ex) { // Let the user know what went wrong. MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); } string SQLstring = @"" + SQLinput; //********************** try { SqlConnection myConnection = new SqlConnection( "Data Source=sql-01;" + "Initial Catalog=Training;" + "Persist Security Info=True;" + "User ID=xxx;Password=*xxx;"); myConnection.Open(); SqlDataReader reader = null; SqlCommand SQLcmd = new SqlCommand(SQLstring, myConnection); reader = SQLcmd.ExecuteReader(); // ************************* if (reader.HasRows) { while (reader.Read()) { //textBox1.Text = textBox1.Text + reader[0].ToString() + " : "+ reader[1].ToString() + Environment.NewLine; textBox1.Text = textBox1.Text +reader.GetString(0)+Environment.NewLine; } } else { MessageBox.Show("Error!"); } myConnection.Close(); } catch (SqlException ex) { OK = false; MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); } ovalShape1.FillColor = Color.Red; } private void ovalShape1_Click(object sender, EventArgs e) { } }
Comment