0

I have two functions. Insert functions provided by Soner Gönül (thanks),......

Table Name Students

Database

     `Field Name    Data Type
     *StudentID Number
      StudentName   Text
      StudentCNIC   Text
      StudentDOB    Date/Time

*PK

using System.Data.OleDb; 

private void Form1_Load(object sender, EventArgs e)
    {

        myCon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data   Source=|DataDirectory|\Access_and_CSharp.accdb");
        this.studentsTableAdapter.Fill(this.access_and_CSharpDataSet.Students);
    }

Insert Function

private void Insertbtn_Click(object sender, EventArgs e)
    {
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "INSERT INTO Students(StudentID, StudentName, StudentCNIC, StudentDOB) Values(@StudIDTxt, @StudNameTxt, @StudCNCITxt, @StudDOBTxt)";
        cmd.Parameters.AddWithValue("@StudIDTxt", StudIDTxt.Text);
        cmd.Parameters.AddWithValue("@StudNameTxt", StudNameTxt.Text);
        cmd.Parameters.AddWithValue("@StudCNCITxt", StudCNCITxt.Text);
        cmd.Parameters.AddWithValue("@StudDOBTxt", StudDOBTxt.Text);
        cmd.Connection=myCon;   
        myCon.Open();
        cmd.ExecuteNonQuery();
         myCon.Close();
    }

And this is the update function

private void Updatebtn_Click(object sender, EventArgs e)
    {
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "UPDATE [Students] set [StudentName] = ?, [StudentCNIC] = ?, [StudentDOB] = ? WHERE [StudentID] = ?";
        cmd.Parameters.AddWithValue("@StudIDTxt", StudIDTxt.Text);
        cmd.Parameters.AddWithValue("@StudNameTxt", StudNameTxt.Text);
        cmd.Parameters.AddWithValue("@StudCNCITxt", StudCNCITxt.Text);
        cmd.Parameters.AddWithValue("@StudDOBTxt", StudDOBTxt.Text);
        cmd.Connection = myCon;
        myCon.Open();
        int rowsAffected = cmd.ExecuteNonQuery();
        myCon.Close();
    }

Problem 1 - When I use the insert function I can see new data at the front end. But I cannot see the new data in Access. Other times when I close the application and restart, new recorded is not there. If I look in Access application and closed it then open VS2010 application new data is not there. What is going on?

Problem 2 - When I use the update function, data remains updated while the application running first time. This is not true when the application is closed and running again. Where have I gone wrong?

For both of problems can anyone see where the problem(s) is/are?

Thanks in advance

EDIT Updating to say I am looking the following website where I have gone wrong. http://www.c-sharpcorner.com/uploadfile/e628d9/inserting-retrieving-records-from-ms-access-2007-using-odbc/

Update I have Windows 7, MS Access 2007 and VS 2010. I am wondering if this is the problem. If it is then it's probably not worth the trouble. I have downloaded AccessDatabaseEngine but its 32bit so I don't know? Probably make my life easier if I use SQL Server instead of Access.

I think this question has been asked too many times.

4

1 に答える 1

1

まず、データベースがプロジェクトのビルドに含まれていないことを確認してください。

ソリューション エクスプローラーで、データベースを見つけて、[ビルド アクション] = [なし]および[出力ディレクトリにコピー] = [コピーしない]を指定します。

スクリーンショット

データベースは、自分のbinフォルダー以外のフォルダーに配置する必要があります。そうしないと、プロジェクトを起動するたびに、プロジェクト内にある同じデータベースが出力フォルダーにコピーされます。

代わりに、データベースを別の場所 (C:\Program Files (x86)\Common Filesまたは別の場所) に配置して接続します。

あなたのプロジェクトには他にも問題があるかもしれませんが、これは私が飛び出している大きな問題です。

于 2013-03-07T21:49:23.413 に答える