尝试在vb.net中的SQL Server 2005数据库中添加数据
有人可以帮助我吗,我只是一个初学者,想学习使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在调用:
before:
这就是您的空引用的来源。在调用 open 之前,您需要“new”sqlcon。
尝试一下:
由于我们不了解您的环境,因此很难帮助您处理连接字符串。但这里有一个很棒参考的链接:http://connectionstrings.com/
You're calling:
before:
That's where your null ref is coming from. You need to "new" sqlcon before calling open on it.
Try this:
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/
Paul 是对的,但除了 Paul 的回答之外,由于 SqlConnection 实现了 IDisposable 接口,我建议您将其包含在 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:
Note that I´ve omitted
"select * from [student]"
, you didn´t use this query!