Enterprise Library

G

Guest

I have a web method that I want to improve by using the Enterprise Library
Data block. I have added the Microsoft.Practices.EnterpriseLibrary.Data.dll
and Microsoft.Practices.EnterpriseLibrary.Data.xml references to my project
but I can't work out how to use them. The Help is confusing and I can't find
any examples anywhere.
Please can someone tell me how to convert the following code? It is
written in Visual Studio 2005 in C# and it works correctly as it is. It
connects to a SQL Server 2005 database.

TIA,
Helen

// Takes xml data and stores it in a table using the connection and
SQL in the associated config file
[WebMethod]
public bool XMLFileToDatabase(String XMLText)
{
String ConnectionString;
SqlConnection XMLConnection;
SqlCommand UpdateXML;

// Connect to the database
ConnectionString =
ConfigurationSettings.AppSettings["ConnectionString"];
XMLConnection = new SqlConnection(ConnectionString);
XMLConnection.Open();

// Update the XML
UpdateXML = new SqlCommand();
UpdateXML.CommandText = ConfigurationSettings.AppSettings["SQL"];

UpdateXML.Parameters.Add("@XMLText", SqlDbType.Text).Value =
XMLText;
UpdateXML.Connection = XMLConnection;
UpdateXML.ExecuteScalar();

return true;
}
 
S

sloan

Something like this:

[WebMethod]
public bool XMLFileToDatabase(String XMLText)
{
string sql = ConfigurationSettings.AppSettings["SQL"];


Database db = DatabaseFactory.CreateDatabase(); // this will use the
defaultDatabase in the config file
//or//Database db =
DatabaseFactory.CreateDatabase("MainDatabaseConnectionString");


DBCommandWrapper cmd = db.GetSqlStringCommandWrapper(sql);
cmd.AddInParameter("@XMLText", DbType.String, XMLText);

db.ExecuteNonQuery();


return true;

}

................

These lines

// Connect to the database
ConnectionString =
ConfigurationSettings.AppSettings["ConnectionString"];

are completely wrong for using the EnterpriseLibrary.Data in a 2.0 project.



You need to add connection string information to your app.config (or
web.config) file.


Something like this:

<configuration>

<configSections>
<section name="dataConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings,
Microsoft.Practices.EnterpriseLibrary.Data"/>
</configSections>

<appSettings>

DO NOT PUT "ConnectionString" here, delete this line

--other app settings values , delete this line

</appSettings>


<connectionStrings>
<!-- This name most likely will match the 'dataConfiguration
defaultDatabase' below. -->
<add name="MainDatabaseConnectionString"
connectionString="server=MyServer;database=MyDatabase;User
ID=myuser;password=mypassword" providerName="System.Data.SqlClient"/>

<add name="AnotherConnectionStringNameIfYouNeedIt"
connectionString="server=MyServer2;database=MyDatabase2;User
ID=myuser;password=mypassword" providerName="System.Data.SqlClient"/>
</connectionStrings>

<dataConfiguration defaultDatabase="MainDatabaseConnectionString"/>

-----------all the other stuff already in your config file , delete this
line

</configuration>





Helen Trim said:
I have a web method that I want to improve by using the Enterprise Library
Data block. I have added the
Microsoft.Practices.EnterpriseLibrary.Data.dll
and Microsoft.Practices.EnterpriseLibrary.Data.xml references to my
project
but I can't work out how to use them. The Help is confusing and I can't
find
any examples anywhere.
Please can someone tell me how to convert the following code? It is
written in Visual Studio 2005 in C# and it works correctly as it is. It
connects to a SQL Server 2005 database.

TIA,
Helen

// Takes xml data and stores it in a table using the connection and
SQL in the associated config file
[WebMethod]
public bool XMLFileToDatabase(String XMLText)
{
String ConnectionString;
SqlConnection XMLConnection;
SqlCommand UpdateXML;

// Connect to the database
ConnectionString =
ConfigurationSettings.AppSettings["ConnectionString"];
XMLConnection = new SqlConnection(ConnectionString);
XMLConnection.Open();

// Update the XML
UpdateXML = new SqlCommand();
UpdateXML.CommandText =
ConfigurationSettings.AppSettings["SQL"];

UpdateXML.Parameters.Add("@XMLText", SqlDbType.Text).Value =
XMLText;
UpdateXML.Connection = XMLConnection;
UpdateXML.ExecuteScalar();

return true;
}
 
