connect to database c#

后端 未结 2 1686
面向向阳花
面向向阳花 2021-01-06 18:59

I\'m trying to connect to a DB on a server using C# but with no luck.

I tried using this:

public static string m_ConnectionString =
    @\"Network Li         


        
相关标签:
2条回答
  • 2021-01-06 19:23

    I think that Data Source=*server ip*,*port*; should be Data Source=*server ip*:*port*;, replacing , with :. But if the port is not specific, I don't think you really need it. Also you're not defining a driver, I don't know it this works without it.

    Also a advice: look up LINQ to SQL or ADO.NET Entity Data Model. Those can really simplify use of databases and using LINQ you can write a query inside code which is a lot similar to sql and Visual Studio also helps with intellisense so you don't have to remember all table and column names.

    0 讨论(0)
  • 2021-01-06 19:41

    A very good source for SQL Server (and many other) connection strings is http://www.connectionstrings.com/sql-server/. Depending whether you are connecting through ODBC, OLE DB or Native Client, you have to choose another connection string.

    Try

    Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;
    

    or

    Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
    

    There are a lot of options to choose from, depending on the exact SQL Server version, the security type and many more.


    UPDATE

    First you have to choose a data access technology.

    • .NET Framework Data Provider for SQL Server (SqlConnection), Is the preferred way of accessing the SQL Server from .NET code. (See When to use the SQL Native Client for a comparison)

    • Native Client: Is a very fast way of accessing the SQL Server and supports the new features, as it accesses the SQL Server TDS protocol directly and works for non .NET code. It should be preferred for non .NET code.

    • ODBC: Is relatively fast and compatible to a lot of different databases. Choose this one if the data base type might change in future or if you are accessing "exotic" databases.

    • OLEDB: For SQL Server it is relatively slow and will be depreciated by Microsoft.

    Then you have to choose between SQL Server Authentication (User/Password) and Windows Authentication. I would choose the latter if possible. With Windows Authentication the SQL-Server assumes that if you logged in successfully to Windows you are a trusted user. The Windows user name will then be mapped 1 to 1 to a SQL-Server user. Of course this user then must still have been granted the rights requested for the operations that he will perform on the SQL Server (like SELECT, INSERT, UPDATE, DELETE). If the DBA didn't install Windows Authentication, you will have to go with uid/pwd.

    This worked for me:

    string connectionString =
        "Data Source=192.168.123.45;Initial Catalog=MyDatabase;Integrated Security=SSPI;";
    using (SqlConnection connection = new SqlConnection(connectionString)) {
        using (SqlCommand command = new SqlCommand(
                     "SELECT Region FROM dbo.tlkpRegion WHERE RegionID=30", connection)) {
            connection.Open();
            string result = (string)command.ExecuteScalar();
            MessageBox.Show("Region = " + result);
        }
    }
    
    0 讨论(0)
提交回复
热议问题