Last active
June 8, 2024 19:05
-
-
Save ChrisMoney/15541e9b42088c6151d93749fc26d71d to your computer and use it in GitHub Desktop.
Read SQL connection string from XML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This is an example of a connectionString node. | |
<connectionStrings> | |
<add name="YourConnectionStringKey" | |
providerName="System.Data.SqlClient" | |
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=YourDB;Trusted_Connection=Yes" /> | |
</connectionStrings> | |
*/ | |
// Global Var outside of CRUD methods | |
string _connectionString = | |
ConfigurationManager.ConnectionStrings["YourConnectionStringKey"].ConnectionString; | |
// Global vars | |
SqlConnection _connection; | |
SqlTransaction _transaction; | |
int GetData() { | |
int retVal = 0; | |
_connection.Open(); | |
// Start a local transaction. | |
_transaction = _connection.BeginTransaction("SampleTransaction"); | |
// Must assign both transaction object and connection | |
// to Command object for a pending local transaction | |
command.Connection = connection; | |
command.Transaction = transaction; | |
try | |
{ | |
DAOClass daoClass = new DAOClass(_connection, _transaction); | |
retVal = daoClass.GetData(); | |
transaction.Commit(); | |
return retVal; | |
} | |
catch (Exception ex) | |
{ | |
transaction.RollBack(); | |
Log(ex.ToString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment