powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / ADO.NET, LINQ, Entity Framework, NHibernate, DAL, ORM [игнор отключен] [закрыт для гостей] / из БД на диск
5 сообщений из 5, страница 1 из 1
из БД на диск
    #34472685
serghey
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
не получается выгрузить в файл на диск хранящийся в таблице файл в двоичной форме:

string source = @"Data source=sergm\sergm;" +
"Initial Catalog=document; Integrated security=SSPI";
SqlConnection sqlcon = new SqlConnection(source);
SqlCommand com = new SqlCommand();
com.CommandText = @"select [document] from doc where id_doc=12";
com.Connection = sqlcon;
FileStream fs;
long retval;
BinaryWriter writer;
int bufferSize = 100;
byte[] document = new byte[bufferSize];
long startIndex = 0;
sqlcon.Open();

SqlDataReader reader = com.ExecuteReader(CommandBehavior.SequentialAccess);
while (reader.Read())
{

fs = new FileStream(@"d:\test10.odt", FileMode.OpenOrCreate, FileAccess.Write);
startIndex = 0;
retval = reader.GetBytes(1, startIndex, document, 0, bufferSize);



while (retval == bufferSize)
{
writer.Write(document);
writer.Flush();

startIndex += bufferSize;
retval = reader.GetBytes(1, startIndex, document, 0, bufferSize);
}
writer.Write(document, 0, (int)retval - 1);
writer.Flush();
writer.Close();
fs.Close();
}
reader.Close();
sqlcon.Close();

помогите пожалуйста
...
Рейтинг: 0 / 0
из БД на диск
    #34472728
serghey
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
извиняюсь, вот этот:

string source = @"Data source=sergm\sergm;" +
"Initial Catalog=document; Integrated security=SSPI";
SqlConnection sqlcon = new SqlConnection(source);
SqlCommand com = new SqlCommand();
com.CommandText = @"select [document] from doc where id_doc=12";
com.Connection = sqlcon;
FileStream fs;
long retval;
BinaryWriter writer;
int bufferSize = 100;
byte[] document = new byte[bufferSize];
long startIndex = 0;
sqlcon.Open();

SqlDataReader reader = com.ExecuteReader(CommandBehavior.SequentialAccess);
while (reader.Read())
{

fs = new FileStream(@"d:\test10.odt", FileMode.OpenOrCreate, FileAccess.Write);
writer = new BinaryWriter(fs);
startIndex = 0;
retval = reader.GetBytes(1, startIndex, document, 0, bufferSize);
while (retval == bufferSize)
{
writer.Write(document);
writer.Flush();

// Reposition start index to end of last buffer and fill buffer.
startIndex += bufferSize;
retval = reader.GetBytes(1, startIndex, document, 0, bufferSize);
}
writer.Write(document, 0, (int)retval - 1);
writer.Flush();
writer.Close();
fs.Close();
}
reader.Close();
sqlcon.Close();
...
Рейтинг: 0 / 0
из БД на диск
    #34472760
serghey
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
ошибка: Index was outside the bounds of the array.
...
Рейтинг: 0 / 0
из БД на диск
    #34473008
serghey
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
string source = @"Data source=sergm\sergm;" +
"Initial Catalog=document; Integrated security=SSPI";
SqlConnection sqlcon = new SqlConnection(source);
SqlCommand com = new SqlCommand();
com.CommandText = @"select [document] from doc where id_doc=16";
com.Connection = sqlcon;
FileStream fs;
long retval;
BinaryWriter writer;
int bufferSize = 100;
byte[] document = new byte[bufferSize];
long startIndex = 0;
sqlcon.Open();

SqlDataReader reader = com.ExecuteReader(CommandBehavior.SequentialAccess);
while (reader.Read())
{
if (File.Exists(@"D:\test10.odt"))
File.Delete(@"D:\test10.odt");
fs = new FileStream(@"d:\test10.odt", FileMode.OpenOrCreate, FileAccess.Write);
writer = new BinaryWriter(fs);
startIndex = 0;
retval = reader.GetBytes(0, startIndex, document, 0, bufferSize);
while (retval == bufferSize)
{
writer.Write(document);
writer.Flush();

startIndex += bufferSize;
retval = reader.GetBytes(0, startIndex, document, 0, bufferSize);
}
writer.Write(document, 0, (int)retval - 1);
writer.Flush();
writer.Close();
fs.Close();
}
reader.Close();
sqlcon.Close();

теперь ошибка при открытии файла: файл поврежден, попытаться восстановить его?
после восстановления ничего в файле не обнаруживаю
...
Рейтинг: 0 / 0
из БД на диск
    #34473015
serghey
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
размер файла несколько уменьшился
...
Рейтинг: 0 / 0
5 сообщений из 5, страница 1 из 1
Форумы / ADO.NET, LINQ, Entity Framework, NHibernate, DAL, ORM [игнор отключен] [закрыт для гостей] / из БД на диск
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]