C#Windows应用程序登陆界面如何连接到SQL数据库验证用户名和用户密码,我要详细代码……

不要从网上找,好像都不太好用
2024年11月28日 22:36
有3个网友回答
网友(1):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace MyKTV
{
class DBHelper
{
private string connstr = string.Format("Data Source=服务器名;Initial Catalog=MyKTV(数据库名);User ID=用户名;Pwd=密码");//如果是windows登陆就写(Integrated Security=True),服务器名本机可用(.)点,没有密码Pwd可以不写,
private SqlConnection connection;

public SqlConnection Connection
{
get
{
if (connection == null)
{
connection = new SqlConnection(connstr);
}
return connection;
}
}

public void OpenConnection()
{
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
}
else if (Connection.State == ConnectionState.Broken)
{
Connection.Close();
Connection.Open();
}
}

public void CloseConnection()
{
if (Connection.State == ConnectionState.Open || Connection.State == ConnectionState.Broken)
{
Connection.Close();
}
}
}
}

网友(2):

string connstr = @"Data Source=.\sqlexpress;Initial Catalog=数据库名;Integrated Security=True";
SqlConnection conn = new SqlConnection(connstr);
string sql = "select * from admin where username='" + user + "' and pwd='" + pwd + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
int x=cmd.ExecuteNonQuery();
if (x > 0)
{
//登陆成功!
}
else
{
//登陆失败!
}

网友(3):

string slts = "select UserPassword from PersonInfo where UserName='" + username + "' and Admin='" + Ad + "'";

DataSet ds = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(str, CommandType.Text, slts);

if (ds.Tables[0].Rows.Count > 0)
{
string password = ds.Tables[0].Rows[0]["UserPassword"].ToString();
if (password == rpw)
{
Response.Redirect(Dr);
}
}