在C#中安装应用程序后出现数据库更新问题 [英] Database Update problem after making Setup application in C#..

查看:79
本文介绍了在C#中安装应用程序后出现数据库更新问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#winform中创建了一个Windows应用程序。当我在Visual Studio IDE中运行它时它很好(它选择,插入和更新数据库记录)。然后我把这个设置为一个安装项目,在我尝试运行安装程序后,它从数据库表中选择数据,但问题是当我更新&将记录插入该数据库表中,它会给出错误消息操作必须使用可更新的查询。



我的代码是 - :

< pre lang =c#> conn = new OleDbConnection( @ Provider = microsoft.jet.oledb.4.0;数据源= +路径+ \ \ PiyashData.mdb; Jet OLEDB:数据库密码=电源;);
conn.Open();
cmdIn = new OleDbCommand();
DC.SelectSta( select * from Valid 有效);
if (DC.dataSet.Tables [ 有效]。行[ 0 ] [ 0 ]。ToString()。CompareTo (textBox1.Text)== 0
{
DC.CmdStatement( update有效集Pcheck = 1其中PID =' + textBox1.Text + ');
MessageBox.Show( 已成功更新!!!);
this .Dispose();
}



数据库表结构是 - :

字段名称 - >>类型

PID - >>文字

Pcheck - >>是/否

我正在使用Access数据库。



这里DC是一个数据库操作类。

DC CmdStatement方法是 - :

  public   void  CmdStatement( string  stamet)
{
try
{
cmdIn.Connection = conn;
cmdIn.CommandText = stamet;
cmdIn.ExecuteNonQuery();
}
catch (例外e)
{
MessageBox.Show(e.Message);
}
}





感谢高级....

解决方案

看看这个答案,看起来是一样的(乍一看):操作必须使用可更新的查询ms访问权限 [ ^ ]



一个很可能的原因是运行程序的用户没有对数据库文件的读写权限,特别是如果它位于程序文件文件夹中。



所以检查目录和文件如果需要,权限和情绪化。您还可以考虑将数据库文件的位置更改为另一个更易于访问的文件夹。


1)如果 PID 是数字字段,删除'

 DC.CmdStatement(  update有效集Pcheck = 1其中PID = + textBox1.Text); 



2)In MS Access 是/否 | TRUE / FALSE 字段,每 YES / TRUE 值为 -1 并且每个 NO / FALSE 0 (零)。

您可以使用简单的技巧检查它:

  SELECT  CInt(是) AS  YesValue,CInt(NO) AS  NoValue,CInt(TRUE) AS  TrueValue,CInt(FALSE) AS  FalseValue; 


ii遇到了Windows窗体应用程序的这个问题,我找到了一个简单的解决方案,当你创建你的设置项目并从release文件夹中选择想要的项目时,不要选择数据库,只需运行项目并转到应用程序文件夹在用户计算机上创建然后右键单击它选择添加 - >文件选择你的数据库;记得将连接字符串路径设置为应用程序文件夹位于用户计算机上的原始应用程序的app.config文件中的相同位置

示例我有一个将安装在驱动器上的应用程序D:在名为My Application的文件夹中

和数据库在同一文件夹中

连接字符串应如下所示:Provider = Microsoft.ACE.OLEDB.12.0; Data Source = D:\ My Application \ Access File.accdb; Jet OLEDB:Database Password = DbPassword;


I created a windows application in C# winform. when i run this in Visual Studio IDE It's fine(it select,insert and update the database records). then I make this to an setup project, after the setup installation when i tried to run, it select data from database table, but the problem is when i update & Insert records into that database table it give an error message "Operation must use an updateable query".

My code is -:

conn = new OleDbConnection(@"Provider=microsoft.jet.oledb.4.0; Data Source=" + path + "\\PiyashData.mdb;Jet OLEDB:Database Password=power;");
                conn.Open();
cmdIn = new OleDbCommand();
DC.SelectSta("select * from Valid", "Valid");
        if (DC.dataSet.Tables["Valid"].Rows[0][0].ToString().CompareTo(textBox1.Text) == 0)
        {        
              DC.CmdStatement("update Valid set Pcheck=1 where PID='" + textBox1.Text + "'");
              MessageBox.Show("Successfully Updated!!!");
              this.Dispose();
        }


the Database table structure is -:
Field name ->> type
PID ->> text
Pcheck ->> Yes/No
I'm using Access database.

Here DC is an Database manipulation class.
DC CmdStatement methods is -:

 public void CmdStatement(string stamet)
 {
    try
    {                
         cmdIn.Connection = conn;                
         cmdIn.CommandText = stamet;                
         cmdIn.ExecuteNonQuery();
    }
    catch (Exception e)
    {
         MessageBox.Show(e.Message);  
    }
}



thanks in Advanced....

解决方案

Take a look at this answer, seems to be the same (at the first glance): operation must use an updateable query ms access[^]

One quite likely reason is that the user running the program doesn't have read-write access to the database file, especially if it is located in program files folder.

So check the directory and file permissions and moodify them if needed. You can also consider changing the location of the database file to another, more asily accessible folder.


1) If PID is numeric field, remove '.

DC.CmdStatement("update Valid set Pcheck=1 where PID=" + textBox1.Text);


2) In MS Access YES/NO | TRUE/FALSE field, every YES/TRUE value is -1 and every NO/FALSE is 0 (zero).
You can check it, using simple trick:

SELECT CInt(YES) AS YesValue, CInt(NO) AS NoValue, CInt(TRUE) AS TrueValue, CInt(FALSE) AS FalseValue;


i i faced this problem with windows form application and Ive found a simple solution that when you create your set up project and select the wanted project from the release folder don't select the database with it just run the project and go the application folder which will be created at the user machine then right click it chose add -> file select your database; remember to set the connection string path to same place where the application folder is located on the user machine example in app.config file for the origin application
example i have an application that will be installed on the drive D: in folder Named My Application
and the data base is in the same folder
connection string should be like this: Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\My Application \Access File.accdb;Jet OLEDB:Database Password=DbPassword;


这篇关于在C#中安装应用程序后出现数据库更新问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