开发者

SQL Server database doesn't delete records

I have a SQL Server database in a C# project.

I use a connection string to connect to it.. I can use the method ExecuteNonQuery to insert data, no problem there.

But when I delete, it only deletes it momentarily, as soon as I restart the application it kind of rolls back the deletion.. Any ideas?

PS: I tested the string in a direct query, and it worked fine there.

public void executeNonQuery(string input)
{
    db.Open();
    SqlCommand cmd = new SqlCommand(input, db);
    cmd.ExecuteNonQuery();
    db.Close();
}

EDIT: DELETION CODE:

private void buttonSletPost_Click(object sender, EventArgs e)
    {
        if(dataGridView1.GetCellCount(DataGridViewElementStates.Selected)>0){
            for (int i = 0;i < dataGridView1.GetCellCount(DataGridViewElementStates.Selected); i++)
            {
                String str1 = String.Format("WARNING about to DELETE:\n {0} \n BE CAREFULL NO TURNING BACK NOW!", Regnskab.getInstance().dbSelectPostID(dataGridView1.SelectedCells[i].Value.ToString())[0].ToString());
                if (MessageBox.Show(str1, "Confirm Deletion", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {

                    string str = String.Format("DELETE FROM PostTable WHERE PostID={0}", dataGridView1.SelectedCells[i].Value.ToString());
                    Database.getInstance().executeNonQuery(str);
                    Console.WriteL开发者_StackOverflow中文版ine(str);
                }
            }
        }else {MessageBox.Show("No cells selected");}
    }

Which will give following output:

DELETE FROM PostTable WHERE PostID=7

Connection string in app.config:

<connectionStrings>
    <add name="EndProject.Properties.Settings.DBtestConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DBtest.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" providerName="System.Data.SqlClient" />


 private Database() 
 {
        string connStr = ConfigurationManager.ConnectionStrings["EndProject.Properties.Settings.DBtestConnectionString"].ConnectionString;
        db = new SqlConnection(connStr);
 }

And then I open and close it, so the connection ain't open each time.

To be honest:

I don't exactly know where to see my DB info, here's some info from properties in VS2010.

Provider: .NET Framework Data Provider for SQL Server

Type: Microsoft SQL Server

Andrew's 3rd: I think they are deleted momentarily because I reloaded the information in my datagridview and from there it is gone. But then when I close the application and start it again, it is back...

Also i just checked with VS2010's server explorer and did a "Show Data" after I deleted (before I shut it down) and it wasn't deleted.

But I'm totally clueless now. :-(


Many applications use Transactions to manage db connections. SQL Server doesn't do it by default, but other factors in your application may be doing this.

Also, if you're really doing this, and your input is coming from a user interface, I can't wait to introduce you to Little Bobby Tables


Use the SQL Server Profiler to run a trace and capture the actual T-SQL statements that are getting executed on the database.

This is the easiest solution.


The behavior you're describing sounds like autocommit is off. Try the following and see if the record(s) stays deleted:

public void executeNonQuery(string input)
{
         db.Open();
         using (var txn = db.BeginTransaction())
         {
             SqlCommand cmd = new SqlCommand(input, db);
             cmd.Transaction = txn;
             cmd.ExecuteNonQuery();
             db.Close();
             txn.Commit();
        }
}


Ok it was apperently me who was mistaken on the inserting part... Someone suggested that i was because of the Visual Studio databases created every time the applikation ran. So i installed MS SQL 2008 R2. Created a new DB with same layout. Changed the connection string And wuupti woo it seems to work, ill come back to this thread if it breaks down later.. But delete + insert both working greatly now :-)

Thanks to all who tried to help!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