vendredi 24 juin 2016

How To insert data into SQL Sever with C# without using the database wizard(visual studio)

I'm having trouble inserting data into my table called "client" in my database called "vet".

I'm trying to do the connection manually as opposed to using the database wizard. I want to do it this way so that i can hide the majority of my code in a separate class.

I have a feeling I have the logic for this all wrong as I'm not making use of a data set, I was hoping someone could point me in the right direction.

This is for a school assignment and not for real world use. I have read a number of similar posts but am still unable to come to a solution.

public void CommitToDatabase()
{
    using (var con = new SqlConnection(CLSDatabaseDetails.GlobalConnectionString))
    {
        DataTable client = new DataTable();
        DataSet ds = new DataSet();
        string commandString = "SELECT * FROM client";
        SqlCommand cmd = new SqlCommand(commandString, con);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(client);
        DataRow newClientRow = ds.Tables["client"].NewRow();
        newClientRow["ClientID"] = ClientID;
        newClientRow["FirstName"] = FirstName;
        newClientRow["LastName"] = LastName;
        newClientRow["Phone"] = PhoneNumber;
        newClientRow["CAddress"] = Address;
        newClientRow["Email"] = Email;
        newClientRow["CUsername"] = Username;
        newClientRow["CPassword"] = Password;
        client.Rows.Add(newClientRow);
        da.Update(client);
    }
}

Aucun commentaire:

Enregistrer un commentaire