S

sloan

If you have a stored procedure..then change

DBCommandWrapper cmd = db.GetSqlStringCommandWrapper(sql);

TO

DBCommandWrapper cmd =
db.GetStoredProcCommandWrapper("uspMyStoredProcedure");
 
G

Guest

Thanks, sloan, I am nearly there. Unfortunately, I cannot get it to like
DBCommandWrapper. I have added all the relevant references to the EL
Configuration, Data Access, etc and I have this at the top:
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;


But it still gives an unrecognised error at DBCommandWrapper.

Can you tell me what I have missed, please?

Thanks,
Helen


sloan said:
Something like this:

[WebMethod]
public bool XMLFileToDatabase(String XMLText)
{
string sql = ConfigurationSettings.AppSettings["SQL"];


Database db = DatabaseFactory.CreateDatabase(); // this will use the
defaultDatabase in the config file
//or//Database db =
DatabaseFactory.CreateDatabase("MainDatabaseConnectionString");


DBCommandWrapper cmd = db.GetSqlStringCommandWrapper(sql);
cmd.AddInParameter("@XMLText", DbType.String, XMLText);

db.ExecuteNonQuery();


return true;

}

................

These lines

// Connect to the database
ConnectionString =
ConfigurationSettings.AppSettings["ConnectionString"];

are completely wrong for using the EnterpriseLibrary.Data in a 2.0 project.



You need to add connection string information to your app.config (or
web.config) file.


Something like this:

<configuration>

<configSections>
<section name="dataConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings,
Microsoft.Practices.EnterpriseLibrary.Data"/>
</configSections>

<appSettings>

DO NOT PUT "ConnectionString" here, delete this line

--other app settings values , delete this line

</appSettings>


<connectionStrings>
<!-- This name most likely will match the 'dataConfiguration
defaultDatabase' below. -->
<add name="MainDatabaseConnectionString"
connectionString="server=MyServer;database=MyDatabase;User
ID=myuser;password=mypassword" providerName="System.Data.SqlClient"/>

<add name="AnotherConnectionStringNameIfYouNeedIt"
connectionString="server=MyServer2;database=MyDatabase2;User
ID=myuser;password=mypassword" providerName="System.Data.SqlClient"/>
</connectionStrings>

<dataConfiguration defaultDatabase="MainDatabaseConnectionString"/>

-----------all the other stuff already in your config file , delete this
line

</configuration>





Helen Trim said:
I have a web method that I want to improve by using the Enterprise Library
Data block. I have added the
Microsoft.Practices.EnterpriseLibrary.Data.dll
and Microsoft.Practices.EnterpriseLibrary.Data.xml references to my
project
but I can't work out how to use them. The Help is confusing and I can't
find
any examples anywhere.
Please can someone tell me how to convert the following code? It is
written in Visual Studio 2005 in C# and it works correctly as it is. It
connects to a SQL Server 2005 database.

TIA,
Helen

// Takes xml data and stores it in a table using the connection and
SQL in the associated config file
[WebMethod]
public bool XMLFileToDatabase(String XMLText)
{
String ConnectionString;
SqlConnection XMLConnection;
SqlCommand UpdateXML;

// Connect to the database
ConnectionString =
ConfigurationSettings.AppSettings["ConnectionString"];
XMLConnection = new SqlConnection(ConnectionString);
XMLConnection.Open();

// Update the XML
UpdateXML = new SqlCommand();
UpdateXML.CommandText =
ConfigurationSettings.AppSettings["SQL"];

UpdateXML.Parameters.Add("@XMLText", SqlDbType.Text).Value =
XMLText;
UpdateXML.Connection = XMLConnection;
UpdateXML.ExecuteScalar();

return true;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top