类型“String”的值无法转换为“System.Data.SqlClient.SqlConnection”
谁能帮我解决?类型“String”的值无法转换为“System.Data.SqlClient.SqlConnection”ImportsSystem.Data.SqlCl...
谁能帮我解决?
类型“String”的值无法转换为“System.Data.SqlClient.SqlConnection”
Imports System.Data.SqlClient
Public Class Form2
Dim cnn As SqlConnection '声明这些SQL的类
Private Property SqlConnection As SqlConnection
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
''cnn = New SqlConnection("Initial Catalog=学生成绩管理 ;User ID=sa;password=sasa")
' ''cnn.Open()
Dim cnn As String = "Server=.\SQLEXPRESS;AttachDbFiename=E:\学生成绩管理\学生成绩管理\学生成绩管理.mdf;Integrated Security=Ture;User Instance=Ture;"
Using connection As New SqlConnection(cnn)
connection.Open()
Dim mystry As String = "Select 密码 from 用户 Where 用户名='" & TextBox1.Text & "'"
Dim myadapter As New SqlCommand(mystry, cnn)//这块有错误
Dim myReader As SqlDataReader = myadapter.ExecuteReader
While (myReader.Read = True)
If Val(myReader(0)) = Val(TextBox2.Text) Then
If RadioButton1.Checked = True Then
If Val(myReader(0)) = Val(TextBox2.Text.Substring(0, 3)) Then 展开
类型“String”的值无法转换为“System.Data.SqlClient.SqlConnection”
Imports System.Data.SqlClient
Public Class Form2
Dim cnn As SqlConnection '声明这些SQL的类
Private Property SqlConnection As SqlConnection
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
''cnn = New SqlConnection("Initial Catalog=学生成绩管理 ;User ID=sa;password=sasa")
' ''cnn.Open()
Dim cnn As String = "Server=.\SQLEXPRESS;AttachDbFiename=E:\学生成绩管理\学生成绩管理\学生成绩管理.mdf;Integrated Security=Ture;User Instance=Ture;"
Using connection As New SqlConnection(cnn)
connection.Open()
Dim mystry As String = "Select 密码 from 用户 Where 用户名='" & TextBox1.Text & "'"
Dim myadapter As New SqlCommand(mystry, cnn)//这块有错误
Dim myReader As SqlDataReader = myadapter.ExecuteReader
While (myReader.Read = True)
If Val(myReader(0)) = Val(TextBox2.Text) Then
If RadioButton1.Checked = True Then
If Val(myReader(0)) = Val(TextBox2.Text.Substring(0, 3)) Then 展开
2个回答
展开全部
代码中,你一共定义了两个cnn
1、Dim cnn As SqlConnection '声明这些SQL的类
2、Dim cnn As String = "Server=.\SQLEXPRESS;AttachDbFiename=E:\学生成绩管理\学生成绩管理\学生成绩管理.mdf;Integrated Security=Ture;User Instance=Ture;"
在出错的语句中第二个cnn定义生效,是String类型,非SqlConnection,故报错。
改成connection试试。
1、Dim cnn As SqlConnection '声明这些SQL的类
2、Dim cnn As String = "Server=.\SQLEXPRESS;AttachDbFiename=E:\学生成绩管理\学生成绩管理\学生成绩管理.mdf;Integrated Security=Ture;User Instance=Ture;"
在出错的语句中第二个cnn定义生效,是String类型,非SqlConnection,故报错。
改成connection试试。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询