Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process sqlservr.exe keeps running after Connection.Close() in C#

Tags:

c#

sql

I created a connection to a SQL database, but when I close it, the process sqlservr.exe keeps running even after closing the application. I've tried to use Dispose too, but had the same problem. sqlservr.exe is meant to keep running(It wasn't before starting the application)? Is there any way of killing it?

namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string conexao = "Data Source=(LocalDB)\\v11.0;AttachDbFilename=C:\\Users\\SMITH\\Documents\\C#\\WindowsFormsApplication3\\WindowsFormsApplication3\\Database1.mdf;Integrated Security=True";
        SqlConnection conn = new SqlConnection(conexao);
        SqlCommand comando = new SqlCommand("SELECT COUNT(*) FROM Usuarios WHERE NomeUser = @user and SenhaUser = @senha", conn);

        comando.Parameters.Add("@user", SqlDbType.VarChar).Value = textBox1.Text;
        comando.Parameters.Add("@senha", SqlDbType.VarChar).Value = textBox2.Text;


        conn.Open();
        int i = (int)comando.ExecuteScalar();

        string a = i.ToString();

        textBox3.Text = a;

        if(i>0){
            MessageBox.Show("Existe");
        }else{
            MessageBox.Show("Nem existe");

            conn.Dispose();

        }
    }
}   

}

like image 644
gumattos Avatar asked Oct 22 '25 15:10

gumattos


2 Answers

You're probably thinking you're doing something else than what you're really doing.

You're expecting you've got local access to a local DB through an embedded SQL server. This is not the case. In reality, you're just starting the full fledged MS SQL Server service (sqlservr.exe) which is not tied to your application at all - apart from being started by your process, it's the same as if you had it configured to run at startup of Windows etc.

This isn't necessarily a bad thing, but if you're expecting your application to work with an embedded server, you can run into issues. Namely, the SQL server is configured on the computer, not through your application, it has to be installed (separately), if there is another server running, you're attaching to that one - for which you don't necessarily have permissions etc.

If you only use this for an internal tool, don't bother with changing anything, having the sql server process running is fine and you can shut it down through Services. If this is a part of your distributed application, consider using a different SQL server, for example SQL Server Anywhere (SQL Server CE), or perhaps even something completely different, like MS Access (freely available on every Windows PC, not just with Office) or FireBird.

like image 88
Luaan Avatar answered Oct 24 '25 03:10

Luaan


Since your connection string indicates you're using LocalDB, from Introducing LocalDB, an improved SQL Express

LocalDB doesn't create any database services; LocalDB processes are started and stopped automatically when needed. The application is just connecting to "Data Source=(localdb)\v11.0" and LocalDB process is started as a child process of the application. A few minutes after the last connection to this process is closed the process shuts down. (emphasis added)

I have confirmed the "few minutes after" behavior in my own environment. But be sure you don't have any processes holding onto a connection anywhere.

like image 35
Kent A. Avatar answered Oct 24 '25 04:10

Kent A.