+DAAB +GetSqlStringCommand +Connection = Null

Topics: Data Access Application Block
Jun 5, 2007 at 7:27 PM
I'm trying to do do the following:

Database db = DatabaseFactory.CreateDatabase("BancoAspNet");
string strSql = string.Format("SELECT * from aspnet_users where LoweredUserName = '{0}' and codigo = '{1}'", username, codigo);
DbCommand cmd = db.GetSqlStringCommand(strSql);

But the cmd.Connection is always null so I have to do this:

cmd.Connection = db.CreateConnection();
cmd.Connection.Open();

But thats not the right way, right?

Does anyone know what is the problem??

Here's my web.config file:

<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere" />
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>
<dataConfiguration defaultDatabase="ConexaoPadrao" />
<connectionStrings>
<add name="BancoAspNet" connectionString="Data Source=(Local); User Id=teste; Password=teste123; Initial Catalog=aspnetdb;"
providerName="System.Data.SqlClient" />
<add name="ConexaoPadrao" connectionString="Data Source=(Local); User Id=teste; Password=teste123; Initial Catalog=teste;"
providerName="System.Data.SqlClient" />
</connectionStrings>
Jun 5, 2007 at 9:42 PM
Edited Jun 5, 2007 at 9:46 PM
The right syntax is as follows:

Database db = DatabaseFactory.CreateDatabase("BancoAspNet");
string strSql = string.Format("SELECT * from aspnet_users where LoweredUserName = '{0}' and codigo = '{1}'", username, codigo);
DbCommand cmd = db.GetSqlStringCommand(strSql);

using (IDataReader rst = db.ExecuteReader(cmd)) {
while (rst.Read()) {
}
}

The rule:
Don't touch the cmd-object, it's only a reference for you.
Jun 6, 2007 at 2:50 PM
thanx