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

फाइलस्ट्रीम विकल्प का उपयोग कर एसक्यूएल सर्वर 2008 में फाइलों को संग्रहित करना

SQL भाग:

-- Enable the FileStream Feature
EXEC sp_configure filestream_access_level, 2
GO
RECONFIGURE
GO


--Create a special file group and mark it as a stream
CREATE DATABASE FileStreamExample
ON
PRIMARY ( 
    NAME = FileStreamExample_Primary,
    FILENAME = 'c:\Data\FileStreamExample.mdf'),
FILEGROUP FileStreamGroup CONTAINS  FILESTREAM ( 
    NAME = FileStreamExample_FileGroup,
    FILENAME = 'c:\Data\FileStreamExample')
LOG ON ( NAME = FileStreamExample_Log,
    FILENAME = 'c:\Data\FileStreamExample.ldf')
GO

USE FileStreamExample
GO

CREATE TABLE Product
(
  ProductID INT  NOT NULL  PRIMARY KEY,
  Name VARCHAR(50) NOT NULL,
  Picture VARBINARY(MAX) FILESTREAM  NULL,
  RowGuid UNIQUEIDENTIFIER  NOT NULL  ROWGUIDCOL
  UNIQUE DEFAULT NEWID()
)
GO

Insert into Product
Values(1, 'Bicycle', 0x00, default)
GO

Select * From Product

C# भाग:फ़ाइल लिखें

   string connectionString = ConfigurationManager.ConnectionStrings["fileStreamDB"].ConnectionString;
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        //Get the PathName of the File from the database
        command.CommandText = "SELECT Picture.PathName(), "
        + "GET_FILESTREAM_TRANSACTION_CONTEXT() FROM Product WHERE ProductID = 1";
        SqlTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
        command.Transaction = transaction;
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                string path = reader.GetString(0);
                SqlFileStream stream = new SqlFileStream(path,
                    (byte[])reader.GetValue(1), FileAccess.Write,
                    FileOptions.SequentialScan, 0);                        
                string contents = txtInput.Text;                        
                stream.Write((System.Text.Encoding.ASCII.GetBytes(contents)), 0, contents.Length);
                stream.Close();
            }
        }
        transaction.Commit();
    }

C# भाग:फ़ाइल पढ़ें

      string connectionString = ConfigurationManager.ConnectionStrings["fileStreamDB"].ConnectionString;                
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            //Get the PathName of the File from the database
            command.CommandText = "SELECT Picture.PathName(), "
            + "GET_FILESTREAM_TRANSACTION_CONTEXT() FROM Product WHERE ProductID = 1";
            SqlTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
            command.Transaction = transaction;
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {                        
                    string path = reader.GetString(0);                        
                    SqlFileStream stream = new SqlFileStream(path,
                        (byte[])reader.GetValue(1),FileAccess.Read,FileOptions.SequentialScan, 0);                        
                    lstResults.Items.Clear();
                    int length = (int) stream.Length;
                    byte[] contents = new byte[length];
                    stream.Read(contents,0,length);                     
                    string results = System.Text.Encoding.ASCII.GetString(contents);
                    lstResults.Items.Add(results);
                    stream.Close();
                }
            }
            transaction.Commit();
        }



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. MSSQL2008 - Pyodbc - पिछला SQL एक प्रश्न नहीं था

  2. SQL सर्वर मास्टर डेटाबेस को पुनर्स्थापित करना

  3. sp_MSForEachDB फ़ंक्शन के भीतर साइड-इफ़ेक्टिंग ऑपरेटर का अमान्य उपयोग

  4. कई कॉलम से न्यूनतम मान चुनने का सबसे अच्छा तरीका क्या है?

  5. प्रोग्रामेटिक रूप से SQL सर्वर कनेक्शन का परीक्षण करने का सबसे अच्छा तरीका क्या है?