Showing posts with label store. Show all posts
Showing posts with label store. Show all posts

Tuesday, March 27, 2012

Bulk insertion

Hi,

I am working on an application that is to read a large number of XML files, take out specific values from each file, and store these in a SQL server so that reports can be generated from these values. There are some 15-20,000 files for each month of the year. I am OK with parsing the files and getting the fields that I need but I don't want to insert one record at a time as I parse the files. I was told that I can create a .exe file that parses the xml files and stores the required values in a csv file and use these csv files to initiate a bulk insert, using Business Intelligence Studio. I have not been able to find any info or article on how to do this. Any help on how I can accomplish this, or alternate solutions is greatly appreciated.

You can make use of SQL BulkCopy feature of ADO.NET. Load your XML into a DataSet/DataTable and run SQL Bulk copy into your table. Very few lines of code.

using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString))
{
foreach (string tableName in tableNames)
{
SqlBulkCopy(dataSet.Tables[tableName], tableName, bulkCopy);
}
}


private static void SqlBulkCopy(DataTable dataTable, string tableName, SqlBulkCopy bulkCopy)
{
bulkCopy.DestinationTableName = tableName;
bulkCopy.ColumnMappings.Clear();
foreach (DataColumn myCol in dataTable.Columns)
bulkCopy.ColumnMappings.Add(myCol.ColumnName, myCol.ColumnName);
bulkCopy.WriteToServer(dataTable);
}

|||

Thanks for the reply Raghu. I don't need all the data elements in the XML files, only a selected few. This is an example XML file:

<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
<s:ElementType name='row' content='eltOnly' rs:updatable='true'>
<s:AttributeType name='c0' rs:name='AFR Finalization' rs:number='1' rs:write='true'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='18' rs:precision='0' rs:fixedlength='true' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='Processed' rs:number='2' rs:write='true'>
<s:datatype dt:type='int' dt:maxLength='4' rs:precision='0' rs:fixedlength='true' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='Rate' rs:number='3' rs:write='true'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='7' rs:precision='0' rs:fixedlength='true' rs:maybenull='false'/>
</s:AttributeType>
<s:extends type='rs:rowbase'/>
</s:ElementType>
</s:Schema>
<rs:data>
<rs:insert>
<z:row c0='CIF '/>
<z:row c0=' Input ' Processed='12028'/>
<z:row c0=' Finalized ' Processed='5444' Rate=' 45.26%'/>
<z:row c0='RTS '/>
<z:row c0=' Input ' Processed='9802'/>
<z:row c0=' Finalized ' Processed='5504' Rate=' 56.15%'/>
<z:row c0='Interception '/>
<z:row c0=' Input ' Processed='12639'/>
<z:row c0=' Finalized ' Processed='8220' Rate=' 65.04%'/>
</rs:insert>
</rs:data>
</xml>

I only need CIF/Input and RTS/INPUT from this particular file. There will be one of this file for each day of the month and I will have to process a month's worth of xml files. There are tens of different xml files but they all follow the same schema.

Thanks.

Sunday, March 25, 2012

Bulk Insert: Unexpected end-of-file (EOF) encountered...

