在我的项目中我有两个不同名称的表单我打开了两个连接Sql Server在此代码中出错 [英] In My Project I Am Having Two Forms With Different Names I Opened Two Connections For Sql Server Getting Error In This Code

查看:53
本文介绍了在我的项目中我有两个不同名称的表单我打开了两个连接Sql Server在此代码中出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个表单代码正常工作

  Dim  SCON 作为 字符串 =  数据源= localhost; database = Medha_call; user id = sa; password = user 



这是我的第二个得到错误的NullReferenceExceptionHandling按用户代码

 con.ConnectionString = SCON 
Dim conn2 As 字符串 = 数据源= localhost;初始目录= Medha_call;用户ID = sa;密码=用户
con.ConnectionString = conn2



请帮我解释代码和错误详情?



感谢你...

解决方案

很可能你没有初始化 con var iable。



在使用y变量之前你应该在某个地方有一个代码行,比如

  Dim  con  As  SqlConnection =  SqlConnection 
...



但是,我更喜欢使用Use作为连接,如 SqlConnection [ ^ ]

 使用连接作为  SqlConnection( connectionString)
' ...通过连接做一些事情......
结束 使用


my first form code is working properly

Dim SCON As String = "data source=localhost;database=Medha_call;user id=sa;password=user"


this my second one getting error as NullReferenceExceptionHandling By user code

con.ConnectionString = SCON
Dim conn2 As String = "Data Source=localhost;Initial Catalog=Medha_call;User ID=sa;Password=user"
con.ConnectionString = conn2


please help me the code and the error details?

thanking you...

解决方案

Most likely you haven't initialized the con variable.

Before using yhe con variable you should somewhere have a code line like

Dim con As SqlConnection = New SqlConnection
... 


However, I'd prefer to use Using for the connection as in the example in SqlConnection[^]

Using connection As New SqlConnection(connectionString)
'... do somethig with the connection...
End Using


这篇关于在我的项目中我有两个不同名称的表单我打开了两个连接Sql Server在此代码中出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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