微软企业库错误 [英] microsoft enterprise library-error

查看:80
本文介绍了微软企业库错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我的代码
我在这条线上有一个错误----

 Console.WriteLine(" ,名为DB.ExecuteScalar(sqlCmd).ToString()); 


-----错误是
----建立与SQL Server的连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称正确,并且已将SQL Server配置为允许远程连接. (提供者:TCP提供程序,错误:0-未知此类主机.)

 受保护的  void  btnClick_Click(对象发​​件人,EventArgs e)
   {
       /// /从容器中解析默认的数据库对象.
 /// /实际的具体类型由配置设置确定.
 // 数据库defaultDB = EnterpriseLibraryContainer.Current.GetInstance< Database>(); 

       // 使用连接字符串名称从容器中解析数据库对象.
       名为DB = EnterpriseLibraryContainer.Current.GetInstance< Database>(" )的数据库;

       // 使用默认数据库从容器中解析SqlDatabase对象.
       SqlDatabase sqlServerDB = EnterpriseLibraryContainer.Current.GetInstance< Database>(" )// 假定方法GetConnectionString存在于您的应用程序中,并且
       // 返回有效的连接字符串.
       字符串 myConnectionString = GetConnectionString();

       SqlDatabase sqlDatabase =  SqlDatabase(myConnectionString);

       字符串 sUser = txtUserId.Text;
       字符串 pass = txtPassword.Text;
       使用(DbCommand sqlCmd = namedDB.GetSqlStringCommand("  + sUser +  '和password ='" + pass + " ))
       {
           // 调用命令的ExecuteScalar方法.
           Console.WriteLine(" 的结果,名为DB.ExecuteScalar(sqlCmd) .ToString());


       }


   }

   私有 字符串 GetConnectionString()
   {

       名为DB = DatabaseFactory.CreateDatabase(" )的数据库;
       返回 namedDB.ConnectionString;

       // 抛出新的NotImplementedException(); 

   } 

解决方案

检查CP中给出的以下答案:
建立连接时发生与网络相关或与实例有关的错误到SQL Server.找不到服务器或无法访问服务器. [ ^ ]


hi my code
i have one error on this line----

Console.WriteLine("Result using a SQL statement: {0}", namedDB.ExecuteScalar(sqlCmd).ToString());


-----error is
----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: TCP Provider, error: 0 - No such host is known.)

protected void btnClick_Click(object sender, EventArgs e)
   {
       //// Resolve the default Database object from the container.
       //// The actual concrete type is determined by the configuration settings.
       //Database defaultDB = EnterpriseLibraryContainer.Current.GetInstance<Database>();

       // Resolve a Database object from the container using the connection string name.
       Database namedDB = EnterpriseLibraryContainer.Current.GetInstance<Database>("grammart");

       // Resolve a SqlDatabase object from the container using the default database.
       SqlDatabase sqlServerDB = EnterpriseLibraryContainer.Current.GetInstance<Database>("grammart") as SqlDatabase;


       // Assume the method GetConnectionString exists in your application and
       // returns a valid connection string.
       string myConnectionString =GetConnectionString();

       SqlDatabase sqlDatabase = new SqlDatabase(myConnectionString);

       string sUser = txtUserId.Text;
       string pass = txtPassword.Text;
       using (DbCommand sqlCmd = namedDB.GetSqlStringCommand("select count(UserID) as UserID from M_User where UserID= '" + sUser + "'and password = '" + pass + "'"))
       {
           // Call the ExecuteScalar method of the command.
           Console.WriteLine("Result using a SQL statement: {0}", namedDB.ExecuteScalar(sqlCmd).ToString());


       }


   }

   private string GetConnectionString()
   {

       Database namedDB = DatabaseFactory.CreateDatabase("grammart");
       return namedDB.ConnectionString;

       //throw new NotImplementedException();

   }

解决方案

Check this answer given in CP:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.[^]


这篇关于微软企业库错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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