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
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
RE: Can't insert data into MySql from C# code
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