Hi, guys. I'm working on a program in C# (I'm a beginner). I created a database with mySql on PHPmyadmin and I want to connect this database with my program.
After the connection I have to insert, update, delete and view all the datas, but I have a problem: the connection doesn't work.
I post here my code:
I have errors in lines 28, 80, 91, 101, 110, 111.
May you help me, please? Thank you!
After the connection I have to insert, update, delete and view all the datas, but I have a problem: the connection doesn't work.
I post here my code:
using System; using System.Drawing; using System.Windows.Forms; using Microsoft.Office.Interop.Excel; using System.Data.SqlClient; using MySql.Data.MySqlClient; namespace Agility { /// <summary> /// Description of nuovaGara. /// </summary> public partial class formNuovaGara : Form { public formNuovaGara() { // // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent(); // // TODO: Add constructor code after the InitializeComponent() call. // } public static string StringaConnessione = "Data Source=localhost;Database=agility;userid=root;password='';"; public static MySqlConnection Connessione = new MySqlConnection(StringaConnessione); void DataGridView1CellContentClick(object sender, DataGridViewCellEventArgs e) { } void MenuBtnClick(object sender, EventArgs e) { FormMenu m=new FormMenu(); this.Hide(); m.Show(); } void CreaExcelBtnClick(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application Excel= new Microsoft.Office.Interop.Excel.Application(); Workbook wb=Excel.Workbooks.Add(XlSheetType.xlWorksheet); Worksheet ws=(Worksheet)Excel.ActiveSheet; Excel.Visible=true; ws.Cells[1,1]="Nome Gara"; ws.Cells[1,2]="Giudice"; ws.Cells[1,3]="Localit?"; ws.Cells[1,4]="Data"; ws.Cells[1,5]="Tps Opm"; ws.Cells[1,6]="Tpm Opm"; ws.Cells[1,7]="Tps Tot"; ws.Cells[1,8]="Tpm Tot"; for(int i=2;i<=dataGridView1.Rows.Count;i++){ for(int j=2;j<8;j++){ ws.Cells[i,j]=dataGridView1.Rows[i-2].Cells[j-1].Value; } } } void PrintDocument1PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { Bitmap bm=new Bitmap(this.dataGridView1.Width,this.dataGridView1.Height); //dataGridView1.DrawToBitmap(bm, new Rectangle(0,0,this.dataGridView1.Width,this.dataGridView1.Height)); e.Graphics.DrawImage(bm,10,10); } void StampaBtnClick(object sender, EventArgs e) { printDocument1.Print(); } void InserisciBtnClick(object sender, EventArgs e) { Connessione.Open(); SQLDataAdapter SDA=new SqlDataAdapter("INSERT INTO GARA(nome_gara,giudice,localit?,data,tpsopm,tpmopm,tpstot,tpmtot)VALUES('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"','"+textBox5.Text+"','"+textBox6.Text+"','"+textBox7.Text+"','"+textBox8.Text+"')",Connessione); SDA.SelectCommand.ExecuteNonQuery(); Connessione.Close(); MessageBox.Show("Dati salvati correttamente!"); } void ModificaBtnClick(object sender, EventArgs e) { Connessione.Open(); SQLDataAdapter SDA=new SqlDataAdapter("UPDATE INTO GARA set nome_gara='"+textBox1.Text+"',giudice='"+textBox2.Text+"',localit?='"+textBox3.Text+"',data='"+textBox4.Text+"',tpsopm='"+textBox5.Text+"',tpmopm='"+textBox6.Text+"',tpstot='"+textBox7+"',tpmtot='"+textBox8.Text+"')VALUES'"+textBox1.Text"','"+textBox2.Text"','"+textBox3.Text"','"+textBox4.Text"','"+textBox5.Text"','"+comboBox1.Text"','"+comboBox2.Text"','"+textBox8.Text"')",Connessione); SDA.SelectCommand.ExecuteNonQuery(); Connessione.Close(); MessageBox.Show("Dati modificati correttamente!"); } void CancellaBtnClick(object sender, EventArgs e) { SQLDataAdapter SDA=new SqlDataAdapter("DELETE INTO GARA(gara,cane,conduttore,microchip,razza,taglia,club,categoria)VALUES'"+textBox1.Text"','"+textBox2.Text"','"+textBox3.Text"','"+textBox4.Text"','"+textBox5.Text"','"+comboBox1.Text"','"+comboBox2.Text"','"+textBox8.Text"')",Connessione); SDA.SelectCommand.ExecuteNonQuery(); Connessione.Close(); MessageBox.Show("Dati cancellati correttamente!"); } void MostraBtnClick(object sender, EventArgs e) { Connessione.Open(); SQLDataAdapter SDA=new SqlDataAdapter("SELECT * FROM Cane",Connessione); DataTable DATA= new DataTable(); SDA.Fill(DATA); dataGridView1.DataSource=DATA; Connessione.Close(); } } }
I have errors in lines 28, 80, 91, 101, 110, 111.
May you help me, please? Thank you!