Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2003
    Posts
    18

    Can't insert data into mysql with C#!

    I am using C# in visual studio, trying to insert data into a column in MySql 5. I want to take the value of ampm which is am, and put it into my column ampm in my database called teleyapper.

    I can connect to the data with visual studio's odbc data set and insert and retrieve the data. I can go into the mysql querey browser and insert and retrieve the data. But for the life of me, I can't find the right syntax to insert and retieve data in my C# code. The latest syntax attempts are below. I don't catch an exception.

    in addition, ampm is a string in my code, but ampm column is varchar(2).

    I'd appreciate any help, here is my code!


    public void MysqlconnectInsert(string ampm)
    {
    MySql.Data.MySqlClient.MySqlConnection conn;
    string myConnectionString;


    myConnectionString = "server=localhost;uid=root;" +
    "pwd=password;database=teleyapper;";

    try
    {
    conn = new MySql.Data.MySqlClient.MySqlConnection();
    conn.ConnectionString = myConnectionString;
    conn.Open();

    MessageBox.Show(ampm); // this shows the value am

    MySqlCommand command = conn.CreateCommand();
    // command.CommandText = "INSERT INTO `teleyapper`..`callees`(ampm) values('me')";
    command.CommandText = "SELECT * FROM `teleyapper`..`callees`";
    MySqlDataReader reader = command.ExecuteReader();
    string retrieveampm = reader.GetString(0);
    MessageBox.Show(retrieveampm); \\ this message box never shows

    } //end of try
    catch (MySql.Data.MySqlClient.MySqlException ex)
    {
    Console.WriteLine(ex.Message);
    } //end of catch

  2. #2
    Join Date
    Oct 2003
    Posts
    18
    Turns out my mysql commands were wrong and some of my earlier syntax tries were ok. The code below works. Hope this helps someone else.


    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Text;
    using MySql.Data.MySqlClient;
    using System.Data;
    using System.Data.Odbc;
    using System.Text.RegularExpressions;

    namespace Reminder_Call1
    {
    static class Program
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(fals e);
    Application.Run(new Form1());

    } //end of Main()
    } //end of class Program


    //
    // Connects to Mysql
    //
    public class ConnectMysql
    {
    public void MysqlconnectInsert(string ampm)
    {
    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 callees (ampm) VALUES('me')";

    conn.ConnectionString = "server=localhost;uid=root;" +
    "pwd=password;database=teleyapper;";

    try
    {
    conn.Open();
    cmd.Connection = conn;
    cmd.CommandText = myquerystring;
    cmd.ExecuteNonQuery();
    MessageBox.Show("File Inserted into database successfully!",
    "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
    conn.Close();

    } //end of try

  3. #3
    Join Date
    Nov 2008
    Posts
    1

    Question getting data from MySQl database

    Hi. Thanks for the guidance in inserting data into the database. I am trying to read data from the database but with the code below:

    private void find_Click(object sender, EventArgs e)
    {

    string command = "SELECT f_name FROM address.addr where phone_number = '4';"; // i have a string number 4
    MySqlCommand cmd = new MySqlCommand();
    MySqlDataReader reader = null;
    try
    {
    reader = cmd.ExecuteReader();
    while (reader.Read())
    {
    MessageBox.Show(reader.GetString(0));
    }
    }
    catch (MySqlException ex)
    {
    MessageBox.Show(ex.Number.ToString());
    }
    finally
    {
    if (reader != null) reader.Close();
    }

    }

    However, i get the error "Connection must be valid and open". Please assist me.

  4. #4
    Join Date
    Oct 2003
    Posts
    18
    look at the above example.
    You have not specified a connection, nor opened the connection.

  5. #5
    Join Date
    Dec 2008
    Posts
    59

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •