using System; using System.IO; using System.Data; using System.Data.SqlClient;
class MainClass { public static void Main(string[] args) { // connect SqlConnection con = new SqlConnection("server=ARNEPC2\\ARNEPC2RUN;Integrated Security=SSPI;database=TestMSDE"); con.Open();
// create table SqlCommand cre = new SqlCommand("CREATE TABLE imgtest (id INTEGER PRIMARY KEY,img IMAGE)", con); cre.ExecuteNonQuery();
// file -> byte array Stream imgfile = new FileStream(@"C:\blue.jpg", FileMode.Open); byte[] imgdata = new byte[(int)imgfile.Length]; imgfile.Read(imgdata, 0, imgdata.Length); imgfile.Close();
// insert SqlCommand ins = new SqlCommand("INSERT INTO imgtest VALUES(@id,@img)", con); ins.Parameters.Add("@id", SqlDbType.Int); ins.Parameters.Add("@img", SqlDbType.Image); ins.Parameters["@id"].Value = 1; ins.Parameters["@img"].Value = imgdata; ins.ExecuteNonQuery();
// select SqlCommand sel = new SqlCommand("SELECT img FROM imgtest WHERE id = @id", con); sel.Parameters.Add("@id", SqlDbType.Int); sel.Parameters["@id"].Value = 1; byte[] imgdata2 = (byte[])sel.ExecuteScalar();
// drop table SqlCommand drp = new SqlCommand("DROP TABLE imgtest", con); drp.ExecuteNonQuery();
using System; using System.Threading; using System.Data; using ByteFX.Data.MySqlClient;
public class PicTestThread { private int n; private int npics; private int picsize; public PicTestThread(int n, int npics, int picsize) { this.n = n; this.npics = npics; this.picsize = picsize; } public void Run() { PicTest.CheckN(n, npics, picsize); } }
public class PicTest { private const int N = 10000; public static void CreateTable(int npics, int picsize) { MySqlConnection con = new MySqlConnection("Database=Test;Data Source=localhost;User Id=;Password="); con.Open(); MySqlCommand cre = new MySqlCommand("CREATE TABLE pics (id INTEGER PRIMARY KEY, pic MEDIUMBLOB)", con); cre.ExecuteNonQuery(); MySqlCommand ins = new MySqlCommand("INSERT INTO pics VALUES (@id, @pic)", con); ins.Parameters.Add("@id", MySqlDbType.Int); ins.Parameters.Add("@pic", MySqlDbType.MediumBlob); for(int i = 0; i < npics; i++) { ins.Parameters["@id"].Value = i; byte[] data = new Byte[picsize]; for(int j = 0; j < picsize; j++) { data[j] = (byte)((i + j) % 256); } ins.Parameters["@pic"].Value = data; ins.ExecuteNonQuery(); } con.Close(); } public static void CheckOne(int id, int picsize) { MySqlConnection con = new MySqlConnection("Database=Test;Data Source=localhost;User Id=;Password="); con.Open(); MySqlCommand sel = new MySqlCommand("SELECT pic FROM pics WHERE id = " + id, con); byte[] data = (byte[])sel.ExecuteScalar(); if(data.Length != picsize) { Console.WriteLine("picture #" + id + " corrupted"); } for(int j = 0; j < data.Length; j++) { if(((id + j) % 256) != data[j]) { Console.WriteLine("picture #" + id + " corrupted"); } } con.Close(); } public static void DropTable() { MySqlConnection con = new MySqlConnection("Database=Test;Data Source=localhost;User Id=;Password="); con.Open(); MySqlCommand drp = new MySqlCommand("DROP TABLE pics", con); drp.ExecuteNonQuery(); con.Close(); } public static void CheckN(int n, int npics, int picsize) { Random rng = new Random(); for(int i = 0; i < n; i++) { int id = rng.Next(npics); CheckOne(id, picsize); } } public static void CheckTest(int nthreads, int npics, int picsize) { long t1 = DateTime.Now.Ticks; PicTestThread[] ptt = new PicTestThread[nthreads]; Thread[] thr = new Thread[nthreads]; for(int i = 0; i < thr.Length; i++) { ptt[i] = new PicTestThread(N/nthreads, npics, picsize); thr[i] = new Thread(new ThreadStart(ptt[i].Run)); } for(int i = 0; i < thr.Length; i++) { thr[i].Start(); } for(int i = 0; i < thr.Length; i++) { thr[i].Join(); } long t2 = DateTime.Now.Ticks; Console.WriteLine(nthreads + " conc reqs, " + npics + " pics in db, " + picsize/1000 + " KB pics => " + N/((t2 - t1) / 1000000.0) + " pics/sec"); } public static void Main(string[] args) { int[] npics = { 250, 500, 1000 }; int[] picsize = { 50000 /*, 100000, 200000 */ }; int[] nthreads = { 10, 20, 40 }; for(int i = 0; i < npics.Length; i++) { for(int j = 0; j < picsize.Length; j++) { CreateTable(npics[i], picsize[j]); for(int k = 0; k < nthreads.Length; k++) { CheckTest(nthreads[k], npics[i], picsize[j]); } DropTable(); } } } }
MySqlCommand ins = new MySqlCommand( "INSERT INTO tblFiles (FileName,FileSize,ContentType,FileData) VALUES(?FileName,?FileSize,?ContentType,?FileData)", con);
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.