|
-
Oct 24th, 2004, 12:02 PM
#1
Thread Starter
Frenzied Member
Best way to add, update, and delete a database [* Resolved *]
Hello,
I have a database application and have been experiencing problems adding, updating, and deleting using the data table.
Is there a more efficient and robust way to do this. My code l have used is listed below. Someone told me it is better to use the dataset rather than the datatable. If this is so would it be possible to convert the below code to using the dataset. I am not sure on how to use the syntax.
Code:
cnnTeacher.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\IBS Library System\LibrarySystem.mdbersist Security Info=False";
OleDbDataAdapter daTeacher = new OleDbDataAdapter("SELECT * FROM Teacher",cnnTeacher);
OleDbCommandBuilder cbTeacher = new OleDbCommandBuilder(daTeacher);
daTeacher.Fill(dtTeacher);
DataRow drNewTeacher = dtTeacher.NewRow();
drNewTeacher["TeacherID"] = txtAddIDNumber.Text;
drNewTeacher["FirstName"] = txtAddFirstName.Text;
drNewTeacher["LastName"] = txtAddLastName.Text;
drNewTeacher["E-mail"] = txtAddEmail.Text;
dtTeacher.Rows.Add(drNewTeacher);
daTeacher.Update(dtTeacher); //Error - Syntax error in INSERT INTO
Many thanks in advance,
Steve
Last edited by steve_rm; Oct 28th, 2004 at 06:00 AM.
steve
-
Oct 26th, 2004, 06:55 AM
#2
Addicted Member
Hi,
I personally use all SQL statements if I can and just execute them at the database using a connection.execute method. I always feel that if it is possible without using special objects it must be quicker (SQL statements being just pure text).
Hope this is useful?
DJ
-
Oct 26th, 2004, 07:52 AM
#3
Hyperactive Member
don't use inline sql. use storedprocs (or queries as it looks like you are connecting to access). inline sql is bad cos:
means you have to re-compile/deploy your app if you decide to change some sql
it doesn't benefit from the optimisation that takes place in storedprocs (again not sure whether this happens in access)
security.
i've never ever seen a good reason for using inline sql other than a developers own ****ness. "oh well i needed to dynamicaly build parameters onto the end of the where clause so i thought i'd inline the sql." bollocks!
-
Oct 26th, 2004, 08:05 AM
#4
Addicted Member
very nicely put.....! but the guy has a point...... stored procs are much better.... however, I believe you wanted alternatives to using .NET objects? Well there are two more alternatives in here somewhere.... just have to untangle the code examples from the abuse!
DJ
-
Oct 26th, 2004, 09:18 AM
#5
Thread Starter
Frenzied Member
Hello,
I did the insert using SQL. I am using MS Access so i don't think l can use stored procedures. So is this the best way using access. See the code below.
Can anyone tell me if this is the best way. I am interested in learning more about this.
Code:
cnnBook.ConnectionString = @" Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\IBS Library System\LibraryMIS.mdb;Persist Security Info=False";
cnnBook.Open(); //Open connection
OleDbCommand cmdBook = cnnBook.CreateCommand(); //Create a new command
cmdBook.CommandText = @"INSERT INTO Book(RefNumber,Title,Author,Category,Publisher,YearPublished) VALUES('126','mybook','steve','best books','pub','22/08/2004')";// SQL for inserting
cmdBook.CommandType = CommandType.Text; //type of command, text or store procedure
cmdBook.ExecuteNonQuery(); //Execute the command
cnnBook.Dispose(); //Release all the resources than then close
Many thanks in advance,
Steve
-
Oct 27th, 2004, 02:19 PM
#6
PowerPoster
In your initial post, you only told the dataadapter how to select the data, not how to insert it back.
What you need to do is create a command object that is for inserting. Then set the dataadapters insertcommand property equal to that command.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|