从表c#中删除行

时间:2013-12-02 20:07:13

标签: c# sqlanywhere

我是c#的新手,我正在试图弄清楚我桌子的删除和更新部分。我得到了插入部分,因为在点击按钮之前我没有尝试选择表格中的任何内容。但是,通过删除和更新,我对查询如何与表格中的选定行配对感到困惑。如果有人能指出我正确的方向,这将是伟大的。我在devexpress中使用数据集和GridControl。我还必须使用按钮来执行事件,并且不会使用网格中的命令字段。我正在努力使用参数进行插入。

我的清单:

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();
    }
}

执行操作的服务类:

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();
    }
}

1 个答案:

答案 0 :(得分:0)

只需将INSERT sql语句替换为UPDATE

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

删除:

DELETE FROM table_name
WHERE some_column=some_value;