1. I have a DatabaseManager class where I determine my connection string based on the domain authority like this:
string s = HttpContext.Current.Request.Url.Authority;
For example, If I'm working on my development machine, the static constructor would look for "localhost" in the url and set the connection string accordingly. Is there a better way to automatically figure out the connection string value?
2. For all of my Select commands, I have a public method that I call that lives in the DatabaseManager class:
public static DataSet DoGenericSelect(string pSelectCommand)
{
using (DataSet ds = new DataSet())
{
using (SqlDataAdapter da = new SqlDataAdapter(@pSelectCommand, connectionString))
{
da.Fill(ds);
}
return ds;
}
}
Question here is: Am I using the using statement correctly, resources being disposed properly? Can this be improved?
3. This is the method I am worrying about the most:
public static int DoGenericBulkTableUpdate(DataTable pDataTable, string @pSql)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
var dataTableFromDatabase = new DataTable();
var cmd = new SqlCommand(@pSql, conn);
var da = new SqlDataAdapter(cmd);
da.Fill(dataTableFromDatabase);
dataTableFromDatabase.Merge(pDataTable);
var cb = new SqlCommandBuilder(da);
da.UpdateCommand = cb.GetUpdateCommand();
return da.Update(dataTableFromDatabase);
}
}
Do I need more Using statements to make sure I am disposing the SqlCommand, DataAdapter, etc properly?
Mikeyyy
Member
12 Points
24 Posts
hopefully ez questions about my code
Dec 14, 2012 09:58 PM|LINK
Any opinions on these questions are appreciated:
1. I have a DatabaseManager class where I determine my connection string based on the domain authority like this:
string s = HttpContext.Current.Request.Url.Authority;
For example, If I'm working on my development machine, the static constructor would look for "localhost" in the url and set the connection string accordingly. Is there a better way to automatically figure out the connection string value?
2. For all of my Select commands, I have a public method that I call that lives in the DatabaseManager class:
public static DataSet DoGenericSelect(string pSelectCommand)
{
using (DataSet ds = new DataSet())
{
using (SqlDataAdapter da = new SqlDataAdapter(@pSelectCommand, connectionString))
{
da.Fill(ds);
}
return ds;
}
}
Question here is: Am I using the using statement correctly, resources being disposed properly? Can this be improved?
3. This is the method I am worrying about the most:
public static int DoGenericBulkTableUpdate(DataTable pDataTable, string @pSql)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
var dataTableFromDatabase = new DataTable();
var cmd = new SqlCommand(@pSql, conn);
var da = new SqlDataAdapter(cmd);
da.Fill(dataTableFromDatabase);
dataTableFromDatabase.Merge(pDataTable);
var cb = new SqlCommandBuilder(da);
da.UpdateCommand = cb.GetUpdateCommand();
return da.Update(dataTableFromDatabase);
}
}
Do I need more Using statements to make sure I am disposing the SqlCommand, DataAdapter, etc properly?
Thanks!
Mike
thaicarrot
Contributor
5120 Points
1459 Posts
Re: hopefully ez questions about my code
Dec 14, 2012 10:44 PM|LINK
http://msdn.microsoft.com/en-us/library/0e91td57.aspx
You should use using with Unmanaged Code.
Do I need more Using statements to make sure I am disposing the SqlCommand, DataAdapter, etc properly?
You don't need more but if there's more unmanaged code you should have more nest using.
Weera
Mikeyyy
Member
12 Points
24 Posts
Re: hopefully ez questions about my code
Dec 15, 2012 01:13 AM|LINK
danke!
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: hopefully ez questions about my code
Dec 15, 2012 09:43 AM|LINK
We suggest you speaking "Thanks" for "danke" in English, for this is an English forum;)