尝试在vb.net中的SQL Server 2005数据库中添加数据

发布于 2024-08-24 13:49:51 字数 1197 浏览 6 评论 0原文

有人可以帮助我吗,我只是一个初学者,想学习使用 vb.net 操作 SQL Server 数据。我已经体验过在 ms access 中操作数据。所以我只是回收了我在这里使用的代码。但不幸的是我得到了这个错误:

Object reference not set to an instance of an object.

另一个问题是,我不确定我在 SqlConnection 中输入的内容是否与 SQL Server 中的内容相匹配。这是 sql server Management Studio Express 的屏幕截图:

[http://screencast.com/t /Y2Q0NWRhYTA][1]

Imports system.data.sqlclient

'declarations
 Dim idnum As String
    Dim lname As String
    Dim fname As String
    Dim skul As String

    Dim sqlcon As SqlConnection
    Dim sqlcom As SqlCommand  

idnum = TextBox1.Text
        lname = TextBox2.Text
        fname = TextBox3.Text
        skul = TextBox4.Text

sqlcon.open

            sqlcon = New SqlConnection("Data Source=SENBONZAKURA\SQLEXPRESS;Initial Catalog=testing;User ID=SenbonZakura/Rew;")
            sqlcom = New SqlCommand("select * from [student]", sqlcon)

            Dim strsql As String = "insert into [student]([ID], [LASTNAME], [FIRSTNAME], [SCHOOL]) values('" + idnum + "','" + lname + "','" + fname + "','" + skul + "')"


            sqlcom.ExecuteNonQuery()

Could someone please help me in here, I'm just a beginner who want to learn on manipulating SQL Server data using vb.net. I have already experienced manipulating data in ms access. So I just recycled the code that I used in here. But unfortunately I got this error:

Object reference not set to an instance of an object.

And another problem is that, I'm not really sure if what I have inputted in the SqlConnection matches what is in SQL Server. Here is a screen shot of sql server management studio express:

[http://screencast.com/t/Y2Q0NWRhYTA][1]

Imports system.data.sqlclient

'declarations
 Dim idnum As String
    Dim lname As String
    Dim fname As String
    Dim skul As String

    Dim sqlcon As SqlConnection
    Dim sqlcom As SqlCommand  

idnum = TextBox1.Text
        lname = TextBox2.Text
        fname = TextBox3.Text
        skul = TextBox4.Text

sqlcon.open

            sqlcon = New SqlConnection("Data Source=SENBONZAKURA\SQLEXPRESS;Initial Catalog=testing;User ID=SenbonZakura/Rew;")
            sqlcom = New SqlCommand("select * from [student]", sqlcon)

            Dim strsql As String = "insert into [student]([ID], [LASTNAME], [FIRSTNAME], [SCHOOL]) values('" + idnum + "','" + lname + "','" + fname + "','" + skul + "')"


            sqlcom.ExecuteNonQuery()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

大海や 2024-08-31 13:49:51

您正在调用:

sqlcon.open

before:

sqlcon = New SqlConnection("Data Source=SENBONZAKURA\SQLEXPRESS;Initial Catalog=testing;User ID=SenbonZakura/Rew;")

这就是您的空引用的来源。在调用 open 之前,您需要“new”sqlcon。

尝试一下:

sqlcon = New SqlConnection("Data Source=SENBONZAKURA\SQLEXPRESS;Initial Catalog=testing;User ID=SenbonZakura/Rew;")

sqlcon.open

sqlcom = New SqlCommand("select * from [student]", sqlcon)

Dim strsql As String = "insert into [student]([ID], [LASTNAME], [FIRSTNAME], [SCHOOL]) values('" + idnum + "','" + lname + "','" + fname + "','" + skul + "')"


sqlcom.ExecuteNonQuery()

由于我们不了解您的环境,因此很难帮助您处理连接字符串。但这里有一个很棒参考的链接:http://connectionstrings.com/

You're calling:

sqlcon.open

before:

sqlcon = New SqlConnection("Data Source=SENBONZAKURA\SQLEXPRESS;Initial Catalog=testing;User ID=SenbonZakura/Rew;")

That's where your null ref is coming from. You need to "new" sqlcon before calling open on it.

Try this:

sqlcon = New SqlConnection("Data Source=SENBONZAKURA\SQLEXPRESS;Initial Catalog=testing;User ID=SenbonZakura/Rew;")

sqlcon.open

sqlcom = New SqlCommand("select * from [student]", sqlcon)

Dim strsql As String = "insert into [student]([ID], [LASTNAME], [FIRSTNAME], [SCHOOL]) values('" + idnum + "','" + lname + "','" + fname + "','" + skul + "')"


sqlcom.ExecuteNonQuery()

It's tougher to help you with the connection string since we don't know your environment. But here is a link to a great reference: http://connectionstrings.com/

ヤ经典坏疍 2024-08-31 13:49:51

Paul 是对的,但除了 Paul 的回答之外,由于 SqlConnection 实现了 IDisposable 接口,我建议您将其包含在 using 语句中。在这篇文章中查看有关使用的更多

信息重要的建议是使用参数将值传递给您的查询。它为您提供安全性,代码更简单...

完整的代码如下所示:

Using sqlcon As New SqlConnection("Data Source=SENBONZAKURA\SQLEXPRESS;Initial Catalog=testing;User ID=SenbonZakura/Rew;")

    sqlcon.Open()
    Dim sqlcom As New SqlCommand()
    sqlcom.Connection = sqlcon

    sqlcom.CommandText = "INSERT INTO [student](ID, LASTNAME, FIRSTNAME, SCHOOL) VALUES (@ParameterID, @ParameterLastName, @ParameterFirstName, @ParameterSchool)"

    sqlcom.Parameters.AddWithValue("@ParameterID", TextBox1.Text)
    sqlcom.Parameters.AddWithValue("@ParameterLastName", TextBox2.Text)
    sqlcom.Parameters.AddWithValue("@ParameterFirstName", TextBox3.Text)
    sqlcom.Parameters.AddWithValue("@ParameterSchool", TextBox4.Text)

    sqlcom.ExecuteNonQuery()

End Using
  • 编辑

请注意,我省略了“select * from [student]” ,您没有使用此查询!

Paul is right, but in addition to Paul answer, since SqlConnection implements interface IDisposable, I suggest you to enclosure it in a using statement. See more about using in this post

Another important suggestion is to use parameters to pass the values to your query. It gives you security, the code is simplier...

The complete code would be something like this:

Using sqlcon As New SqlConnection("Data Source=SENBONZAKURA\SQLEXPRESS;Initial Catalog=testing;User ID=SenbonZakura/Rew;")

    sqlcon.Open()
    Dim sqlcom As New SqlCommand()
    sqlcom.Connection = sqlcon

    sqlcom.CommandText = "INSERT INTO [student](ID, LASTNAME, FIRSTNAME, SCHOOL) VALUES (@ParameterID, @ParameterLastName, @ParameterFirstName, @ParameterSchool)"

    sqlcom.Parameters.AddWithValue("@ParameterID", TextBox1.Text)
    sqlcom.Parameters.AddWithValue("@ParameterLastName", TextBox2.Text)
    sqlcom.Parameters.AddWithValue("@ParameterFirstName", TextBox3.Text)
    sqlcom.Parameters.AddWithValue("@ParameterSchool", TextBox4.Text)

    sqlcom.ExecuteNonQuery()

End Using
  • EDIT

Note that I´ve omitted "select * from [student]", you didn´t use this query!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文