Multiple Table Insertion Using SQLBulkCopy
Oct 11, 2007
I want to insert data into Header and Details table simultaneously using SQLBulkCopy. Header table contains an Identity columns and Details table contains Foreign Key to this identity column in Header Table.I want to use DataTable as datasource in SQLBulkCopy.Can any body help on this
thanks
View 2 Replies
ADVERTISEMENT
Mar 1, 2006
i am new to sql server
i want to insert five rows continously in sql server database.
my problem is like this .
i am inserting user datails. i want to get the userid which is an identity column.with the userid i want to insert userid and roleid in another table.Roleid values 1 to 5. ie constant.
an sql procedure is required.
View 10 Replies
View Related
Jul 27, 2007
I'm looking at SSIS and SqlBulkCopy as a possible method to quickly insert and process large amounts of data, my current method uses the sp_xml_preparedocument and OPENXML to parse an xml document of the data I want to process and insert into the database, however I'm noticing the performance of SqlServer parsing the xml document isn't that good.
I found the C# SqlBulkCopy method (new in .NET 2.0) and I was thinking it would be faster to use that to load my data into a staging table and then use SSIS to extract the data from the staging table, process and transform it as necessary and finally load it into the final destination tables. I was able to create an Integration Services Project that selects the data from the staging table, does a bit of processing on one of the fields (by calling a stored procedure), and finally loads the processed data into the final table.
The problem with this is I need to clean out the rows that were extracted from the staging table and I'm not sure how I can accomplish this (and I can't just issue a "delete from staging_table" because there maybe new records in the staging table that were not processed), perhaps I can either delete each record as it is proccessed or somehow get the last proccessed identity id from the staging table and delete all records less than or equal to that id.
thanks in advance for any help you can provide, maybe there is an easy way to accomplish what I'm trying to do.
-Matt Palmerlee
View 3 Replies
View Related
Dec 13, 2007
Hi, I'm a Student, and since a few months ago I'm learning JAVA. I'm creating an application to call and compare times. For this I create in Excel a time table which is quite big and it would be a lot of typing work to input one by one the data in each cell in SQL Server, considering that I have to create 8 more tables. I was able to retreive the data from excel usin the JXL API of JAVA but it doesn't give all the funtions to perform math operations as JDBC. That's why I need to move the tables from Excel to SQL.
I found this site http://davidhayden.com/blog/dave/archive/2006/05/31/2976.aspx which gives a code to do so, but I guess that some heathers are missing or maybe I don't know which compiler to use to run that code, I would like you help to identify which compiler use to run that code or if there is some vital piece of code missing.// Connection String to Excel Workbook
string excelConnectionString = @"Provider=Microsoft .Jet.OLEDB.4.0;Data Source=Book1.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
// Create Connection to Excel Workbook
using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
{
OleDbCommand command = new OleDbCommand ("Select ID,Data FROM [Data$]", connection);
connection.Open();
// Create DbDataReader to Data Worksheet
using (DbDataReader dr = command.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = "Data Source=.; Initial Catalog=Test;Integrated Security=True";
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "ExcelData";
bulkCopy.WriteToServer(dr);
}
}
}
On the other hand in this forum I that someelse use that link but implements a totally different code which I'm not able to compile also http://forums.asp.net/p/1110412/2057095.aspx#2057095. It seems this code works as I was able to read, but I do not know which language is used.
Dim excelConnectionString As String = "Provider=Microsoft .Jet.OLEDB.4.0;Data Source=Book1.xls;Extended Properties=""Excel 8.0;HDR=YES;"""
' Using
Dim connection As OleDbConnection = New OleDbConnection(excelConnectionString)
Try
Dim command As OleDbCommand = New OleDbCommand("Select ID,Data FROM [Data$]", connection)
connection.Open()
' Using
Dim dr As DbDataReader = command.ExecuteReader
Try
Dim sqlConnectionString As String = WebConfigurationManager.ConnectionStrings("CampaignEnterpriseConnectionString").ConnectionString
' Using
Dim bulkCopy As SqlBulkCopy = New SqlBulkCopy(sqlConnectionString)
Try
bulkCopy.DestinationTableName =
"ExcelData"
bulkCopy.WriteToServer(dr)
Finally
CType(bulkCopy, IDisposable).Dispose()
End Try
Finally
CType(dr, IDisposable).Dispose()
End Try
Finally
CType(connection, IDisposable).Dispose()
End Try
Catch ex As Exception
End Try
The Compilers I have are: Eclipse, Netbeans, MS Visual C++ Express Edition and MS Visual C# Express Edition. In MS Visual C++
Thanks for your help.
Regads,
Robert.
View 4 Replies
View Related
Jun 19, 2008
Hi Friends,I have 3 labels Steet,City,Pincode and 3 textboxes related to the labels and one button as nae 'Address'I gave the data for Street:abc,City:xyz,Pincode:123 and have to insert into the table.I created one table in the database with table name Adreess and column address varchar(100)but after giving the values in the textboxes and clicked on the button its throwing the exception i.e System.Data.SqlClient.SqlException: The name "abcxyz123" is not permitted in
this context. Valid expressions are constants, constant expressions, and (in
some contexts) variables. Column names are not permitted.I wrote the code like following protected void Button1_Click(object sender, EventArgs e) { string street = txtStreetNo.Text; string city = txtCity.Text; string pincode = txtPincode.Text; string com = street + city+pincode; conn.Open(); SqlDataAdapter daInsert = new SqlDataAdapter("insert into Address values(" + com.ToString() + ")", conn); daInsert.SelectCommand.ExecuteNonQuery();--->here its giving the exception conn.Close(); Response.Write("the values are inserted"); }Please any one tell me am I did the code write or not if its not please give any suggetionsthanksGeeta
View 3 Replies
View Related
Apr 8, 2008
hello
i want to ask if the insertion of a record into a partion is slower than insertion it into a non partitioned table or not?
cuz sql has to decide to wich partion the record has to insert according to the partitioning key and is this decesion process is making insertion slower ?
View 2 Replies
View Related
Sep 9, 2013
I want XML data to be inserted int SQL table but could not figure out. #Currency is my table with assocaite columns and @XMLCurrency is a variable which holds XML string. How can I insert this XML data to my table.
Create table #Currency (CurrencyId int ,ISOCode nvarchar(10),ISONumbricCOde int,ISOName nvarchar(50), IsEnabledForMPV int default 0)
Declare @XMLCurrency nvarchar(max)
Set @XMLCurrency='<R><T><A>0</A><B>USD</B><C>840</C><D>US Dollar</D></T></R>'
Value 840 should insert into column ISONumbricCOde .
value USD should be insert into ISOCode column.
value 0 should insert into column CurrencyId.
values US Dollar should insert into column ISOName .
View 2 Replies
View Related
Jun 23, 2006
Hi,I have a table with 20.000.000 of tuples.I have been monitoring the performance of the insertion and updates,but not convince me at all.The table have 30 columns, what and 12 of it, are calcultated column.The test that i do was this:1 Insertion with all the columns and calculing the calcultated columnsin the insertion sentence.1 insertion and all the columns calculated in @vars..1 insertion with the basic fields, and 10 updates.And the result was that the last test was the most performant.What is your opinion?
View 4 Replies
View Related
Sep 20, 2007
Hi Experts,
I need to know the best approach to save data in master table and then in detail table.
I know this method but i know it's not a good approach why i will explain
Insertion in Master Table..................................... A
Insertion in Detail Table........................................B
Now if there is any exception occurred while step A then the step B will not take place which is ok but if there is exception while step B then the process A will have completed
i.e the data in master table will be Inserted/Deleted/Updated but there will not be a corresponding action in Detail table which is not good approach.
So please can any one tell me a good approach for this.
View 6 Replies
View Related
May 5, 2008
Hi there
I have a relatively noobish query and I am hoping to get a solution to it.
Heres the query in a nutshell.
I have a 'Type' table which has a 'TypeName' varchar attribute. So when I do a row insert into this Type table, I want a new table created with the value I insert into the 'TypeName' column as the table name.
For example If i insert 'xyz' into the 'Type' table for the 'TypeName' column. I wish for a trigger to fire which will create a table 'xyz' with some set attributes. I am really new to SQL Server and my preliminary googling left me disheartened with the results. So here I am.
I hope I was clear in the way I expressed my doubt and also that the people here might be able to help me out in this quest.
View 2 Replies
View Related
Oct 1, 2007
This is my code through which I insert the data into herm_Gen_Liab_Pre table.set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER proc [dbo].[sample]
--@policyno varchar(9),-- FOR MOLD COVERAGE
--@formno varchar(6) -- FOR MOLD COVERAGE
as
TRUNCATE TABLE herm_Gen_Liab_Pre
SET ANSI_WARNINGS OFF
SET ARITHABORT OFF
DECLARE @genliabpre TABLE
( accmnth varchar(2),
accyear varchar (4),
treffmnth varchar (2),
treffyr varchar (4),
trexmnth varchar (2),
trexyr varchar (4),
statecode varchar (2),
typeofpolicy varchar(2),
exposure varchar(7),
annualstatementlineofbusinesscode varchar(3),
PremiumRecordId varchar (14),
Sublinecode varchar (3),
classcode varchar (5),
incemnth varchar (2),
inceyr varchar (4),
territorycode varchar(3)
--policyno varchar(9),-- for mold coverage
--formno varchar(6), -- for mold coverage
--moldcoverage varchar(1) -- mold coverage value
);
insert into @genliabpre (accmnth,accyear,treffmnth,treffyr,trexmnth,trexyr,
statecode,typeofpolicy,exposure,annualstatementlineofbusinesscode,PremiumRecordId,
Sublinecode,classcode,incemnth,inceyr,territorycode)
select datepart(m,p.EntryDate),
datepart(yy,p.EntryDate),
datepart(m,p.PremiumEffectiveDate),
datepart(yy,p.PremiumEffectiveDate),
datepart(m,p.PolicyExpirationDate),
datepart(yy,p.PolicyExpirationDate),
p.statecode,p.policytypecode,p.ExposureLimitAmt,s.lineofbusinesscode,
s.invoiceno,s.sublinecode,s.ClassCode,
datepart(m,p.PolicyOriginalEffectiveDate),
datepart(yy,p.PolicyOriginalEffectiveDate),s.TerritoryCode
from hermitage.dbo.premiumdirect as p join hermitage.dbo.premiumstatdirect as s
on p.invoiceno = s.invoiceno
where
s.lineofbusinesscode in ('052','152','170','171','175','180')--,'192','194') -- general liability
AND P.STATECODE IN ('NY','NJ','PA','RI','GA')
and p.entrydate between '01/01/2004' and '12/31/2007'
order by p.entrydate
----------------------------INSERT MOLD COVERAGE--------------------------------
/*
SET @policyno = (select cast(pd.policyno as varchar(9))
from HERMITAGE.DBO.premiumdirect pd join HERMITAGE.DBO.policyforms pf
on pd.policyno = pf.policyno
where pd.entrydate between '01/01/2004' and '12/31/2007'
and pf.formno in ('CG2167','CG3131')
and exists
( select policyno from HERMITAGE.DBO.premiumdirect p join
HERMITAGE.DBO.premiumstatdirect ps
on substring(pd.policyno,1,9) = substring(pf.policyno,1,9)
where ps.LineOfBusinessCode in ( '170', '052' , '152' , '171', '175', '180')
and ps.sublinecode in ('334','335','336','337','338','339'))
)
SET @FORMNO = (select PF.FORMNO
from HERMITAGE.DBO.premiumdirect pd join HERMITAGE.DBO.policyforms pf
on pd.policyno = pf.policyno
where pd.entrydate between '01/01/2004' and '12/31/2007'
and pf.formno in ('CG2167','CG3131')
and exists
( select policyno from HERMITAGE.DBO.premiumdirect p join
HERMITAGE.DBO.premiumstatdirect ps
on substring(pd.policyno,1,9) = substring(pf.policyno,1,9)
where ps.LineOfBusinessCode in ( '170', '052' , '152' , '171', '175', '180')
and ps.sublinecode in ('334','335','336','337','338','339'))
)
*/
---------------------------------------------------------------------------------
insert into herm_Gen_Liab_Pre (
AccountingDate,
TransactionEffectiveDate,
TransactionExpirationDate,
StateCode,
typeofpolicy,
exposure,
AnnualStatementLineOfBusinessCode,
PremiumRecordId,
SublineCode,
ClassificationCode,
InceptionDate,
territorycode
)
select accmnth= case accmnth
when '1' then '1'
when '2' then '2'
when '3' then '3'
when '4' then '4'
when '5' then '5'
when '6' then '6'
when '7' then '7'
when '8' then '8'
when '9' then '9'
when '10' then '0'
when '11' then '-'
when '12' then '&' end + + SUBSTRING(accyear,4,1),
treffmnth=case treffmnth
when '1' then '1'
when '2' then '2'
when '3' then '3'
when '4' then '4'
when '5' then '5'
when '6' then '6'
when '7' then '7'
when '8' then '8'
when '9' then '9'
when '10' then '0'
when '11' then '-'
when '12' then '&' end + + substring(treffyr,3,2),
trexmnth = case trexmnth
when '1' then '1'
when '2' then '2'
when '3' then '3'
when '4' then '4'
when '5' then '5'
when '6' then '6'
when '7' then '7'
when '8' then '8'
when '9' then '9'
when '10' then '0'
when '11' then '-'
when '12' then '&' end+ + substring(trexyr,3,3),statecode,
typeofpolicy,exposure,AnnualStatementLineOfBusinessCode,premiumrecordid,Sublinecode,
classcode,incemnth = case incemnth
when '1' then '1'
when '2' then '2'
when '3' then '3'
when '4' then '4'
when '5' then '5'
when '6' then '6'
when '7' then '7'
when '8' then '8'
when '9' then '9'
when '10' then '0'
when '11' then '-'
when '12' then '&' end+ + substring(inceyr,3,2),TerritoryCode
from @genliabpre
exec genliab_state
update herm_Gen_Liab_Pre
set territorycode = '999' where statecode = '31'
update herm_Gen_Liab_Pre
set TransactionTypecode ='1'
----------------------STATE EXCEPTION INDICATOR CODE-----------------------------
update herm_Gen_Liab_Pre
set StateExceptionIndicator = '1' where sublinecode = '325' and classificationcode in ('99935','99936') and statecode = '20'
update herm_Gen_Liab_Pre
set StateExceptionIndicator = '1' where sublinecode = '334'
and classificationcode in ('01412','01418','05113','05114','05118','05123','05124'
,'05125','05213','05223','05224','60010','60011','62003','63010','63011'
,'63012','63013','64500') and statecode = '20'
update herm_Gen_Liab_Pre
set StateExceptionIndicator = '1' where sublinecode in ('345','347') and classificationcode = '80050' and statecode = '20'
select * from herm_Gen_Liab_Pre
The problem is there is a column in this table called riskidCoPaymentPercentageMoldDamageCoverageCode and I have to insert value '1' into that column based on this select query
select cast(pd.policyno as varchar(9)),pf.formno
from premiumdirect pd join policyforms pf on pd.policyno = pf.policyno
where pd.entrydate between '01/01/2004' and '12/31/2007'
and pf.formno in ('CG2167','CG3131')
and exists
( select policyno from premiumdirect p join premiumstatdirect ps
on substring(pd.policyno,1,9) = substring(pf.policyno,1,9)
where ps.LineOfBusinessCode in ( '170', '052' , '152' , '171', '175', '180')
and ps.sublinecode in ('334','335','336','337','338','339'))
order by pd.entrydate
How can i insert implement this. I tried temp variable, was successful partially, so need a better approach to it.
Chintan
View 6 Replies
View Related
Feb 1, 2006
how to restrict data insertion upto 50 MB in a table?
View 1 Replies
View Related
Oct 6, 2006
Hi,
This problem is connected with the query i posted yesterday regarding insertion of global variables. I was able to insert the variable in a table to check its value.
This value is mapped to the global variable in a previous Execute SQL Task. But when I use the same global variable to insert in a table, default value 0 is inserted.
My query is does the global variable declared at the package level does not store the value mapped across multiple tasks in control flow?
How can i insert the value stored in a variable in a table from previous SQL Task.
Can anyone suggest some solution,links to try a workaround?
Thanks in advance.
Regards,
Aman
View 4 Replies
View Related
May 25, 2006
HI,
How to create package in SSIS by applying the business Logic like if the record already exist it should be and update else it should be an insert in the destination table. how to achive this funcality in SQL SERVER 2005 (Business Intelligence studion).
Thanks & Regards
Nagaraj.S
View 4 Replies
View Related
Nov 17, 2015
Consider a 4 tables where 1 of them is considered to be as the parent class and the other 3 are sub-classes and they are disjoint so for every recored i insert in the parent class i want to also insert in one of the subclass according to a condition which checks a certain attribute in the recored that is also entered in the parent class .. how could this be done .
View 8 Replies
View Related
May 2, 2008
Hi,
I found out that executing the procedure SP_INDEXOPTION and setting 'AllowRowLocks' to false i can prevent the sql server from locking rows in a table and 'AllowPageLocks' prevents from pages being locked. I need to preform same operation
in case of tables. I need to perform insertion operations concurrently and acquire required locks manually. Is there a way to stop sqlserver from acquiring locks on the table. I need to disable all the locks (row, page and table).
Thank you in advance.
View 9 Replies
View Related
Jan 10, 2008
Hi,
How can we insert multiple records in a OLEDB destination table for each entry from the source table.
To be more clear, for every record from source we need to insert some 'n' number of records into the destination table. this 'n' changes depending on the record.
how is this achieved.
thanks.
View 4 Replies
View Related
Jun 19, 2008
Hi,
I am having problem in bulk update of a sql server table haning identity column from a datatable( has no identity column) using sqlbulkcopy. I tried several approaches, but it does not show any error nor is the table getting updated. But the identity value seems to getting increased every time.
thanks.
varun
View 6 Replies
View Related
May 23, 2007
I am using SQLBULKCOPY to copy Excel spreadsheet into SQL Express Database table. The copying went okay, but the only problem I have is that few records in one of the columns is NULL.
The column contains numeric and alphnumeric fields in the spreadsheet. After copying, the numeric fields got copy with no complication, but all the alphanumeric fields all becam NULL. Can someone help me with this problem?
View 4 Replies
View Related
Jun 9, 2008
HI,
I have one doubt. Is it possible to transfer data from one SQL Server to other SQL Server over the LAN.( Both the SQL Server database are in different cities)
View 1 Replies
View Related
May 19, 2005
Hello,
I was curious if anyone knew if using SqlBulkCopy in code required any special permissions on the database side. I wasn't sure if more permissions than writing capabilities were needed.
Thanks.
View 1 Replies
View Related
Feb 4, 2007
I need to copy large amounts of data between SQL databases, and between SQL and Access. I've been reading a lot of good explanations of SQLBulkCopy, but the only good examples I've found are written in C# and I work in VB. I'm very new to ASP.NET 2.0 (about a month) and came from Classic ASP, not .NET 1.1. I'm still getting my feet wet and I'm afraid it doesn't take much to confuse me. Can someone point me to a good article or tutorial that includes a clear example written in VB?
Diane
View 1 Replies
View Related
Nov 28, 2007
Hi,
I am trying to use sqlbulkcopy to insert the contents of a data table to a temporary table in the database. The content of data table comes from a csv file. I can add the rows to the data table but it seems they are added as strings and when I try to WriteToServer(datatable) it chokes becuase my table has some int fields. Not sure how to do this.private static TimeSpan DoBulkCopy(string filePath)
{Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
StreamReader sr = new StreamReader(filePath);
prepareTable();string fullFileStr = sr.ReadToEnd();
sr.Close();
sr.Dispose(); string[] lines = fullFileStr.Split('');
DataTable dt=new DataTable() ;string[] sArr =lines[0].Split(',');foreach(string s in sArr)
{dt.Columns.Add(new DataColumn());
}
DataRow row;string finalLine = "";foreach (string line in lines)
{
row = dt.NewRow();finalLine = line.Replace(Convert.ToString('
'), "");row.ItemArray = finalLine.Split(',');
dt.Rows.Add(row);
} SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connectionString"].ToString());System.Data.SqlClient.SqlBulkCopy bc = new System.Data.SqlClient.SqlBulkCopy(cn, SqlBulkCopyOptions.TableLock, null);
bc.BatchSize = dt.Rows.Count;
cn.Open();bc.DestinationTableName = "tmpTable";
bc.WriteToServer(dt); // <--------------- it dies here
cn.Close();
bc.Close(); TimeSpan ts = stopWatch.Elapsed;
stopWatch.Stop();return ts;
}private static void prepareTable()
{SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connectionString"].ToString());
string sql = @"if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tmpTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[tmpTable]; CREATE TABLE [dbo].[tmpTable] ([Remote] [int],[KFP] [int]) ON [PRIMARY]";SqlCommand cmd = new SqlCommand(sql, cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
cmd.Dispose();
}
View 1 Replies
View Related
Feb 29, 2008
Hello.
I have a problem using SqlBulkCopy for updating tables. In fact, I can´t update any table. I use this function for insert big groups of records, but I would like to know how I can configure SqlBulkCopy for update rows. I would like to see if there is a record (primary key exists) then update it.
Is possible?
Greetings.
Tanen.
View 1 Replies
View Related
Apr 25, 2008
Does SqlBulkCopy have any adverse affects on it's target table indexes? I like the performance gain but am worried about creating unnecessary table scans if the indexes/stats are not updated properly after it completes...
View 1 Replies
View Related
Aug 10, 2015
Here is my requirement, How to handle using SSIS.
My flatfile will have multiple columns like :
IDÂ key1Â key2Â key3Â key 4
I have SP which accept 3 parameters ID, Key, Date
NOTE: Key is the coulm name from the Excel. So my sp call look like
sp_insert ID, Key1, date
sp_insert ID, Key2,date
sp_insert ID, Key3,date
View 7 Replies
View Related
Apr 15, 2014
I am facing a problem in writing the stored procedure for multiple search criteria.
I am trying to write the query in the Procedure as follows
Select * from Car
where Price=@Price1 or Price=@price2 or Price=@price=3
and
where Manufacture=@Manufacture1 or Manufacture=@Manufacture2 or Manufacture=@Manufacture3
and
where Model=@Model1 or Model=@Model2 or Model=@Model3
and
where City=@City1 or City=@City2 or City=@City3
I am Not sure of the query but am trying to get the list of cars that are to be filtered based on the user input.
View 4 Replies
View Related
Jul 24, 2015
I'm trying to create an email report which gives a result of multiple results from multiple databases in a table format bt I'm trying to find out if there is a simple format I can use.Here is what I've done so far but I'm having troble getting into html and also with the database column:
EXEC msdb.dbo.sp_send_dbmail
@subject
= 'Job Summary',Â
@profile_name =
'SQL SMTP',
  Â
[code]....
View 3 Replies
View Related
Sep 6, 2006
I'm using SqlBulkCopy. Does anyone know how I can output what row (its column names) are throwing a duplicate primary key message when I bulkCopy.WriteToServer(datatable1)?Thanks
View 1 Replies
View Related
Apr 17, 2007
I have a collection of around 16000 records, and have been trying to find the best way to update the information in the DB. I have done alot of reading about both BulkCopy and Batch Update, but haven't come to any clear solutions as to which performs better. I am not doing any inserting, just getting a dataset from the DB, changing the values, them want to update the Db again. Thanks for any help. Mick
View 8 Replies
View Related
Apr 27, 2007
I'm in the final throws of redesigning a web application, and one of the major improvements is adding in an admin page for the users so they can maintain the data and system structure without the need for us to get involved.
One of the areas I'd held back on was promotion of data from SQL server to SQL server - once .NET 2 framework came in and we moved over to VS2005, I'm now in a position to do this work.
I have tested it and it works perfectly well for small tables, but as soon as I try it on a reasonably large table (860,000 rows) I get the following error in VS's output window:
"A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
Additional information: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding."
The application data is stored on SQL2005 servers, and I am running the application locally. I have tried running the copy from table to table on the same server (different databases) and i get the same result, which leads me to believe that it may be memory related. Running the SQL in SQL Server Management Studio works in just 15 to 20 seconds, but using VS falls over in just under a minute. My connection string has a timeout of 3600.
Here's my C# code:// Execute reader...
using (IDataReader vReader = vCmd.ExecuteReader())
{
// Create SqlBulkCopy...
SqlBulkCopy vBulkData = new SqlBulkCopy(aTargetConn);
// Set destination table name...
vBulkData.DestinationTableName = aTableName;
// Write data...
vBulkData.WriteToServer(vReader);
}
aSourceConn and aTargetConn are the appropriate SqlConnections, and aTableName is the table to be populated with data (previously backed up and emtied of contents).
Any help/advice suggestions gratefully received - if any more info needed please ask.
Thanks
Martin
View 6 Replies
View Related
May 2, 2007
I am trying to import a CSV file into an SQL Server table with the OleDbDataReader and SqlBulkCopy objects, like this: using (OleDbConnection dconn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\mystuff\;Extended Properties="text;HDR=No;FMT=Delimited""))
{
using (OleDbCommand dcmd = new OleDbCommand("select * from mytable.csv", dconn))
{
try
{
dconn.Open();
using (OleDbDataReader dreader = dcmd.ExecuteReader())
{
try
{
using (SqlConnection dconn2 = new SqlConnection(@"data source=MyDBServer;initial catalog=MyDB;user id=mydbid;password=mydbpwd"))
{
using (SqlBulkCopy bc = new SqlBulkCopy(dconn2))
{
try
{
dconn2.Open();
bc.DestinationTableName = "dbo.mytable";
bc.WriteToServer(dreader);
}
finally
{
dconn2.Close();
}
}
}
}
finally
{
dreader.Close();
}
}
}
finally
{
dconn.Close();
}
}
}
A couple of the columns for the destination table use a bit datatype. The CSV files uses the strings "1" and "0" to represent these.When I run this code, it throws this exception:Unhandled Exception: System.InvalidOperationException: The given value of type String from the data source cannot be converted to type bit of the specified target column. ---> System.FormatException: Failed to convert parameter value from a String to a Boolean. ---> System.FormatException: String was not recognized asa valid Boolean. at System.Boolean.Parse(String value) at System.String.System.IConvertible.ToBoolean(IFormatProvider provider) at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType) --- End of inner exception stack trace --- at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType) at System.Data.SqlClient.SqlBulkCopy.ConvertValue(Object value, _SqlMetaDatametadata) --- End of inner exception stack trace --- at System.Data.SqlClient.SqlBulkCopy.ConvertValue(Object value, _SqlMetaDatametadata) at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternal() at System.Data.SqlClient.SqlBulkCopy.WriteRowSourceToServer(Int32 columnCount) at System.Data.SqlClient.SqlBulkCopy.WriteToServer(IDataReader reader) at MyClass.Main()It appears not to accept "1" and "0" as valid strings to convert to booleans. The System.Convert.ToBoolean method appears to work the same way. Is there any way to change this behavior? I discovered if you change the "1" to "true" and "0" to "false" in the CSV file it will accept them.
View 3 Replies
View Related
Jun 15, 2007
I am trying to use SQLBulkCopy to copy from an excel spreadsheet to a table in a SQL database and it is all working fine, but I have to now change the connection to the SQl database to an ODBC connection and it is now erroring.
This is the error I get - keyword not supported 'dsn'
Is it possible to use SQlBulkCopy when using an odbc connection to the SQL database?
View 2 Replies
View Related