Hello there I hope you're having a great time. I have a question And I will break it down into 3 points:
1: create a class to connect to sql server the connection should be made using sql server authentication. This class should contain several variables for connection parameters.
2: create a user form that shows the current connection parameters. And allow the user to update those parameters. In this form there should be a button to test the connect and another button to save the user changes to the connection parameters.
3: how to share the connection, created by the class we made in point 1, between different forms in the application. Without keeping too many open connections ideally only one connection should be open.
I will add the code that can solve this problem I hope that you can help me refine it.
I am new to all of this. Thank you all for help.
SqlConnection
and maybe SqlConnectionStringBuilder
SqlConnectionStringBuilder
to a PropertyGrid
- or just write the UI from scratch even "only one connection should be open" is wrong, IMO - let the inbuilt connection pooling deal with that; all you need is some configuration class with the connection string - and just deal with the connections as you need them, very locally - ie
using(var conn = new SqlConnection(Config.ConnectionString)) { conn.Open(); // NOT SHOWN: do a couple of related operations } // <== and here, it dies
1 : go to MSDN website you'll find what you need : http://msdn.microsoft.com/fr-fr/library/system.data.sqlclient.sqlcommand.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2
private static void ReadOrderData(string connectionString)
{
string queryString =
"SELECT OrderID, CustomerID FROM dbo.Orders;";
using (SqlConnection connection = new SqlConnection(
connectionString))
{
SqlCommand command = new SqlCommand(
queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
try
{
while (reader.Read())
{
Console.WriteLine(String.Format("{0}, {1}",
reader[0], reader[1]));
}
}
finally
{
// Always call Close when done reading.
reader.Close();
}
}
}
2: look at your connection properties ( http://msdn.microsoft.com/en-us/library/System.Data.SqlClient.SqlConnection_properties.aspx ) and fill a listView or equivalent with it
3: Use previous SqlConnection.Open() to deal with it
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.