我尝试使用oledb连接这个简单的代码但连接未打开
Imports System.Data.OleDb
Public Class WebForm2
Inherits System.Web.UI.Page
Protected Sub SubmitButton_Click(sender As Object, e As EventArgs) Handles SubmitButton.Click
Dim connetionString As String
Dim cnn As OleDbConnection
connetionString = "Provider=SQLOLEDB;Data Source=((localdb)\v11.0);Initial Catalog=test;User Id=MyUsername; Password=MyPassword;Integrated Security=SSPI;"
cnn = New OleDbConnection(connetionString)
Try
cnn.Open()
MsgBox("Connection Open ! ")
cnn.Close()
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
我也尝试这个连接字符串
connetionString = "Provider=SQLOLEDB;Data Source=test;Integrated Security=SSPI;"
相同的数据库与sql客户端这个连接字符串
运行良好 Imports System.Data.SqlClient
Public Class WebForm2
Inherits System.Web.UI.Page
Dim globalp As String
Dim globals As String
Private strConn As String = "Data Source=(localdb)\v11.0;Initial Catalog=test;Integrated Security=True"
为了简单起见,没有提及额外的代码。
任何额外需要的东西所以请在评论中提及。
还有一个问题是有些人告诉我这个问题是由SQL Server身份验证引起的
我尝试在sql server中构建的visual studio中更改安全身份验证模式但是我没有获取属性弹出菜单或者在属性的vs角部分,从对象资源管理器,
此代码部分中出现新错误`Imports System.Data.OleDb
公共类WebForm2
Inherits System.Web.UI.Page
Dim globalp As String
Dim globals As String
'Dim strConn As String = "Provider=Provider=Microsoft.Jet.OLEDB.4.0;Data Source=((localdb)\v11.0);Initial Catalog=test;Integrated Security=SSPI;"
Dim strConn As String = "Provider=SQLNCLI11.1;Integrated Security=SSPI;Initial Catalog=Test;Data Source=(localdb)\v11.0;"
Dim conn As OleDbConnection
' Dim strConn As String = "Provider=SQLOLEDB; Data Source=(localdb)\v11.0; Initial Catalog=test; Integrated Security=SSPI"
' Private strConn As String = " Provider=Microsoft.Jet.OLEDB.4.0; Data Source=(localdb)\v11.0;Initial Catalog=test; User Id=MyUsername; Password=MyPassword"
' Private sqlCon As OleDbCommand
'Private strConn As OleDbCommand
'Provider=SQLOLEDB;
Protected Sub SubmitButton_Click(sender As Object, e As EventArgs) Handles SubmitButton.Click
If Page.IsValid Then
' cnn = New OleDbConnection(strConn)
' cnn.Open()
' MsgBox("Connection Open ! ")
' cnn.Close()
'sqlComm.CommandText = "sp1_userinformation"
' sqlComm.CommandType = CommandType.StoredProcedure
' sqlComm.Parameters.AddWithValue("UserName", name.Text)
' sqlComm.Parameters.AddWithValue("pass_word", fname.Text)
' sqlComm.Parameters.AddWithValue("CNIC", (cnic.Text))
' sqlComm.Parameters.AddWithValue("PartialAddress", (limitedtextarea.Text))
' sqlComm.Parameters.AddWithValue("Email", (email.Text))
' sqlCon.Open()
'lcit.Text = sname
' sqlComm.ExecuteNonQuery()
' Dim sname As String = name.Text
Dim sfname As String = fname.Text
Dim scnic As String = cnic.Text
Dim slimit As String = limitedtextarea.Text
Dim semail As String = email.Text
Dim stel As Int64 = Int64.Parse(tel.Text)
Dim stel1 As Int64 = 0 & stel
Dim query As String = String.Empty
Dim sdob As Date = Date.Parse(dob.Text)
query &= "INSERT INTO Userinfo (UserName, pass_word, CNIC, "
query &= " PartialAddress, Email, Telephone,DateOfBirth) "
query &= "VALUES (@colName,@colID, @colPhone, @colBranch,@colCourse,@coldblFee,@dobv)"
' query &= " PartialAddress, Email) "
' query &= "VALUES (@colName,@colID, @colPhone, @colBranch,@colCourse)"
' Using (sqlCon)
'sqlCon = New SqlConnection(strConn)
' Dim sqlComm As New SqlCommand()
' sqlComm.Connection = sqlCon
Using conn As New OleDbConnection(strConn)
Using comm As New OleDbCommand()
With comm
.Connection = conn
.CommandType = CommandType.Text
.CommandText = query
.Parameters.AddWithValue("@colName", username.Text)
.Parameters.AddWithValue("@colID", sfname)
.Parameters.AddWithValue("@colPhone", scnic)
.Parameters.AddWithValue("@colBranch", slimit)
.Parameters.AddWithValue("@colCourse", semail)
.Parameters.AddWithValue("@coldblFee", stel1)
.Parameters.AddWithValue("@dobv", sdob)
End With
' Try
conn.Open()
comm.ExecuteNonQuery()
' Catch(ex as SqlException)
'MessageBox.Show(ex.Message.ToString(), "Error Message")
' End Try
End Using
End Using
' End Using
End If
End Sub`
错误是(必须声明标量变量" @colName"。)
错误位置comm.ExecuteNonQuery()
答案 0 :(得分:1)
尝试使用SQL Server Native Client:
Provider=SQLNCLI11.1;Integrated Security=SSPI;Initial Catalog=Test;Data Source=(localdb)\v11.0;
编辑:
您的代码将是:
Dim connetionString As String
Dim cnn As OleDbConnection
connetionString = "Provider=SQLNCLI11.1;Integrated Security=SSPI;Initial Catalog=Test;Data Source=(localdb)\v11.0;"
cnn = New OleDbConnection(connetionString)
Try
cnn.Open()
MsgBox("Connection Open ! ")
cnn.Close()
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
备注:sql localdb不支持SQL Server标准身份验证,它只适用于Windows集成身份验证。
答案 1 :(得分:0)
OleDbCommand使用位置参数和问号?作为参数标记,选择使用命名参数的SqlCommand。所以sql语句应该是:
INSERT INTO Userinfo
(
UserName, pass_word, CNIC, PartialAddress,
Email, Telephone, DateOfBirth
)
VALUES (?,?, ?, ?,?,?,?)
要向OleDbCommand.Parameters集合添加参数,您无需更改任何内容,只要所有参数名称不同,您就可以使用所需的参数名称。但请记住,您需要按顺序添加参数,因为参数是位置的。