Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data.Odbc;
- using System.IO;
- using System.Windows.Forms;
- namespace pictest
- {
- static class Program
- {
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- static void Main()
- {
- double _latitude = 5;
- double _longitude = 5;
- string _birdname = "pictest";
- // Get Database Connection
- string myConnString =
- @"Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=test; User=root;Password=root123;Option=3;";
- // Read Image into Byte Array from Filesystem
- byte[] photo = GetPhoto("C:\\Users\\Steve-Mac\\Desktop\\ball.png");
- var conn = new OdbcConnection(myConnString);
- // Construct INSERT Command
- OdbcCommand query = conn.CreateCommand();
- query.CommandText = @"INSERT INTO geolocation (longitude,latitude,birdname,userPic) VALUES(?,?,?,@Photo)";
- var parameter = new OdbcParameter("longitude", _longitude);
- parameter.OdbcType = OdbcType.Double;
- query.Parameters.Add(parameter);
- parameter = new OdbcParameter("latitude", _latitude);
- parameter.OdbcType = OdbcType.Double;
- query.Parameters.Add(parameter);
- parameter = new OdbcParameter("birdname", _birdname);
- parameter.OdbcType = OdbcType.VarChar;
- query.Parameters.Add(parameter);
- query.Parameters.Add("userPic", OdbcType.Image, photo.Length).Value = photo;
- // Open the Connection and INSERT the BLOB into the Database
- conn.Open();
- query.ExecuteNonQuery();
- conn.Close();
- }
- // **** Read Image into Byte Array from Filesystem
- public static byte[] GetPhoto(string filePath)
- {
- FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
- BinaryReader br = new BinaryReader(fs);
- byte[] photo2 = br.ReadBytes((int)fs.Length);
- br.Close();
- fs.Close();
- return photo2;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement