質問

I'm brand new to c# and am trying to figure out the delete and update portion of my table. I get the insert portion because I am not trying to select anything in my table prior to clicking a button. With the delete and update however, I am confused as to how a query pairs up to the selected row in my table. If anyone could point me in the right direction that would be great. I am using a dataset and GridControl in devexpress. I also have to make use of buttons to perform the events and will not be using the command fields within the grid. And I'm working on making my insert with parameters.

My list:

public partial class PatientList : XtraForm
{
    public PatientList()
    {
        InitializeComponent();
    }

    private void PatientList_Load(object sender, EventArgs e)
    {
        SAConnection conn = new SAConnection("dsn={SQL Anywhere 10};uid=dba;pwd=sql;databasefile=C:\\Users\\Kbaker1\\Desktop\\Training1.db;");
        SADataReader rdr = null;
        string Query = "SELECT * FROM patient";
        SADataAdapter da = new SADataAdapter(Query, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        grdList.DataSource = ds.Tables[0];

        try
        {
            conn.Open();
            SACommand cmd = new SACommand(Query, conn);
            rdr = cmd.ExecuteReader();
        }
        finally
        {
            if (rdr != null)
            {
                rdr.Close();
            }

            if (conn != null)
            {
                conn.Close();
            }
        }
    }

    private void btnNewPatient_Click(object sender, EventArgs e)
    {
        Edit editPat = new Edit();
        editPat.Show();
    }

    private void btnEditPatient_Click(object sender, EventArgs e)
    {
        Edit editPat = new Edit();
        editPat.Show();          
    }

    private void btnDeletePatient_Click(object sender, EventArgs e)
    {
        PatientService ps = new PatientService();
        ps.DeletePatient();
    }

    private void btnClose_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

The service class performing the operations:

public class PatientService 
{
    public void DataAccess()
    {

    }

    public void CreatePatient(Patient patient)
    {
        SAConnection conn = new SAConnection();
        SACommand cmd = new SACommand();
        conn.ConnectionString = ("dsn={SQL Anywhere 10};uid=dba;pwd=sql;DBF=C:\\Users\\Kbaker1\\Desktop\\Training1.db;");
        conn.Open();
        cmd.Connection = conn;
        cmd.CommandText = (@"INSERT INTO patient (patient_id, first_name, last_name, address, city, state, zipcode, phone, classification_id) 
        VALUES ('"
        + patient.PatientID + "','"
        + patient.FirstName + "','"
        + patient.LastName + "','"
        + patient.Address + "','"
        + patient.City + "','"
        + patient.State + "','"
        + patient.ZipCode + "','"
        + patient.Phone + "','"
        + patient.ClassificationID + "'); ");
        cmd.ExecuteNonQuery();
        conn.Close();
    }

    public void UpdatePatient()
    {

    }

    public void DeletePatient()
    {
        SAConnection conn = new SAConnection();
        conn.ConnectionString = ("dsn={SQL Anywhere 10};uid=dba;pwd=sql;DBF=C:\\Users\\Kbaker1\\Desktop\\Training1.db;");
        conn.Open();
        SACommand cmd = new SACommand("DELETE FROM patient WHERE patient_id = @patient_id");
        cmd.Connection = conn;
        cmd.ExecuteNonQuery();
        conn.Close();
    }
}
役に立ちましたか?

解決

Just replace the INSERT sql statement with UPDATE

UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

Delete:

DELETE FROM table_name
WHERE some_column=some_value;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top