[英]Errors with Connect String in C# and SQL Server
我已經修復了連接字符串,因此在這里顯示了錯誤:
建立與SQL Server的連接時發生與網絡相關或特定於實例的錯誤。 服務器未找到或無法訪問。 驗證實例名稱正確,並且已將SQL Server配置為允許遠程連接。 (提供者:命名管道提供程序,錯誤:40-無法打開與SQL Server的連接)
microsoft SQL Server中的密碼也可以使用, service.msc
MSSQLSERVER
的密碼正在運行,但是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.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;
}
}
}
查看錯誤。 它說您的數據庫不存在。
或者由於某種原因,運行代碼的計算機無法與SQL Server通信。 這可能是由很多原因引起的:防火牆問題,DNS /名稱解析,SQL配置(未配置為接受TCP / IP連接)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.