2016-07-15 43 views
-1

我工作了很多,但没有解决这个问题。我正在用access数据库在c#中编写一个程序。访问已经从其底层RCW分离的C#COM对象不能使用

我添加了新的表单,我想用此表单更新数据库。

我以另一种形式获得id; gelenid = Form1.id2;

这是我的代码。我在komut.ExecuteNonQuery()中得到错误;

 isim = comboBox1.Text; 
     yapilacak_is = textBox1.Text.Trim(); 
     bolum = comboBox2.Text.Trim(); 
     tarih = monthCalendar1.SelectionRange.Start.ToShortDateString(); 
     durum = comboBox3.Text; 
     int idm = Convert.ToInt32(gelenid); 
     baglanti.Open(); 
     komut.Connection = baglanti; 
     komut.CommandText = @"UPDATE Bakim SET [Ad][email protected], 
         [Bolum][email protected], 
         [Yapilacak_Is][email protected],   
         [Tarih][email protected], 
         [Durum][email protected] 
         WHERE [Id][email protected]"; 

     komut.Parameters.AddWithValue("@isim", isim); 
     komut.Parameters.AddWithValue("@yapis", yapilacak_is); 
     komut.Parameters.AddWithValue("@bolum", bolum); 
     komut.Parameters.AddWithValue("@tarih", tarih); 
     komut.Parameters.AddWithValue("@durum", durum); 
     // Moved after the Cover parameter 
     komut.Parameters.AddWithValue("@Id", idm); 
     komut.ExecuteNonQuery(); 
     baglanti.Close(); 

     komut.Dispose(); 

我加入komut用于命令

OleDbConnection的baglanti =新的OleDbConnection( “提供者= Microsoft.ACE.Oledb.12.0;数据源= BakimVeritabani.accdb”); OleDbCommand komut = new OleDbCommand();

+0

您需要显示'komut'是如何创建的。此外,你真的应该使用'使用'块,而不是调用'.Dispose()'和'.Close()'maunally。 –

+0

OleDbConnection baglanti = new OleDbConnection(“Provider = Microsoft.ACE.Oledb.12.0; Data Source = BakimVeritabani.accdb”); OleDbCommand komut = new OleDbCommand(); Form1 frmAna =(Form1)System.Windows.Forms.Application.OpenForms [“Form1”]; –

+0

永远不要把代码放在注释中,它应该只在问题中进行并且格式正确。你还需要展示两者如何相互关联。在功能中创建baglanti还是您有一个连接,您继续重复使用 –

回答

0

我有一个连接。

这可能是你的问题,你的连接可能不能处理被打开和关闭多次,你应该每次做一个新的连接,并用using声明它处置。

using(OleDbConnection baglanti = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=BakimVeritabani.accdb") 
using(OleDbCommand komut = new OleDbCommand()) 
{ 
    isim = comboBox1.Text; 
    yapilacak_is = textBox1.Text.Trim(); 
    bolum = comboBox2.Text.Trim(); 
    tarih = monthCalendar1.SelectionRange.Start.ToShortDateString(); 
    durum = comboBox3.Text; 
    int idm = Convert.ToInt32(gelenid); 
    baglanti.Open(); 
    komut.Connection = baglanti; 
    komut.CommandText = @"UPDATE Bakim SET [Ad][email protected], 
        [Bolum][email protected], 
        [Yapilacak_Is][email protected],   
        [Tarih][email protected], 
        [Durum][email protected] 
        WHERE [Id][email protected]"; 

    komut.Parameters.AddWithValue("@isim", isim); 
    komut.Parameters.AddWithValue("@yapis", yapilacak_is); 
    komut.Parameters.AddWithValue("@bolum", bolum); 
    komut.Parameters.AddWithValue("@tarih", tarih); 
    komut.Parameters.AddWithValue("@durum", durum); 
    // Moved after the Cover parameter 
    komut.Parameters.AddWithValue("@Id", idm); 
    komut.ExecuteNonQuery(); 
} 
相关问题