<connectionStrings>
<add name="ConnectionString" connectionString="server=.\SQLEXPRESSuid=用户名pwd=密码database=你的数据库名字"/>
</connectionStrings>
上面的server=你的数据库实例名,.\SQLEXPRESS是我这里的一个实例,根据实际修改
private void Button1_Click(object sender,EventArgs e){
//获取用户输入的用户名和密码
var id=txt_id.text
var pwd=txt_pwd.text
var result=0//定义一个变量接受结果
var cmd="select count(*) from zhucedenglu where id=@id and password=@pwd" //sql语句
var constr="" //连接字符串自己写
//定义参数列表
var sqlparas=new SQLParameter[]{
new SQLParameter("@id",DBType.String){value=id},
new SQLParameter("@pwd",DBType.String){value=pwd}
}
//创建连接
using(var sqlcon=new SQLConnection(constr))
{
//创建命令对象
using(var sqlcmd=new SQLCommand(sqlcon,cmd))
{
sqlcmd.Parameters.AddRange(sqlparas) //添加参数
sqlcon.Open() //打开连接
result=sqlcmd.ExcuteScalar() //执行命令
}
}
if(result==1)
{
//TODO:登录成功
}
else
{
//TODO:登录失败
}
}
你有数据库吗?连接你的数据库的位置,加他的 连接条件就可以拉下面是一个连接数据库的模块,大家可以使用它来连接SQL数据库,使用起来比ADO控件还简单:
代码如下:
Public rsNew As ADODB.Recordset
Public CnNew As ADODB.Connection
Public addFlag As Boolean
'连接数据库
Public Function OpenCn() As Boolean
Dim Mag As String
On Error GoTo strErrMag
Set CnNew = New ADODB.Connection
CnNew.ConnectionTimeout = 25
CnNew.Provider = "sqloledb"
CnNew.Properties("data source").value = "" 'SQL服务器的名
CnNew.Properties("initial catalog").value = "pubs"'库名
CnNew.Properties("integrated security").value = "SSPI"'登陆类型
'con.Properties("user id").value = "sa"
'con.Properties("password").value = "wwww"
CnNew.Open
OpenCn = True
addFlag = True
Exit Function
strErrMag:
Mag = "数据库末连接"
Call MsgBox(Mag, vbOKCancel, "错误:数据库连接")
addFlag = False
End
End Function
Public Sub Clocn()
'闭关数据库
On Error Resume Next
If CnNew.State <>adStateClosed Then CnNew.Close
Set CnNew = Nothing
End Sub
Public Function OpenRs(ByVal strSql As String) As Boolean
'连接数据库记录集
Dim Mag As String
Dim rpy As Boolean
On Error GoTo strErrMag
Set rsNew = New ADODB.Recordset
If addFlag = False Then rpy = OpenCn
With rsNew
.ActiveConnection = CnNew
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open strSql
End With
addFlag = True
OpenRs = True
Exit Function
strErrMag:
Mag = "数据库数据集末连接"
Call MsgBox(Mag, vbOKOnly, "错误:数据库连接")
OpenRs = False
End
End Function
Public Sub cloRs()
'闭关数据集
On Error Resume Next
If rsNew.State <>adStateClosed Then rsNew.Close
Set rsNew = Nothing
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)