×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Can't insert data into MySql from C# code

Can't insert data into MySql from C# code

Can't insert data into MySql from C# code

(OP)
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. My connection appears to work when I tried just to connect then disconnect. 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).  For now, I just put in two characters to insert-  me  

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

RE: Can't insert data into MySql from C# code

You have two dots between `teleyapper` and `callees`. If those are the database and table names respectively, then there should only be one dot there.

RE: Can't insert data into MySql from C# code

(OP)
Thanks for your help.  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(false);
            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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close