用户'(null)'登录失败。原因:与受信任的SQL Server连接无关。 [英] Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection.

查看:73
本文介绍了用户'(null)'登录失败。原因:与受信任的SQL Server连接无关。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#编写了一个全局函数代码。



我的代码如下:

I have written a global function code using C#.

My code is as follows:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;


namespace Faculty_Schedule_Time_Table
{

        public class Global_Function
    {

            public SqlConnection con = new SqlConnection();
            private string connectionstring ="Data Source=NARASIMAN;connect timeout=120;initial catalog=master";
            private SqlCommand cmd;
            private SqlDataReader dr;

            public string error;

            public Global_Function()
            {

            }

            public void BindCon()
            {
                con = new SqlConnection();
                con.ConnectionString = connectionstring;
                con.Open();

            }

            public void insertdata(String SQL)
            {
                try
                {
                    BindCon();
                    cmd = new SqlCommand(SQL, con);
                    cmd.ExecuteReader();
                }
                 catch(Exception e1)
                  {
                   error = e1.Message.ToString();
                  }
            }
            public SqlDataReader ReadSql(string SQL)
            {

            con = new SqlConnection(connectionstring);
            con.Open();
            cmd  = new SqlCommand(SQL,con);
            dr = cmd.ExecuteReader();
            return (dr);
            
        }
           
            public string CntString()
        {
            return connectionstring;
        }
    }
}



当我运行错误时显示如下:


When I run error shows as follows:

Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection.



以上错误显示在下面的代码 con.open(); line


The above error shows in the below code con.open(); line

public SqlDataReader ReadSql(string SQL)
       {

       con = new SqlConnection(connectionstring);
       con.Open();(in this line the above error shows)
       cmd  = new SqlCommand(SQL,con);
       dr = cmd.ExecuteReader();
       return (dr);

   }



如何解决这个问题?



请帮帮我。



问候&谢谢,

Narasiman P


How can I fix this?

Please help me.

Regards & Thanks,
Narasiman P

推荐答案

你的连接字符串不好。您必须指定用户名和密码或将其设置为使用可信连接(请在此处查看正确的语法: http:// www .connectionstrings.com / sql-server-2008 [ ^ ],假设SQL 2008;但在那里你也可以找到其他版本的SQL服务器和其他数据源。

可能是这样的:

private string connectionstring = @Server = NARASIMAN; Database = master; Trusted_Connection = True;;
Your connection string is not good. You have to either specify user name and password or set it to use trusted connection (check here for proper syntax: http://www.connectionstrings.com/sql-server-2008[^], supposing SQL 2008; but there you can find other versions of SQL server and other datasources too).
Could be this:
private string connectionstring =@"Server=NARASIMAN;Database=master;Trusted_Connection=True;";


这篇关于用户'(null)'登录失败。原因:与受信任的SQL Server连接无关。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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