简体   繁体   English

C#和SQL Server中的连接字符串错误

[英]Errors with Connect String in C# and SQL Server

I have fixed the connection string, so it give me errors show here: 我已经修复了连接字符串,因此在这里显示了错误:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与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. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (提供者:命名管道提供程序,错误:40-无法打开与SQL Server的连接)

The password in microsoft SQL Server has worked also, and in service.msc MSSQLSERVER is running, but Visual Studio shows errors. microsoft SQL Server中的密码也可以使用, service.msc MSSQLSERVER的密码正在运行,但是Visual Studio显示错误。

This is my code: 这是我的代码:

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.Sql;
using System.Data.SqlClient;

namespace Learn_App_Login
{
    public partial class Form1 : Form
    {
        //khai bao bien
        private SqlConnection InitCon;
        private SqlCommand cmd;

        public Form1()
        {
            InitializeComponent();
        }

        //ham ket noi database 
        public static SqlConnection GetCon()
        {
            string strcon = @"data source=LAPTOP-AFR31CFM.;
            initial catalog ='SaleDB';
            user id='LAPTOP-AFR31CFM\D4RKDR4G0N'; password='12@#!';
            Integrated Security=true;";
            SqlConnection con = new SqlConnection(strcon);
            return con;
        }

        private void button1_Click(object sender, EventArgs e)
        {      
            SqlConnection connectionToServer = new SqlConnection();

            SqlConnection con = GetCon();
            con.Open();

            string sqlSelect = "select * from account" +
                               "Where username ='" + txtUsername.Text + "' and password ='" + txtPassword.Text + " ' ";

            // khoi tao doi tuong command
            cmd = new SqlCommand(sqlSelect,con);
            cmd.CommandType = CommandType.Text;

            //khoi tao doi tuong adapter
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);

            //tao datable chua data
            DataTable dt = new DataTable();

            //su dung adapter do data vao table nay
            adapter.Fill(dt);

            //binding eridview voi table
            dgwAccount.DataSource = dt;
        }
    }
}

Look at the error. 查看错误。 It says that your database doesn't exist. 它说您的数据库不存在。

Or for some reason the computer the code was running on could not communicate with the SQL Server. 或者由于某种原因,运行代码的计算机无法与SQL Server通信。 This could be caused by a lot of things: Firewall issues, DNS/Name resolution, SQL Configuration (not configured to accept TCP/IP connection) 这可能是由很多原因引起的:防火墙问题,DNS /名称解析,SQL配置(未配置为接受TCP / IP连接)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM