Gemme billede i MySQL
Jeg er igang med at lave en lille ting som kun skal have til funktion at kunne uploade et billede, men jeg hænger fast ved at få læst billedet, og så overført det til databasenusing System;
using System.Text;
using System.Data;
using System.IO;
using System.Threading;
using System.Data.SqlClient;
namespace pic2mysql
{
class Program
{
static void Main(string[] args)
{
string zFile = "C://Users//Parx//Documents//Visual Studio 2010//Projects//pic2mysql//pic2mysql//bin//Debug//test.jpg";
if (File.Exists(zFile))
{
FileStream fs = new FileStream(zFile, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(fs);
byte[] BlobValue = reader.ReadBytes((int)fs.Length);
fs.Close();
reader.Close();
MySql.Data.MySqlClient.MySqlConnection conn;
MySql.Data.MySqlClient.MySqlCommand cmd;
conn = new MySql.Data.MySqlClient.MySqlConnection();
cmd = new MySql.Data.MySqlClient.MySqlCommand();
string myquerystring;
myquerystring = "INSERT INTO mrtg (varName, image) VALUES('blaaah', 'hvad faen skal der ind her?')";
conn.ConnectionString = "server=address;uid=root;pwd=password;database=dbname;";
try
{
conn.Open();
cmd.Connection = conn;
cmd.CommandText = myquerystring;
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception e)
{
Console.WriteLine("Error: " + e);
Thread.Sleep(5000000);
}
reader.Close();
Console.WriteLine("Done");
Thread.Sleep(1000);
}
else
{
Console.WriteLine("File not found");
Thread.Sleep(5000);
}
}
}
}