c#


C# Connection to Access Database


The code runs and compiles without errors. However, no records are inserted into the database. I am using access database.
try
{
OleDbCommand cmd = new OleDbCommand(strAccessSelect, myAccessConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(cmd);
int id = 2;
myAccessConn.Open();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into `Customer` (`cusID`,`cusFname`,`cusLname`,`cusPhone`) values (?,?,?,?);";
cmd.Parameters.Add("#cusID", OleDbType.VarChar).Value = id;
cmd.Parameters.Add("#cusFname", OleDbType.VarChar).Value = c.CusFirstName;
cmd.Parameters.Add("#cusLname", OleDbType.VarChar).Value = c.CusLastName;
cmd.Parameters.Add("#cusPhone", OleDbType.VarChar).Value = c.CusPhone;
Console.WriteLine(cmd.CommandText);
cmd.Connection = myAccessConn;
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine("Error: \n{0}", ex.Message);
Console.ReadKey();
return;
}
finally
{
myAccessConn.Close();
}
Just change
cmd.CommandText = "insert into `Customer` (`cusID`,`cusFname`,`cusLname`,`cusPhone`) values (?,?,?,?);";
to
cmd.CommandText = "insert into `Customer` (`cusID`,`cusFname`,`cusLname`,`cusPhone`) values (#cusID, #cusFname, #cusLname, #cusPhon);";
or remove the # symbols from the parameter names - they aren't needed as they are used for SQL Server, not Access.

Related Links

Simple repository for .Net 2.0
Excel file generated from WinForm app via template is sometimes “InVisible”
Dictionary.FirstOrDefault() how to determine if a result was found
How can I make an indeterminate progress bar in a Python console app?
Which design pattern should I use for encryption?
How to determine what “Ne” port the Adobe PDF printer is on?
How to center a TextBlock inside a GridViewColumn? (Sample xaml included)
subsonic 3 active records paging
Add an extra interface using Castle Dynamic Proxy 2?
What difference does TransactionContext make when adding an object?
Loading XmlDocument via HttpWebRequest
error adding update onto a gridview
Null Reference Problem In Inherited Object
Checking if a stream is empty
Double-click beavior on a TreeNode checkbox
Related to ProcessStartInfo() method in C# [duplicate]

Categories

HOME
asp.net
max
http
indexing
twitter-bootstrap
ns2
abc
pandoc
esxi
overrides
terminal
perl6
ms-access-2010
glympse
collision-detection
wavefront
vsixmanifest
angularjs-ng-repeat
coding-style
jcodemodel
ndepend
velocity
vichuploaderbundle
distinct
watson-dialog
react-select
exchange-transport-agents
powerpivot
django-rest-auth
datazen-server
lotusscript
gnu-screen
policy
datastax-enterprise
nsopenpanel
workflow-foundation
angular-schema-form
multilanguage
ng2-bootstrap
circle
microstrategy
openvms
dom4j
sdl-2
azure-storage-files
word-wrap
latency
irr
qt-quick
google-cloud-shell
standard-error
dynamic-linking
seyren
alfred
jira-agile
scriptengine
nugetgallery
taverna
persian
traminer
vensim
eula
valdr-bean-validation
wingdb
crtdbg.h
system.io.packaging
growl
agent-based-modeling
telerik-appbuilder
xps
dayofweek
facebook-java-api
lastinsertid
thucydides
grunt-contrib-imagemin
azure-pack
windows-scheduler
gmail-imap
blotter
dnsbl
rfc822
m3u
nude.js
capitalization
clgeocoder
execjs
soapexception
overlays
file-processing
designated-initializer
netduino
lwp
glassfish-embedded
zend-acl
opengl-to-opengles
office-2003

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile