PC Review


Reply
 
 
Jonathan Wood
Guest
Posts: n/a
 
      5th Nov 2007
Well, I spent about 45 minutes trying to find a simple example that shows
how to add records to a SQL database from C# code.

All I can find is databound stuff, or examples working with Access and who
knows what.

How hard can it be? I want to write some code that adds records to an
existing table. No controls will be involved (bound or otherwise).

Can anyone show me how, or point me in the right direction?

Thanks!

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

 
Reply With Quote
 
 
 
 
TAJ
Guest
Posts: n/a
 
      5th Nov 2007
In short, to get you started...

// namespace
using System.Data.SqlClient;

// Database Connection
SqlConnection conn = new SqlConnection(
"Data Source=(local);Initial Catalog=Northwind;Integrated
Security=SSPI");

// Execute SQL Statement
SqlCommand cmd = new SqlCommand("select * from Customers", conn);

--------------------
taken from -
http://www.csharp-station.com/Tutori.../Lesson01.aspx

TAJ


"Jonathan Wood" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Well, I spent about 45 minutes trying to find a simple example that shows
> how to add records to a SQL database from C# code.
>
> All I can find is databound stuff, or examples working with Access and who
> knows what.
>
> How hard can it be? I want to write some code that adds records to an
> existing table. No controls will be involved (bound or otherwise).
>
> Can anyone show me how, or point me in the right direction?
>
> Thanks!
>
> --
> Jonathan Wood
> SoftCircuits Programming
> http://www.softcircuits.com
>


 
Reply With Quote
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      5th Nov 2007
Jonathan,

I know that they are hard to find a full configured DataAdapter for that
where you have used do one by one the deletes, the updates and the the
insert.

However if your insert/update/delete is a simple one, then you can use the
Commandbuilder.

http://www.vb-tips.com/SQLServerUpdate.aspx

There are more tips on our website using the commandbuilder and only few to
use full written updates. Those needs a lot of code and are therefore not so
well to use as tip.

Cor

 
Reply With Quote
 
Jonathan Wood
Guest
Posts: n/a
 
      5th Nov 2007
Thanks, but that code doesn't add any records. Moreover, I couldn't see
where the list of articles at the link you provided does either.

?

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"TAJ" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> In short, to get you started...
>
> // namespace
> using System.Data.SqlClient;
>
> // Database Connection
> SqlConnection conn = new SqlConnection(
> "Data Source=(local);Initial Catalog=Northwind;Integrated
> Security=SSPI");
>
> // Execute SQL Statement
> SqlCommand cmd = new SqlCommand("select * from Customers", conn);
>
> --------------------
> taken from -
> http://www.csharp-station.com/Tutori.../Lesson01.aspx
>
> TAJ
>
>
> "Jonathan Wood" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Well, I spent about 45 minutes trying to find a simple example that shows
>> how to add records to a SQL database from C# code.
>>
>> All I can find is databound stuff, or examples working with Access and
>> who knows what.
>>
>> How hard can it be? I want to write some code that adds records to an
>> existing table. No controls will be involved (bound or otherwise).
>>
>> Can anyone show me how, or point me in the right direction?
>>
>> Thanks!
>>
>> --
>> Jonathan Wood
>> SoftCircuits Programming
>> http://www.softcircuits.com
>>

>


 
Reply With Quote
 
Jonathan Wood
Guest
Posts: n/a
 
      5th Nov 2007
I'll be adding hundreds of rows and I'm not sure the CommandBuilder would be
the most efficient. I'd really like to know how to add records to a database
from code.

I can't yet tell if the code at that link does what I need or not. I'll
check it out.

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"Cor Ligthert[MVP]" <(E-Mail Removed)> wrote in message
news:CDE6485C-2423-4A60-BD8A-(E-Mail Removed)...
> Jonathan,
>
> I know that they are hard to find a full configured DataAdapter for that
> where you have used do one by one the deletes, the updates and the the
> insert.
>
> However if your insert/update/delete is a simple one, then you can use the
> Commandbuilder.
>
> http://www.vb-tips.com/SQLServerUpdate.aspx
>
> There are more tips on our website using the commandbuilder and only few
> to use full written updates. Those needs a lot of code and are therefore
> not so well to use as tip.
>
> Cor
>


 
Reply With Quote
 
TAJ
Guest
Posts: n/a
 
      5th Nov 2007
Replace the select with your sql insert command

http://www.w3schools.com/sql/sql_insert.asp



"Jonathan Wood" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Thanks, but that code doesn't add any records. Moreover, I couldn't see
> where the list of articles at the link you provided does either.
>
> ?
>
> --
> Jonathan Wood
> SoftCircuits Programming
> http://www.softcircuits.com
>
> "TAJ" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> In short, to get you started...
>>
>> // namespace
>> using System.Data.SqlClient;
>>
>> // Database Connection
>> SqlConnection conn = new SqlConnection(
>> "Data Source=(local);Initial Catalog=Northwind;Integrated
>> Security=SSPI");
>>
>> // Execute SQL Statement
>> SqlCommand cmd = new SqlCommand("select * from Customers", conn);
>>
>> --------------------
>> taken from -
>> http://www.csharp-station.com/Tutori.../Lesson01.aspx
>>
>> TAJ
>>
>>
>> "Jonathan Wood" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> Well, I spent about 45 minutes trying to find a simple example that
>>> shows how to add records to a SQL database from C# code.
>>>
>>> All I can find is databound stuff, or examples working with Access and
>>> who knows what.
>>>
>>> How hard can it be? I want to write some code that adds records to an
>>> existing table. No controls will be involved (bound or otherwise).
>>>
>>> Can anyone show me how, or point me in the right direction?
>>>
>>> Thanks!
>>>
>>> --
>>> Jonathan Wood
>>> SoftCircuits Programming
>>> http://www.softcircuits.com
>>>

>>

>


 
Reply With Quote
 
William Vaughn
Guest
Posts: n/a
 
      6th Nov 2007
Yes, TDS is "tabular data stream". It's the low-level protocol I discuss in
Chapter 2. It's the only interface exposed by SQL Server to the outside
world. All of the data access interfaces like ADO, ADO.NET and the rest use
to communicate with the server. The BCP operators bypass much of the
overhead involved in adding rows.

Ah, did you try this:
http://www.google.com/search?hl=en&q=sqlbulkcopy

Here is a VB.NET example drawn from the book--I hope it will suffice:
It opens a delimited text file and imports the data to SQL Server using the
SqlBulkCopy class.

Imports System.Data.Sqlclient
Imports System.Data.odbc
Public Class Form1
Dim WithEvents bc As SqlBulkCopy
Dim dr As Odbc.OdbcDataReader
Dim cn As SqlConnection
Dim cn2 As OdbcConnection
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
cn = New SqlConnection("data source=betav7;integrated
security=sspi;database=biblio")
cn.Open()
cn2 = New
OdbcConnection("DSN=TextFile;DATABASE=c:\MSFTStock.TXT;Extensions=asc,csv,tab,txt")
cn2.Open()
Dim cmd As New OdbcCommand("SELECT * FROM MSFTStock.TXT", cn2)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
bc = New SqlBulkCopy(cn)
With bc
.BatchSize = 10
.DestinationTableName = "StockData"
.NotifyAfter = 5
.WriteToServer(dr)
End With
Catch exIO As InvalidOperationException
Debug.Print(exIO.ToString)
Catch ex As Exception
MsgBox(ex.ToString)
Finally
Dim cmd2 As New SqlCommand("SELECT Count(*) FROM StockData", cn)
Dim strRows As String
strRows = cmd2.ExecuteScalar().ToString
lblRowsInTable.Text = strRows
cn.Close()
cn2.Close()
End Try

End Sub

Private Sub bc_SqlRowsCopied(ByVal sender As Object, ByVal e As
System.Data.SqlClient.SqlRowsCopiedEventArgs) Handles bc.SqlRowsCopied
lblRowsCopied.Text = e.RowsCopied.ToString
lblRowsCopied.Refresh()
End Sub

End Class


----- Original Message -----
From: "Jonathan Wood" <(E-Mail Removed)>
Newsgroups: microsoft.public.dotnet.framework.adonet
Sent: Monday, November 05, 2007 12:42 PM
Subject: Re: Importing Data


> Bill,
>
>>>>>>

> ADO and ADO.NET (and all of its predecessors after DB-Library) are QUERY
> interfaces, not designed for bulk operations. It's a waste of time and
> resources to try to use the System.Data classes to import more than a few
> rows of data.
>>>>>>

>
> Can you explain why it's a waste of time? Looking at what I've found so
> far, that approach would probably take me, personally, about hundredth of
> the time requires to figure out the other stuff. And that takes into
> consideration the fact that I've been so far unable to find a simple
> example showing how to add records to a database using the System.Data
> classes.
>
>>>>>>

> When it's time to import data into SQL Server, the fastest (by an order of
> magnitude or more) is to use the TDS bulk copy approach. This is exposed
> in a number of ways including:
> a.. The BCP commandline utility.
> b.. TSQL BulkCopy operations
> c.. DTS/SSIS scripts
> d.. ADO.NET SqlBulkCopy method.
> All of these techniques can import data from anything that can be read by
> a .NET data provider, an OLE DB data provider, and ODBC driver or your own
> custom-written data provider that exposes a DataReader. This means other
> database tables, flat files, text files, delimited files--almost anything.
> These routines can import millions of rows in no time. Some Oracle
> customers buy SQL Server just to get SSIS.
>>>>>>

>
> Okay...
>
> I guess TDS stands for something but "tds bulk copy" brings up one a
> single result on Google, and that wasn't much help.
>
> Let's try this: Can you tell me which of these is available with what I
> have (VS2005 and SQL Server Management Studio Express)? Also, any spec of
> details on using any of these would be a tremendous help. Maybe even a
> link of some sort.
>
> And I have your book "Hitchhiker's Guide to Visual Studio and SQL Server."
> Am I understanding correctly that this doesn't step through what I want to
> do anywhere? Or how about an example that shows how to add records to a
> database from C# (with no controls, Access, or anything else other than C#
> and SQL)?
>
>>>>>>

> My latest book details how to use SqlBulkCopy. BOL shows how to do the
> rest. I'll be happy to show it to you if you come to one of my sessions
> this week at DevConnections in Vegas or at my workshop in Vancouver BC on
> the 26th (see www.devweek.com for availability).
> <<<<<
>
> Sounds good, but I'll be far from Vegas at that time.
>
> Thanks.
>
> --
> Jonathan Wood
> SoftCircuits Programming
> http://www.softcircuits.com
>


--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant, Dad, Grandpa
Microsoft MVP
INETA Speaker
www.betav.com
www.betav.com/blog/billva
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------

"Jonathan Wood" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Well, I spent about 45 minutes trying to find a simple example that shows
> how to add records to a SQL database from C# code.
>
> All I can find is databound stuff, or examples working with Access and who
> knows what.
>
> How hard can it be? I want to write some code that adds records to an
> existing table. No controls will be involved (bound or otherwise).
>
> Can anyone show me how, or point me in the right direction?
>
> Thanks!
>
> --
> Jonathan Wood
> SoftCircuits Programming
> http://www.softcircuits.com
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieve records, and all records joined to those records, etc., e Peter Stone Microsoft Access Queries 2 12th Jun 2009 05:41 AM
How to Form/Subform automatically duplicate records everytime I update records. ylho Microsoft Access Forms 0 6th Jun 2006 11:48 PM
to Duane Hookom - how to use Pivot Charts for individual records instead of summing all records Harold Good Microsoft Access Reports 1 21st Dec 2005 06:26 PM
Nslookup query for NS records returns all of the NS records, but not all of the Host records Bob Microsoft Windows 2000 Networking 1 8th Nov 2004 07:03 PM
Nslookup query for NS records returns all of the NS records, but not all of the Host records Bob Microsoft Windows 2000 DNS 2 7th Nov 2004 04:42 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:20 PM.