Solved

Inserting data into a SQL table from a C# program

Posted on 2010-08-27
9
207 Views
Last Modified: 2013-12-17
Greetings,

As the question states, I'd like to insert data into a table using the AdventureWorks database.  I would like to do this using a grid on a Windows Form application.

I have created the application and I'm ready to drag a grid to the Form.  These fields would be inserted into the Persons.Contact table:

      [FirstName] [dbo].[Name] NOT NULL,
      [LastName] [dbo].[Name] NOT NULL,
      [EmailPromotion] [int] NOT NULL CONSTRAINT
      [PasswordHash] [varchar](40) NOT NULL,
      [PasswordSalt] [varchar](10) NOT NULL,
      [rowguid] [uniqueidentifier] ROWGUIDCOL  NOT NULL CONSTRAINT
       [ModifiedDate] [datetime] NOT NULL CONSTRAINT

I've chosen these fields because I have no choice given they are not NULL.  The entire table definition is located in the code below.

I've put this question in the C# area hoping to get the necessary answers for both the C# code and the database.   Please don't assume I know anything in regard to the grid or how to configure the grid to accept data when the user inputs the data.

Thank you very much  in advance for this help!



[ContactID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
	[NameStyle] [dbo].[NameStyle] NOT NULL CONSTRAINT [DF_Contact_NameStyle]  DEFAULT ((0)),
	[Title] [nvarchar](8) NULL,
	[FirstName] [dbo].[Name] NOT NULL,
	[MiddleName] [dbo].[Name] NULL,
	[LastName] [dbo].[Name] NOT NULL,
	[Suffix] [nvarchar](10) NULL,
	[EmailAddress] [nvarchar](50) NULL,
	[EmailPromotion] [int] NOT NULL CONSTRAINT [DF_Contact_EmailPromotion]  DEFAULT ((0)),
	[Phone] [dbo].[Phone] NULL,
	[PasswordHash] [varchar](40) NOT NULL,
	[PasswordSalt] [varchar](10) NOT NULL,
	[AdditionalContactInfo] [xml](CONTENT [Person].[AdditionalContactInfoSchemaCollection]) NULL,
	[rowguid] [uniqueidentifier] ROWGUIDCOL  NOT NULL CONSTRAINT [DF_Contact_rowguid]  DEFAULT (newid()),
	[ModifiedDate] [datetime] NOT NULL CONSTRAINT [DF_Contact_ModifiedDate]  DEFAULT (getdate()),

Open in new window

0
Comment
Question by:John500
  • 6
  • 3
9 Comments
 
LVL 30

Expert Comment

by:MlandaT
ID: 33546847
0
 

Author Comment

by:John500
ID: 33546957
MlandaT,

Appreciate this article.  It starts off with a good subject which is the connection string.  Since I'm dealing with MSSQL and the AdventureWorks database, can you tell me what that connection string would look like to you ?

Thanks!
0
 
LVL 30

Expert Comment

by:MlandaT
ID: 33547017
Definitive guide for connection strings: http://www.connectionstrings.com/sql-server-2005

Samples:

Data Source=dataserver;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

Data Source=dataserver\sqlexpress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

Data Source=192.168.0.1;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

Data Source=192.168.0.1\sqlexpress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:John500
ID: 33547626
Here's what I have so far.  The picture below shows my Form application and the fact that I have a Data Source defined for the AdventureWorks database.

Upon launching the program, I'd like to fill it with every contact in the Person.Contact table.  

The only other thing I need is to be able to insert a person at the end of the list.  I'll take another look at the article you referenced.  Is there anything that comes to mind that I should be doing in order to populate the Window?

Thanks
0
 

Author Comment

by:John500
ID: 33547637
forgot the picture ...

Although the datasource says 'None', it is now set to AdventureWorks
AW-DataGridView.JPG
0
 
LVL 30

Accepted Solution

by:
MlandaT earned 500 total points
ID: 33548405
Select the "Contact" table so that the DataSource is set to the Contacts table.
0
 

Author Comment

by:John500
ID: 33549476
Thanks for you help on this.  I opened another question that gets further into the details of inserting a record.  The first article you provided is good for binding but not for the particulars of adding a row.

If you know how to handle the particulars, here's a link to that question:

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_26436224.html

Thanks
0
 

Author Comment

by:John500
ID: 33549490
I forgot to mention that for this question I was looking for an answer that resembles this:

DataRow row = dataTable.Rows [currRec];
 
row.BeginInsert ();
row ["Title"]        = txtTitle.Text;
row ["FirstName"]    = txtFirstName.Text;
row ["MiddleName"]   = txtMiddleName.Text;
row ["LastName"]     = txtLastName.Text;
row ["Suffix"]       = txtSuffix.Text;
row ["Phone"]        = txtPhone.Text;
row ["EmailAddress"] = txtEmailAddress.Text;
row.EndEdit ();

You were able to get the data to load when you directed me to set the data source to the actual table.  But as I say, that first article didn't come close to addressing inserts.  I'm just sayin ....

Thanks
0
 

Author Closing Comment

by:John500
ID: 33549498
I jumped the gun by asking about inserts when I wasn't even able to load the data from the table.

I should have started with loading and then moved to inserts.

My bad
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question