How to connect to a MySQL database using the mysql .NET connector in C#.
Download and install the MySql.Net connector.
Add a reference to it in your project, that is - click on "Project'->'Add reference', on your '.net assembly browser' tab and choose the library, make sure you're adding the 'Assembly' (the type is shown in the window just after you select the file).
And here is sample code from a working program:
using MySql.Data.MySqlClient;
namespace my_namespace_whatever
{
public partial class MainForm : Form
{
private MySqlConnection MyConn;
private MySqlCommand MyCom;
private MySqlDataAdapter MyDR;
private string ConnStr;
public MainForm()
{
InitializeComponent();
// now the interesting part
ConnStr = "SERVER=localhost;DATABASE=my_database;UID=my_user;PWD=my_password;";
MyConn = new MySqlConnection(ConnStr);
if (MyConn.State == ConnectionState.Closed)
{
MyConn.Open();
}
if (MyConn.State != ConnectionState.Open)
{
MessageBox.Show("Could not connect");
}
// A.S.O
}
}
miercuri, 30 noiembrie 2011
MySql .NET connector example
system.data.odbc.odbcexception: error [im002] [microsoft][odbc driver manager] data source name not found and no default driver specified
Trying to use MySQL ODBC driver with a C# program to make a connection to the MySQL database.
Downloaded the latest MySQL ODBC 5.1 Driver from mysql's website, yes, the right version (32-bit for my Windows, check your version - you might need the 64-bit one)
Installed driver.
Created DSN in User DSN (control panel->administrative tools->data sources, and NO, it is NOT needed to create the ODBC Data Source in your System DSN or other, just in User DSN).
Yes, entered correct server name, username, password.
Now going to my C# program, at Connection.Open() - i get the following error: system.data.odbc.odbcexception: error [im002] [microsoft][odbc driver manager] data source name not found and no default driver specified
WHAT in God's name is the problem????
I must say that i've installed and used the mysql odbc connector in the past too, and the connection went smoothly then..
Yes, the problem is the connection string. This is what i had:
Banged head, ripped clothes off, cut veins, and then success: copy and paste the EXACT ODBC driver name, and now i have this:
Please notice the string at 'driver=', it must be the same as the ODBC Driver Name, in my case: MYSQL ODBC 5.1 Driver
You find the driver name under Control Panel -> Administrative Tools -> Data Sources (ODBC) , in the 'driver' column (obvious, eh?), next to my own MySQL ODBC Data Source.
Pheewww, that was a bugger.
PS: Oh, and NO, you don't need to add any references to the mysql library either in your project. This is the ODBC method, and you are not using any mysql objects/references here. You must include though your System.Data. If you want to go the mysql .net connection method, search for another post.
Downloaded the latest MySQL ODBC 5.1 Driver from mysql's website, yes, the right version (32-bit for my Windows, check your version - you might need the 64-bit one)
Installed driver.
Created DSN in User DSN (control panel->administrative tools->data sources, and NO, it is NOT needed to create the ODBC Data Source in your System DSN or other, just in User DSN).
Yes, entered correct server name, username, password.
Now going to my C# program, at Connection.Open() - i get the following error: system.data.odbc.odbcexception: error [im002] [microsoft][odbc driver manager] data source name not found and no default driver specified
WHAT in God's name is the problem????
I must say that i've installed and used the mysql odbc connector in the past too, and the connection went smoothly then..
Yes, the problem is the connection string. This is what i had:
ConnStr = "DRIVER={MYSQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=my_database;UID=my_username;PWD=my_password;OPTION=1";
Banged head, ripped clothes off, cut veins, and then success: copy and paste the EXACT ODBC driver name, and now i have this:
ConnStr = "DRIVER={MYSQL ODBC 5.1 Driver};SERVER=localhost;DATABASE=my_database;UID=my_username;PWD=my_password;OPTION=1";
Please notice the string at 'driver=', it must be the same as the ODBC Driver Name, in my case: MYSQL ODBC 5.1 Driver
You find the driver name under Control Panel -> Administrative Tools -> Data Sources (ODBC) , in the 'driver' column (obvious, eh?), next to my own MySQL ODBC Data Source.
Pheewww, that was a bugger.
PS: Oh, and NO, you don't need to add any references to the mysql library either in your project. This is the ODBC method, and you are not using any mysql objects/references here. You must include though your System.Data. If you want to go the mysql .net connection method, search for another post.
luni, 28 noiembrie 2011
Responsive design
WTF is 'responsive design'?
In my search for an answer i've bumped across this page, which pretty much says it all:
http://designmodo.com/responsive-design-examples/
In my search for an answer i've bumped across this page, which pretty much says it all:
http://designmodo.com/responsive-design-examples/
marți, 15 noiembrie 2011
Javascript URL regex
Here's how i check and replace URL patterns in Javascript, with this neat URL regexp pattern:
Huh? what do you think about this regular expression?
var URL3_pat = /http:\/\/([\w\d\.\-]+)\.([a-z]{2,8})(($)|([\/\?\#]+$)|(([\/\?\#]+[\w\d\:\@\%\/\;\~\_\?\\\+\-\=\\\\\.\&\,]+)))/m;
r = t.replace(URL3_pat, '<a href="$&" target="out">$&</a>');
Huh? what do you think about this regular expression?
Etichete:
Javascript,
pattern,
Regex,
regexp,
regular expression,
syntax,
textarea,
URL
vineri, 28 octombrie 2011
TinyMCE add content - add HTML
Here's the plain simple method to insert some HTML into your tinyMCE *textarea*
Where id_blog_body is your textarea's ID attribute.
html = '<br><br><a href="gourl"><img src="someimageurl" border=0 alt=""></a>';
document.getElementById('#elm1').tinymce().execCommand('mceInsertContent',false,html);
Where id_blog_body is your textarea's ID attribute.
<textarea id="elm1" class="tinymce"></textarea>
Save your form before you go (onbeforeunload)
So, the onbeforeunload action.
This doesn't seem to have too many options. It acts differently in the different browsers, and no, in Firefox 4 or later you cannot change in any way the browser's security confirmation box ("Note that in Firefox 4 and later the returned string is not displayed to the user." - https://developer.mozilla.org/en/DOM/window.onbeforeunload ). In Firefox, the confirmation box invariably says: "this page is asking you to confirm that you want to leave data you have entered may not be saved".
So, once the onbeforeunload action is set, one can just show the browser's default confirmation dialog box or add another (underline - another) confirmation or other kind of dialog box. One can't remove or skip the browser's default confirmation box.
Here's my code:
I'm using the global global_form_changed variable so that i can either ask the user to save the content or not ask anything, depending on whether the user has actually made any changes to the form or not.
I'm modifying my global_form_changed like this:
On every input within the form, i'm setting the onchange attribute with this:
'
Of course there are various other ways to see if the form has actually changed or not.
This doesn't seem to have too many options. It acts differently in the different browsers, and no, in Firefox 4 or later you cannot change in any way the browser's security confirmation box ("Note that in Firefox 4 and later the returned string is not displayed to the user." - https://developer.mozilla.org/en/DOM/window.onbeforeunload ). In Firefox, the confirmation box invariably says: "this page is asking you to confirm that you want to leave data you have entered may not be saved".
So, once the onbeforeunload action is set, one can just show the browser's default confirmation dialog box or add another (underline - another) confirmation or other kind of dialog box. One can't remove or skip the browser's default confirmation box.
Here's my code:
global_form_changed = true;
window.onbeforeunload = function (e) {
e = e || window.event;
ret = global_form_changed?"You have not saved your contents. Are you sure you want to leave?":null;
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = ret;
}
// For Safari
return ret;
};
I'm using the global global_form_changed variable so that i can either ask the user to save the content or not ask anything, depending on whether the user has actually made any changes to the form or not.
I'm modifying my global_form_changed like this:
On every input within the form, i'm setting the onchange attribute with this:
'
onchange="global_frm_edit_changed = true"'Of course there are various other ways to see if the form has actually changed or not.
marți, 27 septembrie 2011
Website with subdomains (addition about cookies and sessions)
Oh, and yes,
If you want the session or the cookies to be working over all of the domains, you'd have to specify this for the cookies or for the session.
This is how to do it in PHP. It works for named domain names, as well as for IP's and also for localhost or LAN server names, but not for composed TLD's (e.g. works for example.com, but not for example.co.uk):
This is it!
Questions? Comments?
If you want the session or the cookies to be working over all of the domains, you'd have to specify this for the cookies or for the session.
This is how to do it in PHP. It works for named domain names, as well as for IP's and also for localhost or LAN server names, but not for composed TLD's (e.g. works for example.com, but not for example.co.uk):
if ( strpos($_SERVER["SERVER_NAME"], ".")!==false && preg_match("/([^0-9\.])/i", $_SERVER["SERVER_NAME"]) ) // check if there are any dots inside the server name and also letters (excludes IP's and LAN server names)
{
$p = preg_split("/\./i", $_SERVER["SERVER_NAME"], -1, PREG_SPLIT_NO_EMPTY); // split the domain name by dots (www, example, com)
while ( count($p)>2 ) array_shift($p); // push out all elements except the last two (example and com)
$COOKIE_PARAMS["dom"] = ".".join(".", $p); // this is what we need ".example.com"
}
else $COOKIE_PARAMS["dom"] = $_SERVER['SERVER_NAME']; // the full name of the server for IP's and local server names
$COOKIE_PARAMS["path"] = "/"; // the path under which the cookie is available - we'll use this also for the session
session_set_cookie_params (0, $COOKIE_PARAMS["path"], $COOKIE_PARAMS["dom"]);
// lifetime =0 (until browser is closed),
// path=$COOKIE_PARAMS["path"] (path under which the session is available),
// and the important part: domain=$COOKIE_PARAMS["dom"] (".example.com" so it works for all possible subdomains)
setcookie("mycookie", "myvalue", time()+30*60, $COOKIE_PARAMS["path"], $COOKIE_PARAMS["dom"], false); // set a cookie "mycookie" with the value "myvalue", lifetime 30 minutes, available under the path $COOKIE_PARAMS["path"]
This is it!
Questions? Comments?
Abonați-vă la:
Postări (Atom)