4个回答
展开全部
'1 连接数据库(使用命令)
Dim conStr As String = ConfigurationManager.ConnectionStrings("conStr").ConnectionString
'Dim conStr As String = "Data Source=.;Initial Catalog=Hotel;Persist Security Info=True;User ID=sa;Password=123456"
Dim con As SqlConnection = New SqlConnection(conStr)
Dim sql As String = "select * from UserDetail where userName='" & strName & "' and userPwd='" & strPwd & "'"
'Console.WriteLine(sql)
'2 创建命令
Dim cmd As SqlCommand = New SqlCommand(sql, con)
'3 打开连接
con.Open()
'4 执行
Dim reader As SqlDataReader = cmd.ExecuteReader()
'5 访问
If reader.Read() Then
MsgBox(reader(0) & ControlChars.Tab & reader("userPwd"))
'Console.WriteLine(reader(0) & ControlChars.Tab & reader("userPwd"))
Else
MsgBox("查询失败!用户名或者密码错误!")
End If
Console.Read()
'6 关闭
reader.Close()
con.Close()
Dim sql As String = "select * from userdetail where userName=@name and userPwd=@pwd"
Dim p1 As SqlParameter = New SqlParameter("@name", name)
Dim p2 As SqlParameter = New SqlParameter("@pwd", pwd)
Dim reader As SqlDataReader = DBOper.GetReader(sql, p1, p2)
Dim conStr As String = ConfigurationManager.ConnectionStrings("conStr").ConnectionString
'Dim conStr As String = "Data Source=.;Initial Catalog=Hotel;Persist Security Info=True;User ID=sa;Password=123456"
Dim con As SqlConnection = New SqlConnection(conStr)
Dim sql As String = "select * from UserDetail where userName='" & strName & "' and userPwd='" & strPwd & "'"
'Console.WriteLine(sql)
'2 创建命令
Dim cmd As SqlCommand = New SqlCommand(sql, con)
'3 打开连接
con.Open()
'4 执行
Dim reader As SqlDataReader = cmd.ExecuteReader()
'5 访问
If reader.Read() Then
MsgBox(reader(0) & ControlChars.Tab & reader("userPwd"))
'Console.WriteLine(reader(0) & ControlChars.Tab & reader("userPwd"))
Else
MsgBox("查询失败!用户名或者密码错误!")
End If
Console.Read()
'6 关闭
reader.Close()
con.Close()
Dim sql As String = "select * from userdetail where userName=@name and userPwd=@pwd"
Dim p1 As SqlParameter = New SqlParameter("@name", name)
Dim p2 As SqlParameter = New SqlParameter("@pwd", pwd)
Dim reader As SqlDataReader = DBOper.GetReader(sql, p1, p2)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
需要把你的数据文件(自定义.mdf)
从SQL中分离,然后复制到C#项目对应的文件夹里
然后开启SQL SERVER
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
方法里面写:
string myConnectionString = "Data Source=. \\SQLEXPRESS;Integrated Security=True;User Instance=False;
AttachDBFilename=|DataDirectory|自定义.mdf";
这样就能连接了
从SQL中分离,然后复制到C#项目对应的文件夹里
然后开启SQL SERVER
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
方法里面写:
string myConnectionString = "Data Source=. \\SQLEXPRESS;Integrated Security=True;User Instance=False;
AttachDBFilename=|DataDirectory|自定义.mdf";
这样就能连接了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using (SqlConnection conn = new SqlConnection("Server=;Database=;Uid=;Pwd="))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "";
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
}
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "";
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
新建一个数据库连接
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询