•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 422,977 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,925 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 13490 | Replies: 3
![]() |
•
•
Join Date: Mar 2006
Posts: 20
Reputation:
Rep Power: 3
Solved Threads: 0
Good day, I have been trying to add a new record to the Access Database that has already been connected to the Visual Basic Application (Using Visual Studio 2005) that I have been developing, using the DataSource and the DataAdapter objects, though I'm able to add an Insert query and run it, but the record is not saved in the related database at all.
Can you give me some hints and directions as how to solve the above mentioned problem, thanks.
Can you give me some hints and directions as how to solve the above mentioned problem, thanks.
Access doesnt have transactional and stored proc support and I really really really think you should migrate to MSDE aka SQL express. But if you have to use Access....
[ripped off MSDN]
The problem with this is that it can introduce SQL Injection attacks if used on a website. If you don't know what this is, it goes something like this (old customer id parameter has to be larger for this lets say its char 20 instead of 5)
oldCustomerid = "1;delete customer;";
oops!
Even a poorly written stored procedure would pass this along, but atleast if the customerid was a number, the type would be a number and you would get an error.
If you do use a DBMS, don't do "Select * from..." there is a hidden cost to this. There is additional overhead to get the table column schema first, then the query gets resubmitted as "Select field1, field2, ... fieldn from..."
Lastly, explicitely open the connection so it reminds you to close the connection so there are no memory leaks! If you dont close the connection the objects wont be freed until the GC comes along whenever that may be.
[ripped off MSDN]
private static OleDbDataAdapter CreateCustomerAdapter(
OleDbConnection connection)
{
OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
OleDbCommand command;
OleDbParameter parameter;
// Create the SelectCommand.
command = new OleDbCommand("SELECT * FROM dbo.Customers " +
"WHERE Country = ? AND City = ?", connection);
command.Parameters.Add("Country", OleDbType.VarChar, 15);
command.Parameters.Add("City", OleDbType.VarChar, 15);
dataAdapter.SelectCommand = command;
// Create the UpdateCommand.
command = new OleDbCommand(
"UPDATE dbo.Customers SET CustomerID = ?, CompanyName = ? " +
"WHERE CustomerID = ?", connection);
command.Parameters.Add(
"CustomerID", OleDbType.Char, 5, "CustomerID");
command.Parameters.Add(
"CompanyName", OleDbType.VarChar, 40, "CompanyName");
parameter = command.Parameters.Add(
"oldCustomerID", OleDbType.Char, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;
dataAdapter.UpdateCommand = command;
//do the same for the insert and delete commands on the data adapter
return dataAdapter;
}The problem with this is that it can introduce SQL Injection attacks if used on a website. If you don't know what this is, it goes something like this (old customer id parameter has to be larger for this lets say its char 20 instead of 5)
oldCustomerid = "1;delete customer;";
oops!
Even a poorly written stored procedure would pass this along, but atleast if the customerid was a number, the type would be a number and you would get an error.
If you do use a DBMS, don't do "Select * from..." there is a hidden cost to this. There is additional overhead to get the table column schema first, then the query gets resubmitted as "Select field1, field2, ... fieldn from..."
Lastly, explicitely open the connection so it reminds you to close the connection so there are no memory leaks! If you dont close the connection the objects wont be freed until the GC comes along whenever that may be.
Venjense
![]() |
•
•
•
•
•
•
•
•
DaniWeb C# Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Writing to an Access Database (Visual Basic 4 / 5 / 6)
- i cant save data on an access database (C#)
- Inserting a new Access Database Record (VB.NET)
- Inserting Data into Access Database (Java)
Other Threads in the C# Forum
- Previous Thread: MDI Child Form Placement
- Next Thread: I want to learn - How to create and use objects.


Linear Mode