简体   繁体   English

如何从SQL Server数据库中删除表行?

[英]How to delete table rows from a SQL Server database?

I'm having trouble trying to delete rows. 我在尝试删除行时遇到了麻烦。

All rows are selected and displayed on a ListBox control. 选择所有行并将其显示在ListBox控件上。
Then, when calling the btnObrisi_Click event (Obrisi = delete), the selected item from the ListBox will be deleted. 然后,当调用btnObrisi_Click事件(Obrisi = delete)时,将从ListBox框中选择的项目删除。

The item is actually removed from the control, but the value is not deleted from the database. 该项目实际上已从控件中删除,但未从数据库中删除该值。

This is the code I'm using: 这是我正在使用的代码:

private string conString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";
private SqlConnection con;
private SqlCommand com;
private DataTable dt;
private SqlDataAdapter da;

private DataTable SelectAll()
{
    con = new SqlConnection(conString);
    com = new SqlCommand();
    dt = new DataTable();
    da = new SqlDataAdapter();

    com.Connection = con;
    com.CommandType = CommandType.Text;
    com.CommandText = "SELECT * FROM predmeti";
    da.SelectCommand = com;
    da.Fill(dt);

    return dt;
}

private void Obrisi(int id)
{
    con = new SqlConnection(conString);
    com = new SqlCommand();
    com.Connection = con;
    com.CommandType = CommandType.Text;
    com.CommandText = "DELETE FROM predmeti WHERE predmetID = @predmetID";
    com.Parameters.AddWithValue("@predmetID", id);

    try
    {
        con.Open();
        com.ExecuteNonQuery();
         MessageBox.Show("Obrisano");
    }
    catch(Exception ex)
    {
         MessageBox.Show(ex.Message);
    }
    finally
    {
         con.Close();
    }
}

private void Citaj()
{
    listBox1.ValueMember = "predmetID";
    listBox1.DisplayMember = "ime_predmeta";
    listBox1.DataSource = SelectAll();
}

private void btnOsvezi_Click(object sender, EventArgs e)
{
    Citaj();
}

private void Form1_Load(object sender, EventArgs e)
{
    Citaj();
}

private void btnObrisi_Click(object sender, EventArgs e)
{
    int id = Convert.ToInt32(listBox1.SelectedValue);
    Obrisi(id);
    Citaj();
}

When you have a database file in your project and then you build the project, the database file could be copied from the root project folder into the output (bin\\debug or bin\\release) folder. 当项目中有数据库文件然后构建项目时,可以将数据库文件从根项目文件夹复制到输出(bin \\ debug或bin \\ release)文件夹中。
This behavior is controlled by the Copy To Output Directory property on the database file. 此行为由数据库文件上的“ Copy To Output Directory属性控制。

If you have this property set to Copy Always then, every time you build your project a fresh copy of the database file is copied from the root project folder to the output directory overwriting the one already there and destroying the changes you have made in the previous debug session. 如果将此属性设置为“始终复制”,则每次构建项目时,都会从根项目文件夹中将数据库文件的新副本复制到输出目录,从而覆盖已经存在的目录并破坏您在上一个目录中所做的更改。调试会话。

A suggested fix is to change this property to Copy Never or Copy if Newer 建议的解决方案是将此属性更改为Copy NeverCopy if Newer

See a detailed explanation on MSDN at this page 在此页面上查看有关MSDN的详细说明

The shortcut |DataDirectory| 快捷方式|DataDirectory| in the connection string points (for a winforms project) to the bin\\debug or bin\\release relative subfolder of the current project, Any changes made by your program works on the database located there, but these changes are overwritten at the next run 在指向当前项目的bin \\ debug或bin \\ release相对子文件夹的连接字符串点(对于Winforms项目)中,您的程序所做的任何更改都会在该数据库所在的数据库上起作用,但是在下次运行时这些更改将被覆盖

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM