Created
November 3, 2012 22:20
-
-
Save pewerner/4009096 to your computer and use it in GitHub Desktop.
ActiveX Control for queryiing a MySql Database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Runtime.InteropServices; | |
using System.Collections; | |
using MySql.Data.MySqlClient; | |
//using System.Data; | |
using System.Linq; | |
using System.Threading; | |
using System.Text; | |
namespace LIMS | |
{ | |
[ | |
Guid("577EF7BB-7A8E-4DE3-9619-91BC697DEC3B"), | |
InterfaceType(ComInterfaceType.InterfaceIsDual), | |
ComVisible(true) | |
] | |
public interface ILIMSConnector | |
{ | |
[DispId(1)] | |
void setConnectionString(string server, | |
string database, string userID, | |
string password); | |
[DispId(2)] | |
string getData(string sqlQuery); | |
[DispId(3)] | |
void setData(string sqlQuery); | |
}; | |
[ | |
Guid("0467EBEB-F390-4430-AB76-BE3699331A73"), | |
ProgId("db.Connector"), | |
ClassInterface(ClassInterfaceType.None), | |
ComDefaultInterface(typeof(ILIMSConnector)), | |
ComVisible(true) | |
] | |
public class LIMSConnector : ILIMSConnector | |
{ | |
#region [ILIMSConnector implementation] | |
public static string connectionString; | |
public static string queryString; | |
public void setConnectionString(string server, string database, string userID, string password) | |
{ | |
connectionString = "SERVER=" + server + ";DATABASE=" + database + ";UID=" + userID + ";PASSWORD=" + password + ";"; | |
} | |
public string getData(string sqlQuery) | |
{ | |
queryString = sqlQuery; | |
MySqlConnection connection = new MySqlConnection(connectionString); | |
MySqlCommand command = connection.CreateCommand(); | |
command.CommandText = queryString; | |
connection.Open(); | |
string returndata = ""; | |
MySqlDataReader reader = command.ExecuteReader(); | |
while (reader.Read()) | |
{ | |
for (int i = 0; i < reader.FieldCount; i++) | |
{ | |
returndata = returndata + reader.GetValue(i).ToString() + ","; | |
} | |
} | |
connection.Close(); | |
returndata = returndata.Substring(0, returndata.Length - 1); | |
return returndata; | |
} | |
public void setData(string sqlQuery) | |
{ | |
//--Run Query in a new thread | |
//queryString = sqlQuery; | |
//ThreadStart sqlJob = new ThreadStart(sqlThread); | |
//Thread thread = new Thread(sqlJob); | |
//thread.Start(); | |
MySqlConnection connection = new MySqlConnection(connectionString); | |
MySqlCommand command = connection.CreateCommand(); | |
//command.CommandText = "insert into absorbance(elisa_barcode, phage_barcode, order_id, absorbace) values('ex100','pp,100','10001',0.1234);"; | |
command.CommandText = sqlQuery; | |
connection.Open(); | |
command.ExecuteNonQuery(); | |
connection.Close(); | |
} | |
public static void sqlThread() | |
{ | |
//MySqlConnection connection = new MySqlConnection(connectionString); | |
//MySqlCommand command = connection.CreateCommand(); | |
//command.CommandText = queryString; | |
//connection.Open(); | |
//command.ExecuteNonQuery(); | |
//connection.Close(); | |
} | |
#endregion | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment