如何使用字段是/否数据类型更新数据库...我想将数据从False更新为True。我有我的代码,它不会工作.. [英] How Do I Update The Database With Field Yes/No Datatype...I Want To Update The Data From False To True. I Have My Code And It Wont Work..

查看:131
本文介绍了如何使用字段是/否数据类型更新数据库...我想将数据从False更新为True。我有我的代码,它不会工作..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

con.Open();
command = new OleDbCommand("select * from Instructor where insno=? AND inclass=?", con);
command.Parameters.Add("@insno", OleDbType.VarChar).Value=txtid.Text;
command.Parameters.Add("@inclass", OleDbType.Boolean).Value=false;
adapter.SelectCommand = command;
OleDbDataReader reader = command.ExecuteReader();
if (reader.Read() == true)
{
    command = new OleDbCommand("UPDATE Instructor SET inclass = ? WHERE insno=?", con);
    command.Parameters.Add("@insno", OleDbType.VarChar).Value = txtid.Text;
    command.Parameters.Add("@inclass", OleDbType.Boolean).Value = false;
    adapter.UpdateCommand = command;
    adapter.UpdateCommand.ExecuteNonQuery();
{
    MessageBox.Show("!!!!!");
}
con.Close();

推荐答案

您的参数声明顺序不正确。代码看起来不错。根据查询, inclass 参数应首先出现,然后 insno

Your order of parameter declaration is incorrect. Otherwise code looks good. According to the query, inclass parameter should come first and then insno.
command = new OleDbCommand("UPDATE Instructor SET inclass = ? WHERE insno=?", con);
    
command.Parameters.Add("@inclass", OleDbType.Boolean).Value = false;
command.Parameters.Add("@insno", OleDbType.VarChar).Value = txtid.Text;


我认为你可以在一次执行时进行更新

I think you can do the update at one execution
con.Open();
bool inclass =false; // set this value true or false 
var command1 = new OleDbCommand("UPDATE Instructor SET inclass = ? WHERE inclass = ? and insno=?", con);
// above line ~inclass will set the invert value of current inclss value if the where condition match 
command1.Parameters.AddWithValue("?", !inclass); 
command1.Parameters.AddWithValue("?", inclass); 
command1.Parameters.AddWithValue("?", txtid.Text);
int rowcount =command1.ExecuteNonQuery(); // if rowcount > 0 means update success 
con.Close();


请参阅我的过去的答案 [ ^ ]。我建议使用命名参数。
Please see my past answers[^]. I suggest to use named parameters.


这篇关于如何使用字段是/否数据类型更新数据库...我想将数据从False更新为True。我有我的代码,它不会工作..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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