我想在vb.net上的应用程序中创建与数据库的连接 我存储了一个名为“CTable”的过程,如果它们不存在则会创建表
我正在使用此代码:
Dim strConnection As String
strConnection = "Data Source=Localhost; Initial Calalog=Northwind; Integrated Security=True"
Dim MyConn As SqlConnection
Dim cmd As SqlCommand
MyConn = New SqlConnection(strConnection)
Dim query As String = "EXEC CTable"
cmd = New SqlCommand(query, MyConn)
MyConn.Open()
cmd.ExecuteNonQuery()
MyConn.Close()
但是他给出了这个错误: “System.Data.dll中发生了'System.ArgumentException'类型的未处理异常 其他信息:不支持关键字:'initial calalog'“
我的问题是:我应该在我的'strConnection'变量中放置什么才能执行我的CTable过程????
答案 0 :(得分:3)
连接字符串中有拼写错误。尝试“目录”而不是“Calalog”;)
答案 1 :(得分:1)
这个词是Catalog
而不是Calalog
。
所以这应该有效:
strConnection = "Data Source=Localhost; Initial Catalog=Northwind; Integrated Security=True"
请注意,您还必须将CommandType
设置为CommandType.StoredProcedure
。
但是总是使用Using
语句来确保即使出错也会处理非托管资源:
Dim strConnection = "Data Source=Localhost; Initial Catalog=Northwind; Integrated Security=True"
Using MyConn = New sqlclient.SqlConnection()
Using cmd = New SqlClient.SqlCommand("EXEC CTable")
cmd.CommandType = CommandType.StoredProcedure
MyConn.Open()
cmd.ExecuteNonQuery()
End Using
End Using ' also closes the conection
答案 2 :(得分:0)
标准安全
Data Source = serverName \ instanceName; Initial Catalog = myDataBase; User Id = myUsername; Password = myPassword;
可信连接
Data Source = serverName \ instanceName; Initial Catalog = myDataBase; Integrated Security = SSPI;