SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

asher
Starting Member

Netherlands
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,
Adrian.


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()
{
this.name = "1";// 0 name;
this.company ="2";// 1 company;
this.street="3";//2 street;
this.pobox = "4";// 3 pobox;
this.city ="5";//4 city;
this.telephone ="6";//5 telephone;
this.email="7";//6 email;
this.customer_key="abc";//7 customer_key;

try
{
using(SqlConnection my_connection = new SqlConnection (@"Data Source=.\SQLExpress;Integrated Security=true;AttachDbFilename=C:\NewDatabase\NewTest.mdf;User Instance=true;"))
{
my_connection.Open();
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_table.Rows.Add(dr);

my_adapter.Update(my_dataset, "Customers2");
my_table.AcceptChanges();
}
}
}
my_connection.Close();
}
}
catch (Exception x)
{
new Warning("Writing to Customers\r\n" + x.Message);
}
}
}
}

LoztInSpace
Aged Yak Warrior

939 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

asher
Starting Member

Netherlands
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  
 New 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.05 seconds. Powered By: Snitz Forums 2000