Hi to all,
I have a problem about a importation of a file *.csv with SQL Server,
through a bulk insert, called in a store procedure that a c# sw calls.
This is the description of the error:
--
System.Data.SqlClient.SqlException stata individuata
Message="Bulk Insert: Unexpected end-of-file (EOF) encountered in
data file.\r\nOLE DB provider 'STREAM' reported an error. The provider
did not give any information about the error.\r\nOLE DB error trace
[OLE/DB Provider 'STREAM' IRowset::GetNextRows returned 0x80004005:
The provider did not give any information about the error.].\r\nThe
statement has been terminated."
Source=".Net SqlClient Data Provider"
ErrorCode=-2146232060
Class=16
LineNumber=1
Number=4832
Procedure=""
Server="ets3971"
State=1
StackTrace:
at System.Data.SqlClient.SqlConnection.OnError(SqlExc eption
exception, Boolean breakConnection)
at
System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException
exception, Boolean breakConnection)
at
System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObject
stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,
SqlCommand cmdHandler, SqlDataReader dataStream,
BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject
stateObj)
at
System.Data.SqlClient.SqlCommand.FinishExecuteRead er(SqlDataReader ds,
RunBehavior runBehavior, String resetOptionsString)
at
System.Data.SqlClient.SqlCommand.RunExecuteReaderT ds(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean
async)
at
System.Data.SqlClient.SqlCommand.RunExecuteReader( CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String
method, DbAsyncResult result)
at
System.Data.SqlClient.SqlCommand.InternalExecuteNo nQuery(DbAsyncResult
result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at sarbox.Default.LoadFlux_Click(Object sender, EventArgs e) in
c:\Inetpub\wwwroot\Zarbox2.2\SoxAdmin\Default.aspx .cs:line 1509
--

Th@.nks to all

AB@.AB@. (b.aharon44@.gmail.com) writes:

Quote:

Originally Posted by

I have a problem about a importation of a file *.csv with SQL Server,
through a bulk insert, called in a store procedure that a c# sw calls.
This is the description of the error:
--
System.Data.SqlClient.SqlException stata individuata
Message="Bulk Insert: Unexpected end-of-file (EOF) encountered in
data file.\r\nOLE DB provider 'STREAM' reported an error. The provider
did not give any information about the error.\r\nOLE DB error trace
[OLE/DB Provider 'STREAM' IRowset::GetNextRows returned 0x80004005:
The provider did not give any information about the error.].\r\nThe
statement has been terminated."


Unfortunately, the information you posted is not sufficient to help
you. Could you please post:

1) The BULK INSERT statement.
2) Any format file you are using.
3) A short sample of the data file.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||On 17 Apr, 23:40, Erland Sommarskog <esq...@.sommarskog.sewrote:

Quote:

Originally Posted by

AB@. (b.aharo...@.gmail.com) writes:

Quote:

Originally Posted by

I have a problem about a importation of a file *.csv with SQL Server,
through a bulk insert, called in a store procedure that a c# sw calls.
This is the description of the error:
--
System.Data.SqlClient.SqlException stata individuata
Message="Bulk Insert: Unexpected end-of-file (EOF) encountered in
data file.\r\nOLE DB provider 'STREAM' reported an error. The provider
did not give any information about the error.\r\nOLE DB error trace
[OLE/DB Provider 'STREAM' IRowset::GetNextRows returned 0x80004005:
The provider did not give any information about the error.].\r\nThe
statement has been terminated."


>
Unfortunately, the information you posted is not sufficient to help
you. Could you please post:
>
1) The BULK INSERT statement.
2) Any format file you are using.
3) A short sample of the data file.
>
--
Erland Sommarskog, SQL Server MVP, esq...@.sommarskog.se
>
Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx


I have risolve it - thanks|||This looks resolved, but I've experienced this problem before.
Resolution occured in one of three ways:

1) We sometimes get files that are cut off prematurely and a line
will only be a fraction completed. This will fail a bulk-insert.
2) Sometimes an extra carriage return is at the end of the file.
I've seen this fail the bulk-insert with an Unexecpected EOF message.
3) Sometimes I just couldn't figure out the answer and using DTS
instead of bulk insert resolved the problem.

I hope that helps somebody. :D

-Utah

Monday, March 19, 2012

bulk insert not working from asp.netpage

Hi,

I am tryin to run the store procedure which has bulk insert command.when i run the code,it says you don't have permission to use bulk insert command. This query/storeprocedure is running succesfully in the backend(sqlserver2000).

solution is only user in Bulkadmin role has permission for bulk insert .But i don't know how to create one and use it in asp.net.

Can anyone help me in this. ..Here is the code.

string conn_string1 =@."Data Source=SENTHIL\TEST;Initial Catalog=EMPLOYEE;Integrated Security=SSPI";

SqlConnection objconn1=

new SqlConnection(conn_string1);

objconn1.Open();

SqlCommand command1=

