Sqlserver
 sql >> डेटाबेस >  >> RDS >> Sqlserver

मैं SQL सर्वर 2008 में varbinary (अधिकतम) कॉलम में Excel फ़ाइलों को कैसे सम्मिलित/पुनर्प्राप्त करूं?

यदि आप इसे सीधे ADO.NET में करना चाहते हैं, और आपकी एक्सेल फाइलें इतनी बड़ी नहीं हैं कि वे एक ही बार में मेमोरी में फिट हो सकें, तो आप इन दो विधियों का उपयोग कर सकते हैं:

// 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();
    }
}

एक्सेल शीट को वापस पाने और उसे एक फाइल में स्टोर करने के लिए, इस विधि का उपयोग करें:

public void RetrieveExcelFromDatabase(int ID, string excelFileName)
{
    byte[] excelContents;

    string selectStmt = "SELECT BinaryContent FROM dbo.YourTableHere WHERE ID = @ID";

    using (SqlConnection connection = new SqlConnection("your-connection-string-here"))
    using (SqlCommand cmdSelect = new SqlCommand(selectStmt, connection))
    {
        cmdSelect.Parameters.Add("@ID", SqlDbType.Int).Value = ID;

        connection.Open();
        excelContents = (byte[])cmdSelect.ExecuteScalar();
        connection.Close();
    }

    File.WriteAllBytes(excelFileName, excelContents);
 }

बेशक, आप इसे अपनी आवश्यकताओं के अनुसार अनुकूलित कर सकते हैं - आप बहुत सी अन्य चीजें भी कर सकते हैं - इस पर निर्भर करते हुए कि आप वास्तव में क्या करना चाहते हैं (आपके प्रश्न से बहुत स्पष्ट नहीं है)।



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. SQL Server 2008 में XML फ़ील्ड से मानों का चयन करें

  2. SQL सर्वर आंतरिक:योजना कैशिंग पीटी। मैं - पुन:उपयोग योजना

  3. तालिका में पहचान कॉलम के लिए एक स्पष्ट मान केवल तभी निर्दिष्ट किया जा सकता है जब कॉलम सूची का उपयोग किया जाता है और IDENTITY_INSERT SQL सर्वर पर होता है

  4. एसक्यूएल:पिछली पंक्ति मान के साथ खाली कक्षों को कैसे भरें?

  5. किसी SQL से बेस डेटा प्रकार लौटाएं_SQL सर्वर में भिन्न मान