mysql - C# INSERT TO access database creates 2 database entries rather than 1 -
I have created a C # program to the data in an Access database combined with some well-known address.
Every time I code runs, I, I get two entries in the database
namespace quotes {public partial class quote form: Form {private OleDbConnection quotescon; Private OLEDB Commands OLDBCMD = New OLEDB Commands (); Private String Connect = @ "Provider = Microsoft.Jet.OlDD.4.0; Data Source = H: \ Excerpt. MDB; Continue Security Information = Wrong"; Public Quotation Form () {QuotesCon = New AllBackNation (Connect); InitializeComponent (); } Private void btn_insert_Click (object sender, EventArgs e) {int quote = Convert.ToInt32 (txtb_Quotenumber.Text); Quotescon.Open (); Oledbcmd.Connection = quotescon; Oledbcmd.CommandText = "Insert Table 1 ([citation counts], account, made, accepted) values ( '+ Quotes +' ','" + this.txtb_name.Text + " ','" + this.date_created. Text + "','" + This.comboBox1.Text + "');"; Oledbcmd.ExecuteNonQuery (); Int temp = oledbcmd.ExecuteNonQuery (); If (floating gt; 0) {txtb_Quotenumber.Text = null; Txtb_name.Text = null; MessageBox.Show ("The entry has been successfully added to the database", "Data added", MessageBoxButtons.OK, MessageBoxIcon.Information); } Other {message box. Show ("The data entry has not been successfully added, please try again", "Failed to add data", message box button. OK, messagebox icon. Error); } CotesCon. Stop it (); }
Because you execute your command twice.
oledbcmd.ExecuteNonQuery ();
and another
int temp = oledbcmd.ExecuteNonQuery ();
Just delete the first. A Transact-SQL statement affects the number of connections and rows against
execution .
But more importantly, you should use Always . String consensation of this type is open for attack.
And your OleDbConnection
and OleDbCommand
.
is to use (OleDbConnection quotes = new OleDbConnection (Connect using)) (OleDbCommand oledbcmd = con.CreateCommand ()) {oledbcmd.CommandText = @ "Insert Table 1 (Using [quote number], account, med, accepted) value (?,?,?,?) "; Oledbcmd.Parameters.AddWithValue ("number", quote); Oledbcmd.Parameters.AddWithValue ("@Account", this.txtb_name.Text); Oledbcmd.Parameters.AddWithValue ("Created", this.date_created.Text); Oledbcmd.Parameters.AddWithValue ("Approved", this.comboBox1.Text); Int temp = oledbcmd.ExecuteNonQuery (); If (floating gt; 0) {txtb_Quotenumber.Text = null; Txtb_name.Text = null; MessageBox.Show ("The entry has been successfully added to the database", "Data added", MessageBoxButtons.OK, MessageBoxIcon.Information); } Other {message box. Show ("The data entry has not been successfully added, please try again", "Failed to add data", message box button. OK, messagebox icon. Error); }}
Comments
Post a Comment