登录cn.open();错误 [英] Login cn.open(); error

查看:108
本文介绍了登录cn.open();错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我无法登录工作并希望获得一些专业知识,以下是Visual Studios的编码:



Hello, I am having trouble getting my login to work and would like some expertise, below is the coding from Visual Studios:

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的数据库:



Database from SQL Management Studio:

CREATE table tbl_users
(
	userid int PRIMARY KEY IDENTITY,
	username varchar(50) NULL,
	password varchar(50) NULL,
	role varchar (50) NULL,
)







When I try to run the code and enter the username and password, it gives me an error at cn.Open(), saying sqlException was unhandled. Please help





更多详情

.

More Detail

{"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"}





我尝试过:



我尝试按照其他一些网站和YouTube的步骤,但他们给了我同样的错误。这可能是连接字符串的问题吗?



What I have tried:

I tried following the steps on some other sites and YouTube, but they gave me the same error. Could it be a problem with the connection string?

推荐答案

如果SQL服务器实例正在运行,您可以检查它吗?还要检查其端口配置。如果您能够从其他IDE打开SQL服务器,请检查连接字符串中提到的值是否符合SQL服务器安全要求。



此处提供连接字符串示例
Can you check your SQL server instance if it is running, Also check for its port configuration. If you are able to open SQL server from some other IDE, please check the value mentioned in connection string meets the SQL server security requirement.

Connection string sample are available here
https://msdn.microsoft.com/en-us/library/ms156450.aspx#Common connection string examples


这篇关于登录cn.open();错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