C# 使用C创建登录时出错#
我在登录工作时遇到问题,希望获得一些专业知识,下面是Visual Studio的代码:C# 使用C创建登录时出错#,c#,connection-string,C#,Connection String,我在登录工作时遇到问题,希望获得一些专业知识,下面是Visual Studio的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace CO6009DissertationV5
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("Data Source=SQL2016.FSE.Network;Initial Catalog=db_1430715_co6009_dissertation;Persist Security Info=True;User ID=user_db_1430715_co6009_dissertation");
**cn.Open();**
SqlCommand cmd = new SqlCommand("select * from tbl_users where username = '" + txtUsername.Text + "', and password = '" + txtPassword.Text + "' ", cn);
SqlDataReader dr;
dr = cmd.ExecuteReader();
int count = 0;
while (dr.Read())
{
count += 1;
}
if (count == 1)
{
MessageBox.Show("OK");
frmWelcome1 formDefault = new frmWelcome1();
formDefault.Show();
}
else if (count > 0)
{
MessageBox.Show("Duplicate username amd password");
}
else
{
MessageBox.Show("Username or password is not correct");
}
txtUsername.Clear();
txtPassword.Clear();
///this.Hide();
//frmWelcome1 formDefault = new frmWelcome1();
//formDefault.Show();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
SQL Management Studio中的数据库:
CREATE table tbl_users
(
userid int PRIMARY KEY IDENTITY,
username varchar(50) NULL,
password varchar(50) NULL,
role varchar (50) NULL,
)
更多细节
{“建立到SQL Server的连接时发生与网络相关或特定于实例的错误。找不到或无法访问该服务器。请验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。(提供程序:命名管道提供程序,错误:40-无法打开到SQL Server的连接)}
当我尝试运行代码并输入用户名和密码时,它在cn.Open()处给我一个错误,说sqlException未处理
请帮助。我认为您的SQL Server服务处于停止状态。转到windows服务并将其置于运行状态。可能您的连接字符串错误。请尝试更改并确保您使用了正确的用户名、密码和数据源。如果这些数据正确,则可能是您的SQL服务未运行。有几点需要检查
此外,您的代码容易出现安全漏洞,如sql注入等。您还应该指出这些ISSE用于生产目的 请查看连接字符串中是否有密码信息?这是我得到的错误: