![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
System.Data.sqlclient.SqlException:"Login failed for user 'sa'.The uer is not associated with a tru
一段C#程序usingSystem;usingSystem.Collections.Generic;usingSystem.Data.SqlClient;usingSys...
一段C#程序
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
namespace eg_9._1
{
class Program
{
static void Main(string[] args)
{
string connectionString = @"server=localhost;database=Northwind;uid=sa;pwd=sa";//连接SQL数据库的字符串
SqlConnection mySqlConnection = new SqlConnection(connectionString);//创建SqlConnectiion对象并连接到SQL自带Northwind数据库
//创建SqlCommand对象
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
string commandString = "Slect CustomerID,CompanyName,ContactName,Address" + "From Customer Where CustomerID = 'ALKFI'";
mySqlCommand.CommandText = commandString;
//用connection对象的open()方法打开数据库。
mySqlConnection.Open();
//创建sqldatareader对象,并执行ExecuteReader方法。
SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
//读取每行记录
mySqlDataReader.Read();
//显示列属性值
Console.WriteLine("mySqlDataReader[\"CustomerID\"] = " + mySqlDataReader["CustomerID"]);
Console.WriteLine("mySqlDataReader[\"CompanyName\"] = " + mySqlDataReader["CompanyName"]);
Console.WriteLine("mySqlDataReader[\"ContactName\"] = " + mySqlDataReader["ContactName"]);
Console.WriteLine("mySqlDataReader[\"Address\"] = " + mySqlDataReader["Address"]);
mySqlDataReader.Close();//关闭数据库
mySqlConnection.Close();//关闭数据库连接
}
}
}
运行的时候出现System.Data.sqlclient.SqlException:"Login failed for user 'sa'.The uer is not associated with a trusted SQL Server connection.这样的错误,求高人指点 展开
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
namespace eg_9._1
{
class Program
{
static void Main(string[] args)
{
string connectionString = @"server=localhost;database=Northwind;uid=sa;pwd=sa";//连接SQL数据库的字符串
SqlConnection mySqlConnection = new SqlConnection(connectionString);//创建SqlConnectiion对象并连接到SQL自带Northwind数据库
//创建SqlCommand对象
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
string commandString = "Slect CustomerID,CompanyName,ContactName,Address" + "From Customer Where CustomerID = 'ALKFI'";
mySqlCommand.CommandText = commandString;
//用connection对象的open()方法打开数据库。
mySqlConnection.Open();
//创建sqldatareader对象,并执行ExecuteReader方法。
SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
//读取每行记录
mySqlDataReader.Read();
//显示列属性值
Console.WriteLine("mySqlDataReader[\"CustomerID\"] = " + mySqlDataReader["CustomerID"]);
Console.WriteLine("mySqlDataReader[\"CompanyName\"] = " + mySqlDataReader["CompanyName"]);
Console.WriteLine("mySqlDataReader[\"ContactName\"] = " + mySqlDataReader["ContactName"]);
Console.WriteLine("mySqlDataReader[\"Address\"] = " + mySqlDataReader["Address"]);
mySqlDataReader.Close();//关闭数据库
mySqlConnection.Close();//关闭数据库连接
}
}
}
运行的时候出现System.Data.sqlclient.SqlException:"Login failed for user 'sa'.The uer is not associated with a trusted SQL Server connection.这样的错误,求高人指点 展开
1个回答
展开全部
SQL Server服务器没有启用混合认证,将认证方式从集成认证改为混合认证
追问
请问怎么改?
求全套代码。
追答
修改注册表
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\LoginMode
LoginMode的键值:1改成2,2 表示混合模式。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询