Welcome to dbFreaks.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Problem passing DBNulls in a DataTable parameter of AdomdC..

 
   Database Help (Home) -> Datamining RSS
Next:  Retrive score in Logistic Regression (Microsoft N..  
Author Message
Nestor

External


Since: Feb 27, 2008
Posts: 5



(Msg. 1) Posted: Wed Feb 27, 2008 7:58 pm
Post subject: Problem passing DBNulls in a DataTable parameter of AdomdCommand
Archived from groups: microsoft>public>sqlserver>datamining (more info?)

The following code would fail, with the error:

"The '' string cannot be converted to the double type."

when trainTable contains rows with DBNull in some of the columns.
How can I pass NULL to MSAS?

using (AdomdCommand comm = connAS.CreateCommand())
{
comm.CommandText = "INSERT INTO MINING MODEL [MyModel]
([statbbrev],[lpop],[ldem],[lemp]) @TrainTable";
comm.Parameters.Add("TrainTable", trainTable);
comm.ExecuteNonQuery();
}

 >> Stay informed about: Problem passing DBNulls in a DataTable parameter of AdomdC.. 
Back to top
Login to vote
Bogdan Crivat MSFT

External


Since: Feb 20, 2004
Posts: 12



(Msg. 2) Posted: Thu Feb 28, 2008 11:30 am
Post subject: Re: Problem passing DBNulls in a DataTable parameter of AdomdCommand [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

This is a known bug, which has been fixed for SQL Server 2008.
In 2005, you can apply the following workaround:
- create your own class deriving from IDataReader and wrapping a reader from
your table (you can get it with CreateReader)
- All methods delegate to the wrapped reader, except GetValue
- GetValue should return null instead of DBNull when the wrapped data reader
contains DBNull



--
--
--
This posting is provided "AS IS" with no warranties, and confers no rights.
Please do not send email directly to this alias. It is for newsgroup
purposes only.

thanks,
bogdan

"Nestor" <nestor RemoveThis @nsuli.com> wrote in message
news:32B719B6-FE6B-47A4-9FF3-8B264351057C@microsoft.com...
> The following code would fail, with the error:
>
> "The '' string cannot be converted to the double type."
>
> when trainTable contains rows with DBNull in some of the columns.
> How can I pass NULL to MSAS?
>
> using (AdomdCommand comm = connAS.CreateCommand())
> {
> comm.CommandText = "INSERT INTO MINING MODEL [MyModel]
> ([statbbrev],[lpop],[ldem],[lemp]) @TrainTable";
> comm.Parameters.Add("TrainTable", trainTable);
> comm.ExecuteNonQuery();
> }

 >> Stay informed about: Problem passing DBNulls in a DataTable parameter of AdomdC.. 
Back to top
Login to vote
Nestor

External


Since: Feb 27, 2008
Posts: 5



(Msg. 3) Posted: Thu Feb 28, 2008 3:53 pm
Post subject: Re: Problem passing DBNulls in a DataTable parameter of AdomdCommand [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thanks much. It worked. Below is the wrapper class for everybody else.
You use it like this:
comm.Parameters.Add("TrainTable", new WorkAroundDataReader(trainTable));

//-------------------------class-----------------------

class WorkAroundDataReader : IDataReader

{

private DataTableReader rdr;

public WorkAroundDataReader(DataTable t)

{

rdr = t.CreateDataReader();

}

#region IDataReader Members

public void Close()

{

rdr.Close();

}

public int Depth

{

get {

return rdr.Depth;

}

}

public DataTable GetSchemaTable()

{

return rdr.GetSchemaTable();

}

public bool IsClosed

{

get { return rdr.IsClosed; }

}

public bool NextResult()

{

return rdr.NextResult();

}

public bool Read()

{

return rdr.Read();

}

public int RecordsAffected

{

get { return rdr.RecordsAffected; }

}

#endregion

#region IDisposable Members

public void Dispose()

{

rdr.Dispose();

}

#endregion

#region IDataRecord Members

public int FieldCount

{

get { return rdr.FieldCount; }

}

public bool GetBoolean(int i)

{

return rdr.GetBoolean(i);

}

public byte GetByte(int i)

{

return rdr.GetByte(i);

}

public long GetBytes(int i, long fieldOffset, byte[] buffer, int
bufferoffset, int length)

{

return rdr.GetBytes(i, fieldOffset, buffer, bufferoffset, length);

}

public char GetChar(int i)

{

return rdr.GetChar(i);

}

public long GetChars(int i, long fieldoffset, char[] buffer, int
bufferoffset, int length)

{

return rdr.GetChars(i, fieldoffset, buffer, bufferoffset, length);

}

public IDataReader GetData(int i)

{

return rdr.GetData(i);

}

public string GetDataTypeName(int i)

{

return rdr.GetDataTypeName(i);

}

public DateTime GetDateTime(int i)

{

return rdr.GetDateTime(i);

}

public decimal GetDecimal(int i)

{

return rdr.GetDecimal(i);

}

public double GetDouble(int i)

{

return rdr.GetDouble(i);

}

public Type GetFieldType(int i)

{

return rdr.GetFieldType(i);

}

public float GetFloat(int i)

{

return rdr.GetFloat(i);

}

public Guid GetGuid(int i)

{

return rdr.GetGuid(i);

}

public short GetInt16(int i)

{

return rdr.GetInt16(i);

}

public int GetInt32(int i)

{

return rdr.GetInt32(i);

}

public long GetInt64(int i)

{

return rdr.GetInt64(i);

}

public string GetName(int i)

{

return rdr.GetName(i);

}

public int GetOrdinal(string name)

{

return rdr.GetOrdinal(name);

}

public string GetString(int i)

{

return rdr.GetString(i);

}

public object GetValue(int i)

{

//HERE IS THE FIX

if (rdr.IsDBNull(i)) return null;

return rdr.GetValue(i);

}

public int GetValues(object[] values)

{

return rdr.GetValues(values);

}

public bool IsDBNull(int i)

{

return rdr.IsDBNull(i);

}

public object this[string name]

{

get { return rdr[name]; }

}

public object this[int i]

{

get { return rdr[i]; }

}

#endregion

}
 >> Stay informed about: Problem passing DBNulls in a DataTable parameter of AdomdC.. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Collation Problem any help - Hello all : My problem is like the following I have an online databse with a latin collation, this database is using ntext and nvarchar values that was with our previous admin ok the problem is in the data is stored should use another collation (the...

Reprocessing a mining model - I'm currently developing a user registration prototype. The aim is to classify certain types of users. However, after the registration I query a decision tree model in Analysis Services with DMX and I want to send the user data into the warehouse to....

Trying to cut down query times - I have a query that is based on two other queries that are based on tables in a database. All of the queries that are used for this final query use grouping and totals. In the application I am making, I need to choose a date range to select the subset....

Update serial number in database - Hi, I want to update specific fields in a existing table in SQL database as below example: L240100001-LG L240100002-LG *serial number is difficult to update one by one L240100003-LG L240100004-LG L240100005-LG change to L240100001-L L240100002-L..

Newbie question - I have SQL Server 2000. What add-ons do I need to try data mining, and where do I best go for resources/references?
   Database Help (Home) -> Datamining All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]