Click here to Skip to main content
15,907,913 members
Home / Discussions / C#
   

C#

 
QuestionRe: Foreach statement Pin
T4AMD19-Aug-07 20:21
T4AMD19-Aug-07 20:21 
AnswerRe: Foreach statement Pin
Eduard Keilholz19-Aug-07 20:34
Eduard Keilholz19-Aug-07 20:34 
Erhm, that's quite something your asking us. There are loads of ways to insert data into a database from any application. The first huge question is your application structure. If you use a neat n-tier app, your database are coded in a seperate project. However to get you going we'll implement the quickest way to insert data in your database, but keep in mind, though the code works, it's might not be a 'best practice' way to implement the functionality.

To implement a quick way to insert data into your database, create a SQL Insert statement. Therefore you need a stringbuilder which is the quickest for string manipulation.

StringBuilder sbInsert = new StringBuilder();
sbInsert.Append("INSERT INTO ");
sbInsert.Append("DatabaseTableName (");
sbInsert.Append("field, field, field, field"); // <-- Those fields are your database table column names
sbInsert.Append(") VALUES (");
sbInsert.Append("'Value 1',"); // <-- This the value in your database for the first field
sbInsert.Append("'Value 2',"); // <-- This the value in your database for the second field
sbInsert.Append("'Value 3',"); // <-- This the value in your database for the third field
sbInsert.Append("'Value 4'"); // <-- This the value in your database for the fourth field
sbInsert.Append(")");

Now you have the SQL statement, you'll only need to execute that query in your SQL Server. Therefore you'll need a SqlCommand object and a SqlConnection object.

SqlConnection sConn = null;
SqlCommand sComm = null;
try
{
using (sConn = new SqlConnection(ConnectionString[^]))
{
sComm = new SqlCommand(sbInsert.ToString(), sConn);
sConn.Open();
sComm.ExecuteNonQuery();
sConn.Close();
}
}
Catch (Exception ex)
{
MessageBox.Show(ex);
}

.: I love it when a plan comes together :.
http://www.zonderpunt.nl

QuestionRe: Foreach statement Pin
T4AMD19-Aug-07 21:40
T4AMD19-Aug-07 21:40 
AnswerRe: Foreach statement Pin
Colin Angus Mackay19-Aug-07 22:30
Colin Angus Mackay19-Aug-07 22:30 
Questiongdtwain Pin
chehreghany19-Aug-07 18:50
chehreghany19-Aug-07 18:50 
QuestionQuestion about Maskedtextbox Pin
cyhsu99919-Aug-07 12:59
cyhsu99919-Aug-07 12:59 
AnswerRe: Question about Maskedtextbox Pin
Rocky#19-Aug-07 21:27
Rocky#19-Aug-07 21:27 
GeneralRe: Question about Maskedtextbox Pin
cyhsu99919-Aug-07 22:15
cyhsu99919-Aug-07 22:15 
Questiondrawing with GDI+ on top of controls Pin
JVilmaire19-Aug-07 11:39
JVilmaire19-Aug-07 11:39 
AnswerRe: drawing with GDI+ on top of controls Pin
Christian Graus19-Aug-07 11:48
protectorChristian Graus19-Aug-07 11:48 
GeneralRe: drawing with GDI+ on top of controls Pin
JVilmaire19-Aug-07 11:54
JVilmaire19-Aug-07 11:54 
GeneralRe: drawing with GDI+ on top of controls Pin
Luc Pattyn19-Aug-07 12:23
sitebuilderLuc Pattyn19-Aug-07 12:23 
GeneralRe: drawing with GDI+ on top of controls Pin
JVilmaire19-Aug-07 15:27
JVilmaire19-Aug-07 15:27 
GeneralRe: drawing with GDI+ on top of controls Pin
Dave Kreskowiak19-Aug-07 15:42
mveDave Kreskowiak19-Aug-07 15:42 
QuestionHow to process a ms-word Doc? Pin
moomoori19-Aug-07 10:56
moomoori19-Aug-07 10:56 
AnswerRe: How to process a ms-word Doc? Pin
Luc Pattyn19-Aug-07 11:01
sitebuilderLuc Pattyn19-Aug-07 11:01 
AnswerRe: How to process a ms-word Doc? Pin
Christian Graus19-Aug-07 11:25
protectorChristian Graus19-Aug-07 11:25 
AnswerRe: How to process a ms-word Doc? Pin
originSH19-Aug-07 22:19
originSH19-Aug-07 22:19 
AnswerRe: How to process a ms-word Doc? Pin
moomoori20-Aug-07 12:24
moomoori20-Aug-07 12:24 
Questiondeleting items from listbox with DEL Pin
mizitras19-Aug-07 10:53
mizitras19-Aug-07 10:53 
AnswerRe: deleting items from listbox with DEL Pin
Luc Pattyn19-Aug-07 10:59
sitebuilderLuc Pattyn19-Aug-07 10:59 
QuestionMOVED TO ASP.NET FORUM [modified] Pin
Malcolm Smart19-Aug-07 10:52
Malcolm Smart19-Aug-07 10:52 
AnswerRe: Generate Doc then Open it Client Side Pin
Judah Gabriel Himango19-Aug-07 11:29
sponsorJudah Gabriel Himango19-Aug-07 11:29 
QuestionNumber of processors Pin
Jay Gatsby19-Aug-07 10:29
Jay Gatsby19-Aug-07 10:29 
AnswerRe: Number of processors Pin
originSH19-Aug-07 10:48
originSH19-Aug-07 10:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.