c# - How store a large file into SQL Server 2008 and retrieve all of it? -
my problem sql server database.
i stored info sql server table c# code:
internal static void saveautocadfileondatabase(string mapcode, byte[] dataelecmap, string dataelecmapname,byte[] datacivilmap, string datacivilmapname, byte[] dataarchimap, string dataarchimapname,byte[]datamechamap,string datamechamapname) { string sqlcommand = string.concat("insert autocadfiles values ('", mapcode, "' , ", "cast ('", dataelecmap, "' varbinary(max)),", " '", dataelecmapname, "' ", " , cast ('", datacivilmap, "' varbinary(max)) ,", " '", datacivilmapname, "' ", " , cast ('", dataarchimap, "' varbinary(max)) ,", " '", dataarchimapname, "' "," , cast ('", datamechamap, "' varbinary(max)) ,", " '", datamechamapname, "' ", ")"); executenonquery(sqlcommand); }
and retrieve code:
internal static datatable retrivemaps(string mapnumber, string maptype) { string s = maptype + "name"; string sqlcommand = string.concat("select ", maptype, " , ", s, " autocadfiles mapcode='",mapnumber, "'"); homecoming getbinaryfiles(sqlcommand); } private static datatable getbinaryfiles(string sqlcommand) { sqlconnection sqlconnction = null; datatable dt = new datatable(); sqlcommand sqlcom = getconnection(sqlcommand, ref sqlconnction); sqldatareader sdldr = sqlcom.executereader(); dt.load(sdldr); homecoming dt; }
i file datatable , save filesystem. stored file volume 0 byte
or 13 byte less stored file in database.
since you're retrieving single row @ time, simpler code work retrieving data:
string filename; byte[] fileread; sqlcommand command; sqlconnection connection = mygetsqlconnection(); connection.open(); command = new sqlcommand("select dataelecmapname autocadfiles mapcode = @mapnumber", connection); command.parameters.add("@mapnumber", sqldbtype.int).value = mapnumber; filename = (string)command.executescalar(); command.dispose(); command = new sqlcommand("select dataelecmap autocadfiles mapcode = @mapnumber", connection); command.parameters.add("@mapnumber", sqldbtype.int).value = mapnumber; fileread = (byte[])command.executescalar(); string temppath = myfunc.gettempdirectory(); filestream fs = new filestream(temppath + filename, filemode.create); fs.write(fileread, 0, fileread.length); system.diagnostics.process.start(temppath + filename); fs.close();
edit utilize code convert file:
public static byte[] convertfiletobytes(string location, ref string filename) { filestream fs = new filestream(location, filemode.open, fileaccess.read); binaryreader reader = new binaryreader(fs); byte[] info = reader.readbytes((int)fs.length); fs.close(); homecoming data; }
c# sql-server wpf sql-server-2008
No comments:
Post a Comment