2016-11-25 55 views
0

對於我正在製作的程序,我想在表格中添加countrycode,城市的郵政編碼和城市的名稱。如果此信息已在表格中,則不需要發生任何事情。插入數據以訪問C#數據庫#

但是,新記錄不會插入我的表中。

例如:只有BE,'3580','Beringen'在我的桌子上。我開始我的程序。 首先我插入已經在我的表中的值,但沒有任何事情發生。

第二我嘗試添加一個新值(例如:('BE''3500','Hasselt'))。我得到的消息框:「數據添加成功!」。

之後,我嘗試添加與之前相同的值('BE''3500','Hasselt')。我的程序什麼都不做。

但是當我打開Access時,請看一下表格。沒有新的數據被添加。

我做錯了什麼?

connection.ConnectionString = @"Provider = Microsoft.Jet.OLEDB.4.0; Data Source = DeJongDatabase.mdb; Persist Security Info = True"; 

這是我的代碼

static class Zipcodes 
{ 
    public static void checkAndSavePostCode(String country, String zipcode, string city) 
    { 
     Globals.connection.Open(); 
     OleDbCommand command = new OleDbCommand(); 
     command.Connection = Globals.connection; 
     command.CommandText = string.Format("SELECT * FROM Zipcodes WHERE CountryCode = @countryCode AND City= @city AND Zipcode= @zipcode"); 
     command.Parameters.AddWithValue("@countyCode", country); 
     command.Parameters.AddWithValue("@city", city); 
     command.Parameters.AddWithValue("@zipcode", zipcode); 
     OleDbDataReader postcodeReader = command.ExecuteReader(); 
     bool exists = false; 

     while (postcodeReader.Read()) 
     { 
      exists = true; 
     } 
     postcodeReader.Close(); 
     command.Dispose(); 
     Globals.connection.Close(); 

     OleDbCommand writeCommand = new OleDbCommand(); 
     writeCommand.Connection = Globals.connection; 

     try 
     { 
      Globals.connection.Open(); 
      if (!exists) 
      { 
       if (Globals.connection.State == System.Data.ConnectionState.Open) 
       { 
        /*writeCommand.CommandText = "INSERT INTO Zipcodes(CountryCode, ZipCode, City) VALUES(@countryCode, @zipcode, @city)"; 
        writeCommand.Parameters.AddWithValue("@countyCode", country); 
        writeCommand.Parameters.AddWithValue("@city", city); 
        writeCommand.Parameters.AddWithValue("@zipcode", zipcode); */ 

        writeCommand.CommandText = "INSERT INTO Zipcodes(CountryCode, ZipCode, City) VALUES(?, ?, ?)"; 
        writeCommand.Parameters.Add(new OleDbParameter("@countryCode", OleDbType.VarChar)).Value = country; 
        writeCommand.Parameters.Add(new OleDbParameter("@zipcode", OleDbType.VarChar)).Value = zipcode; 
        writeCommand.Parameters.Add(new OleDbParameter("@city", OleDbType.VarChar)).Value = city; 

        if (writeCommand.ExecuteNonQuery() > 0) 
        { 
         MessageBox.Show("Data saved successfuly...!"); 
        } 
       } 
       else 
       { 
        MessageBox.Show("FAILED"); 
       } 
      } 
     } 
     catch(OleDbException ex) 
     { 
      MessageBox.Show(ex.Source); 
      MessageBox.Show(ex.ToString()); 
     } 
     finally 
     { 
      Globals.connection.Close(); 
     } 
+2

這通常意味着您正在查看數據庫的副本。 – LarsTech

+0

_「我嘗試添加與以前相同的值,我的程序不會執行任何操作」_您期待它做什麼?如果'exists = true',那麼沒有代碼要執行.. – stuartd

+0

檢查數據庫的位置,你想要更新的位置應該在bin \ Debug文件夾中(你可能在解決方案文件夾中檢查mdb文件) – Nino

回答

0

這工作正常,我的休息。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.OleDb; 

namespace WindowsFormsApplication2 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 

      OleDbConnection conn; 
      conn = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=C:\Users\your_path_here\Northwind.mdb"); 

      conn.Open(); 

      OleDbCommand cmd = conn.CreateCommand(); 

      cmd.CommandText = @"INSERT INTO MyExcelTable([Fname], [Lname], [Address])VALUES('" + textBox1.Text + "', '" + textBox2.Text + "','" + textBox3.Text + "')"; 
      cmd.ExecuteNonQuery(); 
      conn.Close(); 

     } 

     public OleDbConnection myCon { get; set; } 

     private void button2_Click(object sender, EventArgs e) 
     { 

      OleDbConnection conn = new OleDbConnection(); 
      conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ryan\Desktop\Coding\Microsoft Access\Northwind.mdb"; 

      string fstName = textBox1.Text.Trim(); 
      string lstName = textBox2.Text.Trim(); 
      string adres = textBox3.Text.Trim(); 
      OleDbCommand cmd = new OleDbCommand(@"INSERT INTO MyExcelTable (FName, LName, Address) VALUES (@FName, @LName, @Address)") 
      { 
       Connection = conn 
      }; 

      conn.Open(); 

      if (conn.State == ConnectionState.Open) 
      { 
       // you should always use parameterized queries to avoid SQL Injection 
       cmd.Parameters.Add("@FName", OleDbType.VarChar).Value = fstName; 
       cmd.Parameters.Add("@LName", OleDbType.VarChar).Value = lstName; 
       cmd.Parameters.Add("@Address", OleDbType.VarChar).Value = adres; 

       try 
       { 
        cmd.ExecuteNonQuery(); 
        MessageBox.Show(@"Data Added"); 
        conn.Close(); 
       } 
       catch (OleDbException ex) 
       { 
        MessageBox.Show(ex.Source + "\n" + ex.Message); 
        conn.Close(); 
       } 
      } 
      else 
      { 
       MessageBox.Show(@"Connection Failed"); 
      } 
     } 
     } 
    }