Question DB Connection

teoggiuro

New member
Joined
Nov 22, 2016
Messages
1
Programming Experience
Beginner
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:

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!
 
I think that giving us line numbers in such a large code snippet is a bit unreasonable when there are no line numbers actually shown against the code. I have fixed the formatting so that line numbers are displayed. Now, if you could just tell us what the errors actually are, rather than expecting us to guess.
 
Actually, I just noticed that you're using both SqlClient and MySqlClient. They don't mix and match. SqlClient is for SQL Server only and MySqlClient is for MySQL only. You said you're using MySQL so get rid of all the SqlClient stuff and use only MySqlClient. If you still have issues after doing that, please provide specific information on those issues, including error messages.
 

Latest posts

Back
Top Bottom