0

I have some really nice long SQL scripts that I want to run from a windows form application. I have gotten the following code to work but I have to put the actual SQL statement in it.

Is there anyway to reference a .sql file that contains my larger script?

    private void button1_Click(object sender, EventArgs e)
    {
        string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True";
        string sql = "SELECT * FROM Authors";
        SqlConnection connection = new SqlConnection(connectionString);
        SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
        DataSet ds = new DataSet();
        connection.Open();
        dataadapter.Fill(ds, "Authors_table");
        connection.Close();
        dataGridView1.DataSource = ds;
        dataGridView1.DataMember = "Authors_table";
    }
}

}

1 Answers1

0

You did not mention anything about these scripts or where they are located BUT if it is in a file that is shared or accessible you can do this:

string sql = System.IO.File.ReadAllText("c:\\mysqlfile.sql");
Igor
  • 60,821
  • 10
  • 100
  • 175