//Создаём таблицу
CREATE TABLE Documents(
Id int IDENTITY (1, 1) NOT NULL ,
Document text NULL
)
GO
//Читаем данные из файла
string data="";
StreamReader sr = File.OpenText(@"C:\ToDB.txt");
data = sr.ReadToEnd();
sr.Close();
string connStr = "Data Source=localhost;Initial Catalog=pubs;Integrated Security=SSPI;";
using(SqlConnection con = new SqlConnection(connStr))
{
//Вставляем в DB
SqlCommand com = new SqlCommand("Insert into Documents values(@Data)",con);
com.Parameters.Add("@Data",SqlDbType.Text,data.Length).Value=data;
con.Open();
com.ExecuteNonQuery();
//Извлекаем из DB
com.CommandText = "Select Document from Documents";
SqlDataReader dr = com.ExecuteReader(CommandBehavior.SingleRow);
dr.Read();
string result = dr["Document"].ToString();
//Записываем в файл
StreamWriter sw = File.CreateText(@"C:\FromDB.txt");
sw.Write(result);
sw.Flush();
sw.Close();
}
|