本文介绍了将布尔值写入数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些信息写入数据表,由于某种原因,当它为False时,它会转换为True:

I am trying to write some information to a data table and for some reason it is getting converted to True when it is False:

这是我用来将数据写入数据表的代码:

Here is the code that I have used to write the data to the data table:

    Public Sub AddCategory(DBName As String, CatName As String, CatSName As String,
                           Used As Boolean, Lock As Boolean)

        'Create/Open Database Connection
        Dim CategoryConn As SqlConnection = New SqlConnection("Data Source=(local); " &
        "Integrated Security=True")

        If CategoryConn.State = ConnectionState.Closed Then CategoryConn.Open()

        'Change Database
        CategoryConn.ChangeDatabase(DBName)

        'Create The Database Connection Command Structure
        Dim CategoryCommand As New SqlCommand("", CategoryConn)

        Debug.Print("Add Category: Category Name: " & CatName & "; Used: " & Convert.ToInt32(Used) & "; Lock: " & Convert.ToInt32(Lock))

        Dim bUsed As Int32 = Convert.ToInt32(Used)
        Dim bLock As Int32 = Convert.ToInt32(Lock)

        Debug.Print("Add Category 2: Category Name: " & CatName & "; Used: " & bUsed & "; Lock: " & bLock)

        'Create/Run Query to Insert Data into tblCategory
        Try
            With CategoryCommand
                .CommandText = "INSERT INTO tblToolCategory " &
                "(CategoryName, CategoryShortName, CategoryLocked, CategoryUsed) " &
                "VALUES (@CatName, @CatShort, " & bLock & ", " & bUsed & ")"
                .Parameters.AddWithValue("@CatName", CatName)
                .Parameters.AddWithValue("@CatShort", CatSName)
                .Parameters.AddWithValue("@CatLocked", bLock)
                .Parameters.AddWithValue("@Used", bUsed)

                Debug.Print(CategoryCommand.CommandText)

                .ExecuteNonQuery()
            End With

        Catch ex As Exception
            MessageBox.Show(ex.ToString, "modTools | AddCategory")

        Finally
            CategoryCommand.Dispose()

        End Try

        'Close Database Connection
        CategoryCommand.Dispose()
        CategoryConn.Close()

    End Sub

这是3条调试行产生的调试信息:

Here is the debug information that the 3 debug lines produce:

添加类别:类别名称:手动工具;已使用:0;锁:1
添加类别2:类别名称:手动工具;已使用:0;锁:1
插入到tblToolCategory(CategoryName,CategoryShortName,CategoryLocked,CategoryUsed)值(@ CatName,@ CatShort,1、0)
添加类别:类别名称:便携式电动工具;已使用:0;锁:1
添加类别2:类别名称:便携式电动工具;已使用:0;锁:1
插入到tblToolCategory(CategoryName,CategoryShortName,CategoryLocked,CategoryUsed)值(@ CatName,@ CatShort,1、0)
添加类别:类别名称:固定式电动工具;已使用:0;锁:1
添加类别2:类别名称:固定式电动工具;已使用:0;锁:1
插入到tblToolCategory(CategoryName,CategoryShortName,CategoryLocked,CategoryUsed)值(@ CatName,@ CatShort,1、0)
添加类别:类别名称:气动工具;已使用:0;锁:1
添加类别2:类别名称:气动工具;已使用:0;锁:1
插入到tblToolCategory(CategoryName,CategoryShortName,CategoryLocked,CategoryUsed)值(@ CatName,@ CatShort,1、0)


类别名称:使用的手动工具:真锁:真


类别名称:使用的便携式电动工具:True锁:True


类别名称:使用的固定式电动工具:True锁:True


类别名称:使用的气动工具:真锁:真

Add Category: Category Name: Hand Tool; Used: 0; Lock: 1
Add Category 2: Category Name: Hand Tool; Used: 0; Lock: 1
INSERT INTO tblToolCategory (CategoryName, CategoryShortName, CategoryLocked, CategoryUsed) VALUES (@CatName, @CatShort, 1, 0)
Add Category: Category Name: Portable Power Tool; Used: 0; Lock: 1
Add Category 2: Category Name: Portable Power Tool; Used: 0; Lock: 1
INSERT INTO tblToolCategory (CategoryName, CategoryShortName, CategoryLocked, CategoryUsed) VALUES (@CatName, @CatShort, 1, 0)
Add Category: Category Name: Stationary Power Tool; Used: 0; Lock: 1
Add Category 2: Category Name: Stationary Power Tool; Used: 0; Lock: 1
INSERT INTO tblToolCategory (CategoryName, CategoryShortName, CategoryLocked, CategoryUsed) VALUES (@CatName, @CatShort, 1, 0)
Add Category: Category Name: Pnuematic Tool; Used: 0; Lock: 1
Add Category 2: Category Name: Pnuematic Tool; Used: 0; Lock: 1
INSERT INTO tblToolCategory (CategoryName, CategoryShortName, CategoryLocked, CategoryUsed) VALUES (@CatName, @CatShort, 1, 0)


Category Name: Hand Tool Used: True Lock: True


Category Name: Portable Power Tool Used: True Lock: True


Category Name: Stationary Power Tool Used: True Lock: True


Category Name: Pnuematic Tool Used: True Lock: True

在添加类别"和添加类别2"行中,使用的变量为零.将其写入数据表时,将其写入为1.

In the Add Category and the Add Category 2 lines, the used variable is a zero.  When it is written to the data table, it is written as a 1.

哦,这是数据表创建语法:

Oh, and here is the data table creation syntax:

        'Open Database connection
        ProjectConn.Open()

        'Change to the Tool Database
        ProjectConn.ChangeDatabase(strDatabaseName)

        strQuery = "CREATE TABLE tblToolCategory (" &
            "CategoryID int PRIMARY KEY NOT NULL IDENTITY(1,1), " &
            "CategoryName VARCHAR(35) NOT NULL, " &
            "CategoryShortName VARCHAR(35), " &
            "CategoryLocked BIT, " &
            "CategoryUsed BIT)"

        'Create the Tool Category Table
        ToolProjectCommand = New SqlCommand(strQuery, ProjectConn)

        Try
            ToolProjectCommand.ExecuteNonQuery()

        Catch ex As Exception
            MessageBox.Show(ex.ToString)

        End Try


感谢您的输入!


Thanks for any input!!

推荐答案

.CommandText = "INSERT INTO tblToolCategory " &
                "(CategoryName, CategoryShortName, CategoryLocked, CategoryUsed) " &
                "VALUES (@CatName, @CatShort, " & bLock & ", " & bUsed & ")"

对此

.CommandText = "INSERT INTO tblToolCategory " &
                "(CategoryName, CategoryShortName, CategoryLocked, CategoryUsed) " &
                "VALUES (@CatName, @CatShort, @CatLocked, @Used)"


这篇关于将布尔值写入数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 07:09