Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't insert data into MySql from C# code

Status
Not open for further replies.

dghundt

Programmer
Apr 28, 2006
28
US
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
 
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.
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top