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]