0% found this document useful (0 votes)
45 views3 pages

"Localhost": Public Void String

This code defines methods to load, insert, and update product data from a MySQL database table called "product" using ADO.NET. The loadProduct method retrieves all records from the product table and adds them to a ListView control. The insert and update methods insert a new record or update an existing record in the product table based on values from TextBox and DateTimePicker controls, connecting to the database using a connection string.

Uploaded by

Dfunz Wilphen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views3 pages

"Localhost": Public Void String

This code defines methods to load, insert, and update product data from a MySQL database table called "product" using ADO.NET. The loadProduct method retrieves all records from the product table and adds them to a ListView control. The insert and update methods insert a new record or update an existing record in the product table based on values from TextBox and DateTimePicker controls, connecting to the database using a connection string.

Uploaded by

Dfunz Wilphen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

String server = "localhost";

public void loadProduct()


{
string MyConString = "SERVER=" + server + ";" +
"DATABASE=csharpdb;" +
"UID=root;" +
"PASSWORD=;";

MySqlConnection mysqlCon = new MySqlConnection(MyConString);

try
{
String sstatement = "SELECT * FROM product";
MySqlCommand sCommand = new MySqlCommand(sstatement, mysqlCon);
mysqlCon.Open();

MySqlDataReader mysqlReader = sCommand.ExecuteReader();

ListView1.Items.Clear();
while (mysqlReader.Read())
{

ListView1.Items.Add(mysqlReader.GetInt32(0).ToString());
int index = ListView1.Items.Count - 1;
ListView1.Items[index].SubItems.Add(mysqlReader.GetString(1));
ListView1.Items[index].SubItems.Add(mysqlReader.GetString(2));
ListView1.Items[index].SubItems.Add(mysqlReader.GetDateTime(3).ToShortDateString());
ListView1.Items[index].SubItems.Add(mysqlReader.GetString(4));

}
mysqlCon.Close();
}
catch (Exception sException)
{
MessageBox.Show(sException.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}

public void insert()


{
string MyConString = "SERVER=" + server + ";" +
"DATABASE=csharpdb;" +
"UID=root;" +
"PASSWORD=;";

MySqlConnection mysqlCon = new MySqlConnection(MyConString);

String sstatement = "INSERT INTO product(id, name,category,date,price) values" +


"('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" +
String.Format("{0:yyyy-MM-dd}", dateTimePicker1.Value) +"'," + TextBox4.Text + " )";

MySqlCommand sCommand = new MySqlCommand(sstatement, mysqlCon);


mysqlCon.Open();

try
{
sCommand.ExecuteNonQuery();
}
catch (Exception excp)
{
Exception myExcp = new Exception("Could not add user. Error: " +
excp.Message, excp);
throw (myExcp);
}

public void update()


{
string MyConString = "SERVER=" + server + ";" +
"DATABASE=csharpdb;" +
"UID=root;" +
"PASSWORD=;";
MySqlConnection mysqlCon = new MySqlConnection(MyConString);

String sstatement = "UPDATE product SET " +


"name = '" + TextBox2.Text + "'," +
"category = '" + TextBox3.Text + "', " +
"date = '" + String.Format("{0:yyyy-MM-dd}", dateTimePicker1.Value) + "', " +
"price = " + TextBox4.Text + " WHERE id = " + TextBox1.Text;

MySqlCommand sCommand = new MySqlCommand(sstatement, mysqlCon);


mysqlCon.Open();

try
{
sCommand.ExecuteNonQuery();
}
catch (Exception excp)
{
Exception myExcp = new Exception("Could not add user. Error: " +
excp.Message, excp);
throw (myExcp);
}

You might also like