473,385 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Another try - inserting datset into sql

a
NEW Post

Here's my best guess at how to insert this dataset....
the code runs, but no new records are added to the sql table.

I've read and split a delimited text file into a dataset.
It looks fine in a datagrid (5 columns and 5,000 rows),
but I've been trying, without success, to then insert
the resulting dataset called "result" into a single sql
table that has an auto-increment and PK column called ID,
as well as the 5 columns from the dataset.

Any suggestions on a way to perform the insert of the
"result" dataset into the sql table?

Thanks,

Paul

================================================== ================================================
StreamReader sr = new StreamReader("C:\\test.txt"); //Read From A
File instead of a webrequest

DataSet result = new DataSet(); //The DataSet to Return
result.Tables.Add("MyNewTable"); //Add DataTable to hold the DataSet

result.Tables["MyNewTable"].Columns.Add("CompanyName"); //Add a single
column to the DataTable
result.Tables["MyNewTable"].Columns.Add("FormType"); //Add a single
column
result.Tables["MyNewTable"].Columns.Add("CIK"); //Add a single
column
result.Tables["MyNewTable"].Columns.Add("DateFiled"); //Add a single
column
result.Tables["MyNewTable"].Columns.Add("SECWebAddress"); //Add a single
column

string AllData1 = sr.ReadToEnd(); //Read the rest of the
data in the file.
string[] rows = AllData1.Split("\n".ToCharArray()); //Split off each
row at the Line Feed

foreach(string r in rows) //Now add each row to the
DataSet
{
string delimStr1 = "\t";
string[] items = r.Split(delimStr1.ToCharArray()); //Split the row at the
delimiter
result.Tables["MyNewTable"].Rows.Add(items); //Add the item
}

for (int i = 1; i <= 11; i++) //Remove first 8
rows from the DataTable/DataSet
{
result.Tables["MyNewTable"].Rows.RemoveAt(0);
}

dataGrid1.SetDataBinding(result, "MyNewTable"); //Binds DataGrid to
DataSet,displaying datatable

SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(); //Configure a
data adapter

