Solved

Inserting data into a SQL table from a C# program

Posted on 2010-08-27
9
205 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
Comment Utility
0
 

Author Comment

by:John500
Comment Utility
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
Comment Utility
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
 

Author Comment

by:John500
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:John500
Comment Utility
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
Comment Utility
Select the "Contact" table so that the DataSource is set to the Contacts table.
0
 

Author Comment

by:John500
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now