VB.Net Window Programming database coonection open close - vb.net

I am working on a window project using VB.net. On every form of my project I opened Microsoft access database connection on form load and closed on form closing event. So I want to ask that this is good practice to open/close database connection on every form.

Consider the followings:
Would the db connection be used to update or only query?
How many users would open the form and connect to db at the same time?
If the users are many and they update db, I think it's not a good idea to keep connected while opening the form. Usually you may query some data upon opening and then close the connection immediately, and the same for updating.

It is good practice to create/open/close db connection every time you execute some query.
Notice that by create/open/close I mean instantiating new instance of SqlConnection.
Under the hood ADO.NET will open real, physical connection to the database server only once and re-use it every time you will open new connection by your code.
So every query should be execute with own new connection
Using connection As SqlConnection = new SqlConnection(yourConnectionString)
Using command As SqlCommand = new SqlCommand(sqlQuery, connection)
return command.ExecuteNonQuery();
End Using
End Using
If you open connection in Form_Load and close it in Form_Closing it possible that when exception thrown while form is opened your connection will remain opened.
By reducing amount of time when connection is "opened" in your code you can handle this situation by using Using keyword and avoid using of "ugly" try .. catch everywhere
And I hope you are using sql parameters (SqlParameter) in your queries.

Related

Linqpad DB connections - get it to NOT connect when I open a file?

I have many LinqPad scripts I use to query databases. These files are all in dropbox so I have all my queries available across computers. Very handy.
However each script is tied to a database connection, and that connection is not valid across computers. I'm guessing it has to do with how the passwords are stored. And that's OK.
But what is annoying is when I open a script, LinqPad attempts to connect to the invalid database. It is frozen while this happens. Once it is done, all I have to do is change it to the new db and it's fine. But this freeze interrupts my flow.
Is there a way to tell Linqpad to NOT try to connect when I open a file?
you can use a connection by connection string
so you can add some read key or any machanics to execute connection
using(TypedDataContext context = new TypedDataContext(Connection.ConnectionString)){
context.table.Where(w=>w.ID == 123);
}

How to disconnect SQL client connection once crystal report is closed

I'm currently using SAP Crystal Report 2013. I have a ODBC(RDO) connection setup and every time I run the Crystal Report from my native application(Progress 4GL Application), a new SQL DB connection gets established. However on closing the report, the SQL Client is not disconnected (i.e the connection still remains). Every time I open a report a new connection is established and it never disconnects until I close the application.
I tried using the .close() , .dispose() in VB.net code on which my Crystal Report is setup. Nothings seems to help.
Please Advise.
You can use
repo.Database.Dispose();

Global Connection from SQL Server Database to VB.NET

I want to create a global connection class from Sql server database to vb.net to make it easy in later editing the connection. Could anyone guide me the way to code both creating and calling to individual form?
Any help would be appreciated. Thanks
Keep your connection string in your web.config file. (this gives you a single point where you can change the connection string without needing to recompile your application, it also allows you to encrypt it in the future to protect your DB server).
I'd advise against using a single "connection" object for reuse. Open and close a connection for each query or transaction.
Check out the Repository pattern instead. http://msdn.microsoft.com/en-us/library/ff649690.aspx

Does every Form in c#.net needs a new connection to be established with sql database?

I am creating a c#.net winforms application which uses sql server2005 express. I have three forms in the application.
So, first i connected to the database with windows authentication when Form1 is displayed which is the main form.It uses the connection string from app.config file.
Then i changed to the application role by executing sp_setapprole on a button click on form1.
Then, Form2 opens and i want that it uses the same application role which was set in form1 to connect to the database.
How do i do it?
Does every Form needs a new connection to be established with sql database?
I have created the application role beforehand in SQL Server.
No, I'd say that in general, for a thick (single-threaded) client, you probably want only one connection open (probably for the lifetime of the client).
You can keep the parameterless constructor for Form2 (which will be needed for the VS designer to continue to work), but you can create a second constructor for Form2 that is passed the open connection and continues to work with it, e.g.:
public Form2()
{
InitializeComponent();
}
private SqlConnection _conn;
public Form2(SqlConnection conn)
: this()
{
_conn = conn;
}
Otherwise (if you're a multi-threaded application), I'd recommend that you only open connections when required, perform your work, then close the connection again. Connection pooling will ensure this isn't as inefficient as it sounds, and trying to share a single connection among multiple threads can get very messy. If you're doing this, you might want to create an extension method on SqlConnection, something like OpenAndSetAppRole that will open a new connection and call the sp_setapprole proc before returning
As a starting point have a look at this article:
http://msdn.microsoft.com/en-us/library/ms971481.aspx
is a collection of best practices in using ADO.NET. An extract about connection is:
Using Connections
High performance applications keep connections to the data source in use for a minimal amount of time, as well as take advantage of performance enhancing technology such as connection pooling. The following topics provide you with tips to help you achieve greater performance when using ADO.NET to connect to your data source.
In other word you should connect to the database when you are issuing a conversation ( filling a page in a grid, updating a record and so on ).

Updating Datatable to EXISTING global database connection

I am using Microsoft Visual Basic 2010 Express. I have a WPF-based project. I have a successful connection to a database in Database Explorer, and a working existing Data Source in Data Sources.
I created a datatable from the datasource using this code:
Dim roster_table As New DataTable("AGENT_ROSTER")
I can manipulate this datatable just fine, but I cannot figure out how to save its data to my database (agentroster.sdf) since the connection is on a global level, and isn't declared in this particular window.
How do I update this database from the datatable on this window?
By the way, I tried creating a connection on this window's code, using the exact same connection string as the successful globally-connected database, yet it said that it couldn't connect.
Tough to say given the limited information you provided. You should also note that there are a lot of ways to do this. However the most basic way is to use a table adapter. Once you've set the Update, Insert and Delete commands you call its Update method.
Here's an MSDN article that might help you further