//Configure the SQL connection
SqlConnection sqlConnection1 = new SqlConnection("workstation id=AMD;packet
size=4096;" +
"integrated security=SSPI;data source=AMD;persist security
info=False;initial catalog=SEC_XBRL_10");

//Generate the 'INSERT' command
SqlCommand sqlInsertCommand1 = new SqlCommand("INSERT INTO
SEC_Index_01(CompanyName, FormType, CIK, " +
"DateFiled, SECWebAddress) VALUES (@CompanyName, @FormType, @CIK,
@DateFiled, @FileName); SELECT ID, " +
"CompanyName, FormType, CIK, DateFiled, SECWebAddress FROM SEC_Index_01
WHERE (ID = @@IDENTITY)", sqlConnection1);

//Add the parameters
sqlInsertCommand1.Parameters.Add("@CompanyName",
System.Data.SqlDbType.VarChar, 8000, "CompanyName");
sqlInsertCommand1.Parameters.Add("@FormType", System.Data.SqlDbType.VarChar,
8000, "FormType");
sqlInsertCommand1.Parameters.Add("@CIK", System.Data.SqlDbType.VarChar,
8000, "CIK");
sqlInsertCommand1.Parameters.Add("@DateFiled",
System.Data.SqlDbType.VarChar, 8000, "DateFiled");
sqlInsertCommand1.Parameters.Add("@FileName", System.Data.SqlDbType.VarChar,
8000, "SECWebAddress");
sqlDataAdapter1.InsertCommand = sqlInsertCommand1; //Set the insert
command

sqlDataAdapter1.Update(result,"SEC_Index_01"); //Perform the
update
Nov 17 '05 #1
2 4002
First off, check the Rowstate of the rows in question or test the dataset
with Debug.Assert(myDataSet.HasChanges());

If this is false or the rowstate of everything is unchanged, then no matter
how many times you call update, nothing will happen. A typical mistake taht
causes this is calling AcceptChanges on the rows or the dataset sometime
before calling Update.

Next, you'll need to check your update command and make sure that it's valid
and that the column mappings are correct and the parameters all match. For
instance, if you hard coded an insert statement without parameters as your
adapter's insert command, it will fire each time a Row with a rowstate of
inserted is found - but since you hard coded the values, it won't
dynamically change the values its inserting

If you could post the code it would probably help to narrow things down.

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"a" <a@discussions.microsoft.com> wrote in message
news:A7**********************************@microsof t.com...
NEW Post

Here's my best guess at how to insert this dataset....
the code runs, but no new records are added to the sql table.

I've read and split a delimited text file into a dataset.
It looks fine in a datagrid (5 columns and 5,000 rows),
but I've been trying, without success, to then insert
the resulting dataset called "result" into a single sql
table that has an auto-increment and PK column called ID,
as well as the 5 columns from the dataset.

Any suggestions on a way to perform the insert of the
"result" dataset into the sql table?

Thanks,

Paul

================================================== ==========================
======================

StreamReader sr = new StreamReader("C:\\test.txt"); //Read From A
File instead of a webrequest

DataSet result = new DataSet(); //The DataSet to Return
result.Tables.Add("MyNewTable"); //Add DataTable to hold the DataSet

result.Tables["MyNewTable"].Columns.Add("CompanyName"); //Add a single
column to the DataTable
result.Tables["MyNewTable"].Columns.Add("FormType"); //Add a single
column
result.Tables["MyNewTable"].Columns.Add("CIK"); //Add a single
column
result.Tables["MyNewTable"].Columns.Add("DateFiled"); //Add a single
column
result.Tables["MyNewTable"].Columns.Add("SECWebAddress"); //Add a single
column

string AllData1 = sr.ReadToEnd(); //Read the rest of the
data in the file.
string[] rows = AllData1.Split("\n".ToCharArray()); //Split off each row at the Line Feed

foreach(string r in rows) //Now add each row to the
DataSet
{
string delimStr1 = "\t";
string[] items = r.Split(delimStr1.ToCharArray()); //Split the row at the
delimiter
result.Tables["MyNewTable"].Rows.Add(items); //Add the item
}

for (int i = 1; i <= 11; i++) //Remove first 8 rows from the DataTable/DataSet
{
result.Tables["MyNewTable"].Rows.RemoveAt(0);
}

dataGrid1.SetDataBinding(result, "MyNewTable"); //Binds DataGrid to
DataSet,displaying datatable

SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(); //Configure a
data adapter

//Configure the SQL connection
SqlConnection sqlConnection1 = new SqlConnection("workstation id=AMD;packet size=4096;" +
"integrated security=SSPI;data source=AMD;persist security
info=False;initial catalog=SEC_XBRL_10");

//Generate the 'INSERT' command
SqlCommand sqlInsertCommand1 = new SqlCommand("INSERT INTO
SEC_Index_01(CompanyName, FormType, CIK, " +
"DateFiled, SECWebAddress) VALUES (@CompanyName, @FormType, @CIK,
@DateFiled, @FileName); SELECT ID, " +
"CompanyName, FormType, CIK, DateFiled, SECWebAddress FROM SEC_Index_01
WHERE (ID = @@IDENTITY)", sqlConnection1);

//Add the parameters
sqlInsertCommand1.Parameters.Add("@CompanyName",
System.Data.SqlDbType.VarChar, 8000, "CompanyName");
sqlInsertCommand1.Parameters.Add("@FormType", System.Data.SqlDbType.VarChar, 8000, "FormType");
sqlInsertCommand1.Parameters.Add("@CIK", System.Data.SqlDbType.VarChar,
8000, "CIK");
sqlInsertCommand1.Parameters.Add("@DateFiled",
System.Data.SqlDbType.VarChar, 8000, "DateFiled");
sqlInsertCommand1.Parameters.Add("@FileName", System.Data.SqlDbType.VarChar, 8000, "SECWebAddress");
sqlDataAdapter1.InsertCommand = sqlInsertCommand1; //Set the insert command

sqlDataAdapter1.Update(result,"SEC_Index_01"); //Perform the
update

Nov 17 '05 #2
a
That IS the code below the double dashed line...so what did i leave out?

What's the simplest way to insert new rows from the dataset that I've
created here into an existing sql table, with a dataadapter or can I just
loop through the dataset somehow and append the rows to the sql table.

I don't need anything fancy here, like validating or checking the existing
rows
against the new rows that I'm trying to insert

Thanks,

Paul
----------------------------------------------------------------

"W.G. Ryan eMVP" wrote:
First off, check the Rowstate of the rows in question or test the dataset
with Debug.Assert(myDataSet.HasChanges());

If this is false or the rowstate of everything is unchanged, then no matter
how many times you call update, nothing will happen. A typical mistake taht
causes this is calling AcceptChanges on the rows or the dataset sometime
before calling Update.

Next, you'll need to check your update command and make sure that it's valid
and that the column mappings are correct and the parameters all match. For
instance, if you hard coded an insert statement without parameters as your
adapter's insert command, it will fire each time a Row with a rowstate of
inserted is found - but since you hard coded the values, it won't
dynamically change the values its inserting

If you could post the code it would probably help to narrow things down.

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"a" <a@discussions.microsoft.com> wrote in message
news:A7**********************************@microsof t.com...
NEW Post

Here's my best guess at how to insert this dataset....
the code runs, but no new records are added to the sql table.

I've read and split a delimited text file into a dataset.
It looks fine in a datagrid (5 columns and 5,000 rows),
but I've been trying, without success, to then insert
the resulting dataset called "result" into a single sql
table that has an auto-increment and PK column called ID,
as well as the 5 columns from the dataset.

Any suggestions on a way to perform the insert of the
"result" dataset into the sql table?

Thanks,

Paul

================================================== ==========================
======================


StreamReader sr = new StreamReader("C:\\test.txt"); //Read From A
File instead of a webrequest

DataSet result = new DataSet(); //The DataSet to Return
result.Tables.Add("MyNewTable"); //Add DataTable to hold the DataSet

result.Tables["MyNewTable"].Columns.Add("CompanyName"); //Add a single
column to the DataTable
result.Tables["MyNewTable"].Columns.Add("FormType"); //Add a single
column
result.Tables["MyNewTable"].Columns.Add("CIK"); //Add a single
column
result.Tables["MyNewTable"].Columns.Add("DateFiled"); //Add a single
column
result.Tables["MyNewTable"].Columns.Add("SECWebAddress"); //Add a single
column

string AllData1 = sr.ReadToEnd(); //Read the rest of the
data in the file.
string[] rows = AllData1.Split("\n".ToCharArray()); //Split off

each
row at the Line Feed

foreach(string r in rows) //Now add each row to the
DataSet
{
string delimStr1 = "\t";
string[] items = r.Split(delimStr1.ToCharArray()); //Split the row at the
delimiter
result.Tables["MyNewTable"].Rows.Add(items); //Add the item
}

for (int i = 1; i <= 11; i++) //Remove first

8
rows from the DataTable/DataSet
{
result.Tables["MyNewTable"].Rows.RemoveAt(0);
}

dataGrid1.SetDataBinding(result, "MyNewTable"); //Binds DataGrid to
DataSet,displaying datatable

SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(); //Configure a
data adapter

//Configure the SQL connection
SqlConnection sqlConnection1 = new SqlConnection("workstation

id=AMD;packet
size=4096;" +
"integrated security=SSPI;data source=AMD;persist security
info=False;initial catalog=SEC_XBRL_10");

//Generate the 'INSERT' command
SqlCommand sqlInsertCommand1 = new SqlCommand("INSERT INTO
SEC_Index_01(CompanyName, FormType, CIK, " +
"DateFiled, SECWebAddress) VALUES (@CompanyName, @FormType, @CIK,
@DateFiled, @FileName); SELECT ID, " +
"CompanyName, FormType, CIK, DateFiled, SECWebAddress FROM SEC_Index_01
WHERE (ID = @@IDENTITY)", sqlConnection1);

//Add the parameters
sqlInsertCommand1.Parameters.Add("@CompanyName",
System.Data.SqlDbType.VarChar, 8000, "CompanyName");
sqlInsertCommand1.Parameters.Add("@FormType",

System.Data.SqlDbType.VarChar,
8000, "FormType");
sqlInsertCommand1.Parameters.Add("@CIK", System.Data.SqlDbType.VarChar,
8000, "CIK");
sqlInsertCommand1.Parameters.Add("@DateFiled",
System.Data.SqlDbType.VarChar, 8000, "DateFiled");
sqlInsertCommand1.Parameters.Add("@FileName",

System.Data.SqlDbType.VarChar,
8000, "SECWebAddress");
sqlDataAdapter1.InsertCommand = sqlInsertCommand1; //Set the

insert
command

sqlDataAdapter1.Update(result,"SEC_Index_01"); //Perform the
update


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tyler Hudson | last post by:
/*Code below raises following errors: Server: Msg 245, Level 16, State 1, Line 6 Syntax error converting the varchar value 'a' to a column of data type int. */ create table #x (i integer, c...
1
by: bockley | last post by:
I am having trouble inserting two queries...I am trying to insert an new item(pk) into a table in one query, and then inserting an item(fk) in another table that relates to the other item just...
6
by: Rob | last post by:
Is there an easy (and fast) way to transfer the contents of a dataset to Excel (without using the Excel object ) ?
5
by: toton | last post by:
Hi, I want to append one vector after another. so like, vector<intv1; ///put some elements to v2. I have a second vector vector<intv2; ///it has some elements. Now I do v1.reserve(v1.size()...
38
by: Neo Geshel | last post by:
I am seeking a method to load one JS file directly into another, *without* having to dynamically write <scripttags. Is there any method whereby I can call only one external JS file using a ...
13
by: imnewtoaccess | last post by:
Hi, I am getting errors while inserting records in one table from another. These are the structures of two tables : file51tm_new RecordType Text
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
0
by: imagetvr | last post by:
Hello I am using windows XP I need a small programme in Visual basic 6 with Access 2000 I have two tables for example one table named student other table named section In student table...
5
by: rando1000 | last post by:
Okay, here's my situation. I need to loop through a file, inserting records based on a number field (in order) and if the character in a certain field = "##", I need to insert a blank record. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.