I'm developing a simple C# application, I'd like to know this: When I connect my application to SQL Server on my PC, I know the connection string (server name, password etc.), but when I connect it to another PC, the SQL Server connection string is different. Is there a common account in SQL Server that come with default account that can connect? I have heard about sa
account in SQL Server, what is sa
?
相关问题
- Sorting 3 numbers without branching [closed]
- sql execution latency when assign to a variable
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
You can use either Windows authentification, if your server is in Domain, or Sql authentification. Sa - is a System Administratior, the root account for SQL server authentification. But it is a bad practice to use if for conneting of your clients. You should create your own accounts, and use them to connect to your SQL. In each connection you set account login, its password and the default database, you want to connect.
.NET Data Provider -- Default Relative Path -- Standard Connection
.NET Data Provider -- Default Relative Path -- Trusted Connection
.NET Data Provider -- Custom Relative Path -- Standard Connection
.NET Data Provider -- Custom Relative Path -- Trusted Connection
Actually you can use the
SqlConnectionStringBuilder
class to build your connection string. To build the connection string, you need to instantiate an object from thatSqlConnectionStringBuilder
and set their properties with the parameters you use to connect to the DataBase. Then you can get the connection string from theConnectionString
property from theSqlConnectionStringBuilder
object, as is shown in this example:You can either use the
new
operator to make that directly.You can add more parameters to build your connection string. Remember that the parameters are defined by the values setted in the
SqlConnectionStringBuilder
object properties.Also you can get the database connection string from the conection of Microsoft Visual Studio with the attached DB. When you select the DB, in the properties panel is shown the connection string.
The complete list of properties of the
SqlConnectionStringBuilder
class is listed in this page from the Microsoft MSDN site.About the default user of SQL Server, sa means "system-administrator" and its password varies according the SQL Server version. In this page you can see how the password varies.
You can log in with sa user in this login window at the start of SQL Server Database Manager. Like in this image:
// .NET DataProvider -- Standard Connection with username and password
// .NET DataProvider -- Trusted Connection
They are a number of things to worry about when connecting to SQL Server on another machine.
Very often SQL server may be running as a default intance which means you can simply specify the hostname/ip address but you may encounter a scenario where it is running as a named instance (Sql Express for instance). In this scenario you'll have to specify hostname\instance name .
You need to understand that a database server or DBA would not want just anyone to be able to connect or modify the contents of the server. This is the whole purpose of security accounts. If a single username/pwd would work on just any machine, it would provide no protection. That "sa" thing you have heard of, does not work with SQL Server 2005, 2008 or 2012. Not sure about previous versions though. I believe somewhere in the early days of SQL Server, the default username and pwd used to be sa/sa, but that is no longer the case.
FYI, database security and roles are much more complicated now-a-days. You may want to look into the details of Windows-based authentication. If your SQL Server is configured for it, you don't need any username/pwd in the connection string to connect to it. All you need to change is the server machine name and the same connection string will work with both your machines, given both have same db name of course.