Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
asher
Starting Member
36 Posts |
Posted - 2012-12-19 : 16:43:17
|
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
940 Posts |
Posted - 2012-12-19 : 17:33:49
|
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. |
|
|
asher
Starting Member
36 Posts |
Posted - 2012-12-19 : 18:21:30
|
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. |
|
|
|
|
|
|
|