我该如何解决SQL问题 [英] How do I solve the SQL problem

查看:90
本文介绍了我该如何解决SQL问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PC1上运行c#应用程序,在PC2上运行sql server,我已经配置了sql服务器远程访问和工作。所以现在的问题是,当我运行c#应用程序时,我收到此错误,其中显示无法打开数据库database_name登录请求。登录失败。'



请帮我解决这个问题。



这是我的连接字符串:

数据源= ip地址 for  PC2; Initial Catalog = database_name; Persist 安全性信息=真; 用户 ID = user_id;密码= my_password 





我尝试了什么:



我试图更改连接字符串但仍然出现错误。

解决方案

登录失败 - 所以最可能的是你使用的ID和密码组合对于奶嘴SQL服务器实例无效。



试试这个:简单的SQL连接字符串创建 [ ^ ]并使用PC1通过Windows建立与DB的连接。当它工作时,您可以看看将字符串放入您的应用程序。



BTW:我强烈建议使用主机名而不是IP地址 - 它们往往会改变当机器和/或路由器重新上电时。这也意味着如果计算机死机,另一台计算机可以获得相同的主机名,并且所有应用程序都可以继续工作。


确保使用正确的身份验证方法 :Windows或SQL Server身份验证: SQL Server中的身份验证Microsoft Docs [ ^ ]

如果您有SQL Server Express的命名实例,则需要在连接字符串中添加该实例,例如 Server = 123.4.5.6 \sqlexpress

这是一篇很好的CodeProject文章,其中有一个例子:如何将SQL数据库连接到您的C#程序,初学者的教程 [ ^

Im running a c# application on PC1 and the sql server on PC2, i have configured the sql server to be accessed remote and working. So the problem now is that when i run the c# application i got this error which says 'Cannot open database "database_name" requested by the login. The login failed.'

Please help me solve this problem.

Here is my connection string:

Data Source=ip address for PC2;Initial Catalog=database_name;Persist Security Info=True;User ID=user_id;Password=my_password



What I have tried:

I have tried to change the connection string but still i got the error.

解决方案

The login is failing - so the most likely thing is that the ID and password combination you are using is not valid for teat SQL server instance.

Try this: Simple SQL Connection String Creation[^] and use PC1 to establish a connection to the DB via Windows. When that works, you can look at putting the string into your app.

BTW: I would strongly suggest using a hostname instead of an IP address - they tend to change when machines and / or routers are power cycled. It also means that if a computer dies, another can be given the same hostname and all apps continue to work.


Make sure you use the correct method of authentication: Windows or SQL Server authentication: Authentication in SQL Server | Microsoft Docs[^]
If you have a named instance of SQL Server Express you will need to add that in the connection string, e.g. "Server=123.4.5.6\sqlexpress".
Here is a good CodeProject article with an example: How to connect SQL Database to your C# program, beginner's tutorial[^]


这篇关于我该如何解决SQL问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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