new SqlCommand("bulkinsert",objconn1);

command1.CommandType=CommandType.StoredProcedure;

command1.ExecuteNonQuery();

Response.Write("Stored procedure excecuted");

objconn1.Close();

thanks,

kar

In http://msdn2.microsoft.com/en-us/library/ms188365.aspx

Requires INSERT and ADMINISTER BULKOPERATIONS permissions. Additionally, ALTER TABLE permission isrequired if one or more of the following is true:

Constraints exist and the CHECK_CONSTRAINTS option is not specified.

Note: Disabling constraints is the default behavior. To check constraints explicitly, use the CHECK_CONSTRAINTS option.

Friday, February 24, 2012

Bulk insert

Hi,
i'm trying to store some bitmap or jpeg files located on my harddrive into a
table of my datable which have an image type field.
while looking at different help files, it seems to be possible with bulk
insert ...
does anybody got a simple way to to such a job ?
best regards.Franck,
In version 2000, you will find a utility named textcopy.exe, installed on
"C:\Program Files\Microsoft SQL Server\MSSQL\Binn". See if this helps.
Copy Text or Image into or out of SQL Server
http://www.databasejournal.com/feat...cle.php/1443521
AMB
"Franck Fouache" wrote:

> Hi,
> i'm trying to store some bitmap or jpeg files located on my harddrive into
a
> table of my datable which have an image type field.
> while looking at different help files, it seems to be possible with bulk
> insert ...
> does anybody got a simple way to to such a job ?
> best regards.
>
>|||Yes it helps ...
as i didn't know textcopy exists i thought bulk insert was the only way ...
thanks for your help Alejandro.
"Alejandro Mesa" <AlejandroMesa@.discussions.microsoft.com> a crit dans le
message de news: F8380DA0-0EF8-454E-B0FB-234CA7661816@.microsoft.com...[vbcol=seagreen]
> Franck,
> In version 2000, you will find a utility named textcopy.exe, installed on
> "C:\Program Files\Microsoft SQL Server\MSSQL\Binn". See if this helps.
> Copy Text or Image into or out of SQL Server
> http://www.databasejournal.com/feat...cle.php/1443521
>
> AMB
> "Franck Fouache" wrote:
>

Bulk insert

Hi,
i'm trying to store some bitmap or jpeg files located on my harddrive into a
table of my datable which have an image type field.
while looking at different help files, it seems to be possible with bulk
insert ...
does anybody got a simple way to to such a job ?
best regards.Franck,
In version 2000, you will find a utility named textcopy.exe, installed on
"C:\Program Files\Microsoft SQL Server\MSSQL\Binn". See if this helps.
Copy Text or Image into or out of SQL Server
http://www.databasejournal.com/features/mssql/article.php/1443521
AMB
"Franck Fouache" wrote:
> Hi,
> i'm trying to store some bitmap or jpeg files located on my harddrive into a
> table of my datable which have an image type field.
> while looking at different help files, it seems to be possible with bulk
> insert ...
> does anybody got a simple way to to such a job ?
> best regards.
>
>|||Yes it helps ...
as i didn't know textcopy exists i thought bulk insert was the only way ...
thanks for your help Alejandro.
"Alejandro Mesa" <AlejandroMesa@.discussions.microsoft.com> a écrit dans le
message de news: F8380DA0-0EF8-454E-B0FB-234CA7661816@.microsoft.com...
> Franck,
> In version 2000, you will find a utility named textcopy.exe, installed on
> "C:\Program Files\Microsoft SQL Server\MSSQL\Binn". See if this helps.
> Copy Text or Image into or out of SQL Server
> http://www.databasejournal.com/features/mssql/article.php/1443521
>
> AMB
> "Franck Fouache" wrote:
>> Hi,
>> i'm trying to store some bitmap or jpeg files located on my harddrive
>> into a
>> table of my datable which have an image type field.
>> while looking at different help files, it seems to be possible with bulk
>> insert ...
>> does anybody got a simple way to to such a job ?
>> best regards.
>>