Update binary in SQL Server using OLEDBCommand objects
Author Zhu Erlin uses OLEDBConnection / OLEDBCommand in ADO.NET to update the two-credit in SQL Server, the following is a detailed code presentation demo environment: Database Machine Name: S_Test Login Name: SA Password: 7890 Database Name DB_Test Next, create a table: CREATE TABLE TB_TEST (ID Int Id Id IDITENTITY (1), Photo Image, Constraint PK_TB_TEST Primary Key (ID)) 1. Save the file on the hard disk to the database (C #) // ------ -------------------------------------------------- - / / ---------------------------------------------------------------------------- ---------- / / The following example saves the C: /1.txt file to the TB_TEST table of the database / / ---------------- ---------------------------------------- // ------ -------------------------------------------------- --using System; using System.IO;? using System.Data; using System.Data.OleDb; class image_test {[STAThread] static void Main (string [] args) {try {// initialization and OleDbCommandOleDbConnection cn = new OleDbConnection OLEDBCONNECTION ("provider = sqloledb; server = s_test; user ID = sa; password = 7890; initial catalog = db_test"); OLEDBCOMMAND CMD = New OLEDBCOMMAND ("INSERT TB_TEST (Photo) VALUES (?)", Cn); // Open file filestream fs = new fileStream ("c: //1.txt", filemode.open, fileaccess.read; byte [] b = new byte [fs .Length]; fs.read (b, 0, b.length); fs.close (); // Open OLEDBPARAMETER PRM = New OLEDBPARETER ("@photo", oledbtype.varbinary, b.length,? ParameterDirection.input , False, 0, 0, NULL, DATAROWVERSION.CURRENT, B); cmd.Parameters.Add (PRM); cn.open (); // Execute IF (cmd.executenonury () == 1) console.writeline OK "); Elseconsole.writeline (" fail ") ;? cn.close ();} catch (exception ex) {console.writeLine (ex. measureage);}}}}}}}}} -------------------------------------------------- -------- // ---------------------------------------- ---------------- // The following example uses the id = in the TB_TEST table of the database