connecting to database

need help with inserting values into database, it seems it not connect

Page 1 of 1

9 Replies - 968 Views - Last Post: 30 January 2010 - 11:29 PM Rate Topic: -----

#1 randomsai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-January 10

connecting to database

Posted 30 January 2010 - 12:35 AM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {}
              private void hello(object sender,System.EventArgs e)
            {
               
                MySql.Data.MySqlClient.MySqlConnection MyCon = new MySql.Data.MySqlClient.MySqlConnection("server=localhost; Database = textinfo; user id=root; password=random;");
                MyCon.Open();
                MySql.Data.MySqlClient.MySqlCommand command = new MySql.Data.MySqlClient.MySqlCommand("INSERT INTO texting (phone_no) VALUES('666666')", MyCon);
                command.ExecuteNonQuery();
                MyCon.Close();
              

        }
    }
}


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 30 January 2010 - 11:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: connecting to database

#2 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: connecting to database

Posted 30 January 2010 - 01:21 AM

Well, there seems nothing is wrong with your code. Do you get any exception thrown? It might be something wth you database. Could you provide some more info about your database schema.
Was This Post Helpful? 0
  • +
  • -

#3 ThatThatGuy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 15-January 10

Re: connecting to database

Posted 30 January 2010 - 04:27 AM

Your code is perfectly right although i doubt that you are not putting your password... **** like you showed us in the code.....

Also i doubt that the phone_no field is numeric.... and your're putting a data of type string..


debug to see your code....


Click the below link if my reply helped you
Was This Post Helpful? 0
  • +
  • -

#4 TravisP  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 06-January 10

Re: connecting to database

Posted 30 January 2010 - 04:44 AM

It could be your internal security settings, try running VS as administrator (right click and go "open as"). Then see if the connection will work.
Was This Post Helpful? 0
  • +
  • -

#5 randomsai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-January 10

Re: connecting to database

Posted 30 January 2010 - 11:17 PM

no the password is a string, sorry bout that.
well just to text i set the value of the phone_no field to varchar, so i think that the string input into the table heading would have no effect.

i was watching the code and it seemed that this line stopped the debugging when stepped into:
static void Main(string[] args)
{}


what should i do, not to familiar with c#, better at php

also, the purpose of this is just to text that i can send values to the database through c#, as most of my project is php. i am using C# for the texting aspect of it. where a text message is simulated and the phone no and the message sent is stored in the database, just scraping the surface.


thanks to all who responded
Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: connecting to database

Posted 30 January 2010 - 11:21 PM

Since you are using a C# console application your code must be inside the Main tag.

Write in all function invocations inside of the Main function.

Try doing this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MySql.Data.MySqlClient.MySqlConnection MyCon = new MySql.Data.MySqlClient.MySqlConnection("server=localhost; Database = textinfo; user id=root; password=random;");
            MyCon.Open();
            MySql.Data.MySqlClient.MySqlCommand command = new MySql.Data.MySqlClient.MySqlCommand("INSERT INTO texting (phone_no) VALUES('test2')", MyCon);
            command.ExecuteNonQuery();
            MyCon.Close();
        }
    }
}


This post has been edited by stapia.gutierrez: 30 January 2010 - 11:23 PM

Was This Post Helpful? 1
  • +
  • -

#7 randomsai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-January 10

Re: connecting to database

Posted 30 January 2010 - 11:21 PM

i got it to work

This post has been edited by randomsai: 30 January 2010 - 11:24 PM

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: connecting to database

Posted 30 January 2010 - 11:22 PM

Code is "perfectly right," "nothing is wrong"? Can I have some of what you guys were having? :)

Look at the code again (note I added code tags). There's a Main function...then NOTHING INSIDE IT!!!

You must do something inside your Main function for the program to actually...well, do something.
Was This Post Helpful? 0
  • +
  • -

#9 randomsai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-January 10

Re: connecting to database

Posted 30 January 2010 - 11:25 PM

i did exactly that stapia.gutierrez, thanks
Was This Post Helpful? 0
  • +
  • -

#10 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: connecting to database

Posted 30 January 2010 - 11:29 PM

No problem, glad I could help. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1