SQL Server CE + external SQL file
I'm creating a SQL Server CE database programmatically (not using entity or LINQ). Currently I am embedding the SQL statements into the source code, but I'd like 开发者_运维知识库to avoid this if possible.
Is there a way to simply have an external .sql file that I could read in and execute using SQL Server CE classes?
Edited for Clarity**
I am working on the assumption that there is a method in some SQL Server CE class that will simply allow me to execute .sql file. So something like: command.executeSqlFile(filename).
The reason for this is I am building a testing application in which the sql server ce database will need to be created and re-created over throughout the use of the application. I don't really want to hard code the SQL - it would make it much easier for me to just have an external .sql file with:
...
CREATE TABLE TEST(col1 int, col2 int, col3 int);
INSERT INTO test (col1,col2,col3) VALUES (11, 22, 33);
...
Again, I could easily read this in with IO classes, just wondering if there is a cleaner way (using only System.Data & System.Data.SqlServerCE classes) of doing it.
Thanks!
As Rup mentioned in the comment, you should be able to read in the data from a text file you create. You could do this in several ways, including separate files for each .sql statement or one large file.
I would caution that this could be a security risk as well. Someone could easily modify those files and do malicious things to your db. Be careful that you don't store credentials in plain text in those files as well. You may want to consider encrypting your external data files.
Or you could simply create a separate CE file that contains a table of SQL statements that can easily be maintained within the application or even a separate one. In this way, you would open the statement database, call the "procedure" you wanted, pull in the text, close it, open the real DB and use the result of the first query to execute on the real DB. Of course, you're gonna be limited to the 4000 characters per field, but that's quite a bit in itself. If you need more, combine several together.
精彩评论