Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Adding data to a table in the database
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

36 Posts

Posted - 12/19/2012 :  16:43:17  Show Profile  Reply with Quote
Could someone please tell me if the code below that I wrote is alright, or if not right, how to put it right?
I have been searching the Net endlessly - but in vain - for an appropriate example.
Many thanks in advance,

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO;

namespace FilesAndDatabase
public sealed class CustomerWrite
string name;
string company;
string street;
string pobox;
string city;
string telephone;
string email;
string customer_key;

//public CustomerWrite(string name, string company, string street, string pobox, string city, string telephone, string email, string customer_key)
public CustomerWrite()
{ = "1";// 0 name; ="2";// 1 company;
this.street="3";//2 street;
this.pobox = "4";// 3 pobox; ="5";//4 city;
this.telephone ="6";//5 telephone;"7";//6 email;
this.customer_key="abc";//7 customer_key;

using(SqlConnection my_connection = new SqlConnection (@"Data Source=.\SQLExpress;Integrated Security=true;AttachDbFilename=C:\NewDatabase\NewTest.mdf;User Instance=true;"))
using (SqlDataAdapter my_adapter = new SqlDataAdapter("SELECT * From Customers2", my_connection))
using (DataSet my_dataset = new DataSet())
using (SqlCommandBuilder bldr = new SqlCommandBuilder(my_adapter))
my_adapter.Fill(my_dataset, "Cusomers2");
DataTable my_table = my_dataset.Tables[0];

DataRow dr = my_table.NewRow();
dr[0]= name;
dr[1]= company;
dr[2]= street;
dr[3]= pobox;
dr[4]= city;
dr[5]= telephone;
dr[6]= email;
dr[7]= customer_key.PadRight(20,' ');

my_adapter.Update(my_dataset, "Customers2");
catch (Exception x)
new Warning("Writing to Customers\r\n" + x.Message);

Aged Yak Warrior

940 Posts

Posted - 12/19/2012 :  17:33:49  Show Profile  Reply with Quote
Personally, I'd either invoke a stored procedure to do this or just generate an insert statement, but then I am a bit old-school.
Go to Top of Page

Starting Member

36 Posts

Posted - 12/19/2012 :  18:21:30  Show Profile  Reply with Quote

Lozt wrote:
> Personally, I'd either invoke a stored procedure to do this or just generate an insert statement, but then I am a bit old-school.

Thank you for your comment. I understand that there are more than one way data can be added to a table in a database. I have no experience with database programming and would like to learn step by step. I would first like to be able to go the data adapter and the insert way, and then to know which way is the best in which circumstances. First I need to know how to code them correctly. (I believe I know how to go the insert way.) So hopefully someone will give give me feedback - correct the code I posted where necessary and can give me some idea which way is the best given which parameters.
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000