【问题标题】:Insert Data to SQL Server Compact将数据插入 SQL Server Compact
【发布时间】:2012-11-06 06:15:38
【问题描述】:

我正在开发一个将数据输入到 SQL Server CE 本地数据库(*.sdf 文件)的 VB.NET 程序。

我的代码是这样的:

    Imports System.Data.SqlClient
    Public Class Form1
    Dim myConnection As SqlConnection
    Dim myCommand As SqlCommand
    Dim ra As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        myConnection = New SqlConnection("Data Source= Database1.sdf")
        myConnection.Open()
        myCommand = New SqlCommand("Insert into website('"TextBox1.Text"')", myConnection)
        ra = myCommand.ExecuteNonQuery()
        MessageBox.Show("Updated")
        myConnection.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = My.Settings.SaveWebsite
    End Sub
End Class

编译时出现三个错误:

错误 1: 未为“公共子新”的参数“连接”指定参数(cmdText As String,connection As System.Data.SqlClient.SqlConnection,事务为 System.Data.SqlClient.SqlTransaction)'。

错误 2
逗号、')' 或预期的有效表达式继续。

错误 3
“System.Data.SqlClient.SqlConnection”类型的值无法转换为“System.Data.SqlClient.SqlTransaction”。

这是我第一次使用 VB.NET,我希望有人知道我在这里做错了什么。

提前致谢

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    好吧,对于初学者来说:当面向 SQL Server Compact 版(*.sdf 文件)时,您需要使用 SqlCeConnectionSqlCeCommand(不是 SqlConnectionSqlCommand -这些适用于成熟的 SQL Server,非紧凑型)

    其次:您的 INSERT 语句不正确 - 正确的 SQL 语法是:

    INSERT INTO Table (col1, col2, ..., colN) 
    VALUES(val1, val2, ..., valN)
    

    【讨论】:

    • SQLCe 连接是问题所在。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 2018-01-10
    • 2016-05-30
    • 1970-01-01
    相关资源
    最近更新 更多