Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Creating MySQL connection in C# (http://www.programmingforums.org/showthread.php?t=4113)

OpenLoop May 26th, 2005 8:41 AM

Creating MySQL connection in C# <Solved>
 
hi, i'm using visual studio .net 2003 and i'm having trouble connecting my C# application to the MySQL database (testdb) on my computer using ODBC.
While creating the connection, it asks me for a DNS name or file or something :confused: so my question is how to create a DNS name for the MySQL database?

i appreciate any help

Pizentios May 26th, 2005 8:57 AM

just put in the ip address of the computer that you want it to connect to. Or if you have a domain name, you could get the domain host to put a entry into their control files for your db server. It's just as easy to just put the ip in though. If you are just doing some testing and have the mysql server setup on your development box, you can also just put in localhost as the DNS. However, if you plan on giving this program out, localhost won't work...simply cause when the other users (on different computers) would be looking at their boxes for the db server and your program would error out. Your best bet is the ip.

Suggestion, make a configure file, or dialog that lets you change the DNS settings for the mysql connection. that way if the location or ip of the mysql server ever changes, it will make your life alot easyer (as you will only have to change the DNS once instead of a whole bunch of times inside your code), and you won't have to re-compile your code.

OpenLoop May 27th, 2005 10:05 AM

Thanks pizentios but putting the ip didn't work. I guess it's a bigger problem than i thought so i'll hit the books :D

Pizentios May 27th, 2005 10:19 AM

hey,


here's some code for you to look at:

:

using System;
 using System.Data;
 
 using ByteFX.Data.MySqlClient;
 
 public class ConnectToDatabase {
 
                public static void Main(string [] args) {
 
                                string username = args[0];
                                string password = args[1];
 
                            string connectionString = string.Format("Server=localhost;" +
                  "Database=monodn;User ID={0};Password={1};", username, password);
 
                                MySqlConnection conn = null;
                            DataSet monodn = new DataSet("monodn");
 
                                try {
                                            conn = new MySqlConnection(connectionString);
                                            conn.Open();
 
                                            MySqlDataAdapter adapter = new MySqlDataAdapter(
                                                            "select * from book", conn);
                                            MySqlCommandBuilder cmdBuilder =
                                                            new MySqlCommandBuilder(adapter);
 
                                            adapter.Fill(monodn);
                                            conn.Close();
 
                                            DataRow [] rows = monodn.Tables[0].Select("id = 1");
                                            DataRow row = rows[0];
 
                                            DateTime pubDate = (DateTime)row["pubdate"];
                                            Console.WriteLine("original pubdate: {0}", pubDate);
                                            row["pubdate"] = pubDate.AddDays(15);
                                            Console.WriteLine("new pubdate:          {0}",
                                                            monodn.Tables[0].Rows[0]["pubdate"]);
 
                                            conn.Open();
                                            adapter.Update(monodn);
                                            conn.Close();
 
                                } catch (Exception e) {
                                            Console.Error.WriteLine(e);
                                } finally {
                                            if (conn != null && conn.State == ConnectionState.Open) {
                                                            conn.Close();
                                            }
                                }
                }
 }


Might help you figure out your problems?

Infinite Recursion May 27th, 2005 2:02 PM

Ahhh. I remember that code from the Element days :)

OpenLoop May 28th, 2005 12:46 PM

it worked :D
thanks P.


All times are GMT -5. The time now is 4:21 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC