Using parameters inserting data into access database
Same as for any other query:
a) Replace actual hardcoded parameters in your OleDbCommand
with placeholders (prefixed with @
),
b) Add instances of OleDbParameter
to the DbCommand.Parameters
property. Parameter names must match placeholder names.
[WebMethod]
public void bookRatedAdd(string title, int rating, string review, string ISBN, string userName)
{
using (OleDbConnection conn = new OleDbConnection(
"Provider=Microsoft.Jet.OleDb.4.0;"+
"Data Source="+Server.MapPath("App_Data\\BookRateInitial.mdb"));
{
conn.Open();
// DbCommand also implements IDisposable
using (OleDbCommand cmd = conn.CreateCommand())
{
// create command with placeholders
cmd.CommandText =
"INSERT INTO bookRated "+
"([title], [rating], [review], [frnISBN], [frnUserName]) "+
"VALUES(@title, @rating, @review, @isbn, @username)";
// add named parameters
cmd.Parameters.AddRange(new OleDbParameter[]
{
new OleDbParameter("@title", title),
new OleDbParameter("@rating", rating),
...
});
// execute
cmd.ExecuteNonQuery();
}
}
}
Inserting data into a access database using parameters
The connection was created but never set on the command object. BTW SqlCommand is a SQL Server Client command.
Instead of :
var cmd = new SqlCommand("INSERT INTO SUMMARY (A, B, C) VALUES (@A, @B, @C)");
Use
var cmd = new OleDbCommand("INSERT INTO SUMMARY (A, B, C) VALUES (?, ?, ?)",connection);
Access doesn't support named parameters in SQL queries. The parameter values will be substitued by position.
how to using INSERT INTO in c# for access database
2 things:
Use Parameters
When field name contains spaces use brackets
OleDbCommand cmd = new OleDbCommand("INSERT INTO Members(name,family,age,[Juncture scholarships]) values(@name,@family,@age,@Js)",con);
cmd.Parameters.AddWithValue("@name",t1.Text);
cmd.Parameters.AddWithValue("@family",t2.Text);
...
cmd.ExecuteNonQuery();
...
Unable to insert data in MS Access using parameterized query in c#
When i had the similar problems, solution was:
If database is part of application it can be copied in a bin
folder - and then application work with it. That is why you can`t find your changes in datatables with MS Access client.
Inserting Bulk data into Access Database using DAO using C#
I expect the error is due to not referencing the recordset object.
Not sure the recFields array is needed. If the recordset pulls fields in the order you want to enter data, just reference the fields by index.rs(k) = recList[k].Test1;
or rs.Fields(k) = recList[k].Test1;
Or if recFields array actually has field names and you want to reference fields explicitly:rs(recFields[k]) = recList[k].Test1;
Inserting data into database with autonumber in VB.net
You need to change your sql to "Insert into Table1 (OrderNo,Product,Qty,TDate) Values(@OrderNo, @Product, @Qty, @TDate)".
The following code works for me.
DataGridView1.AllowUserToAddRows = False
For Each row As DataGridViewRow In DataGridView1.Rows
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=...;"
Using conn As New OleDbConnection(connString)
Using cmd As New OleDbCommand("Insert into Table1 (OrderNo,Product,Qty,TDate) Values(@OrderNo, @Product, @Qty, @TDate)", conn)
cmd.Parameters.AddWithValue("@OrderNo", TxtOrder.Text.ToString)
cmd.Parameters.AddWithValue("@Product", row.Cells("Product").Value)
cmd.Parameters.AddWithValue("@Qty", row.Cells("Qty").Value)
cmd.Parameters.AddWithValue("@TDate", Date.Now.ToString("MM/dd/yyyy"))
If conn.State = ConnectionState.Open Then
conn.Close()
End If
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End Using
End Using
Next
Use Python to insert data into a MS Access Database
Python's Database API Specification lists several ways of doing parametrization:
qmark
: Question mark style, e.g....WHERE name=?
numeric
: Numeric, positional style, e.g....WHERE name=:1
named
: Named style, e.g....WHERE name=:name
format
: ANSI C printf format codes, e.g....WHERE name=%s
pyformat
: Python extended format codes, e.g....WHERE name=%(name)s
Most implementations only support one or maybe two. It looks like pyodbc
uses qmark
-style parameters, not format
-style.
Try this instead:
cursor.execute("INSERT INTO ABCD (Serial) VALUES (?)", (k,))
Related Topics
Reading CSV File and Storing Values into an Array
Can You Overload Controller Methods in ASP.NET MVC
Transparent Control Over Picturebox
How to Display Indirect Data in Jqgrid
How to Bind a Wpf Datagrid to a Variable Number of Columns
Understanding Covariant and Contravariant Interfaces in C#
How to Use Httpwebrequest (.Net) Asynchronously
.Net Process.Start Default Directory
Json.Net Serialize/Deserialize Derived Types
How to Get the Cpu Usage in C#
What Is the Simplest Method of Inter-Process Communication Between 2 C# Processes
Dpi Awareness - Unaware in One Release, System Aware in the Other
Wix Installer Msi Not Installing the Winform App Created With Visual Studio 2017
Comparing Object Properties in C#