Kullandığınız veri erişim teknolojisine göre değişir. Örneğin, Entity Framework'ü kullanırsanız, nesneleri kullanarak bir bayt dizisini veritabanına kaydedebilirsiniz.
// store Excel sheet (or any file for that matter) into a SQL Server table
public void StoreExcelToDatabase(string excelFileName)
{
// if file doesn't exist --> terminate (you might want to show a message box or something)
if (!File.Exists(excelFileName))
{
return;
}
// get all the bytes of the file into memory
byte[] excelContents = File.ReadAllBytes(excelFileName);
// define SQL statement to use
string insertStmt = "INSERT INTO dbo.YourTable(FileName, BinaryContent) VALUES(@FileName, @BinaryContent)";
// set up connection and command to do INSERT
using (SqlConnection connection = new SqlConnection("your-connection-string-here"))
using (SqlCommand cmdInsert = new SqlCommand(insertStmt, connection))
{
cmdInsert.Parameters.Add("@FileName", SqlDbType.VarChar, 500).Value = excelFileName;
cmdInsert.Parameters.Add("@BinaryContent", SqlDbType.VarBinary, int.MaxValue).Value = excelContents;
// open connection, execute SQL statement, close connection again
connection.Open();
cmdInsert.ExecuteNonQuery();
connection.Close();
}
}
için: düz ADO.NET içinde bunu yapmak istiyorum ve onlar belleğe sığabilecek böylece Excel dosyaları tek seferde çok büyük değilse
Dosyayı kaydetmeden önce sıkıştırmak faydalı olur mu? Eğer dosyalar büyükse ve/veya birçoğu varsa, bu uygunsa, avantajlı olabilir. –
XLSX zaten sıkıştırılmış, –